use std::net::IpAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use wasmtime::component::{Component, HasSelf, Linker};
use wasmtime::{Config, Engine, ResourceLimiter, Store};
use crate::types::HostCtx;
wasmtime::component::bindgen!({
path: "wit",
world: "handler",
imports: { default: async | trappable },
exports: { default: async },
});
const MAX_KV_KEY_BYTES: usize = 512;
const MAX_KV_VALUE_BYTES: usize = 1024 * 1024;
pub(crate) const MAX_KV_PUTS_PER_INVOCATION: usize = 1000;
pub(crate) const MAX_KV_PUT_BYTES_PER_INVOCATION: usize = 8 * 1024 * 1024;
const MAX_LOG_MSG_BYTES: usize = 4 * 1024;
pub(crate) const MAX_LOG_LINES_PER_INVOCATION: usize = 100;
pub(crate) const MAX_LOG_BYTES_PER_INVOCATION: usize = 64 * 1024;
const MAX_HTTP_BODY_BYTES: usize = 1024 * 1024;
pub(crate) const HTTP_TIMEOUT: Duration = Duration::from_secs(5);
pub(crate) fn is_blocked_ip(ip: IpAddr) -> bool {
match ip {
IpAddr::V4(v4) => is_blocked_ipv4(v4),
IpAddr::V6(v6) => {
if let Some(mapped) = v6.to_ipv4_mapped() {
return is_blocked_ipv4(mapped);
}
let seg = v6.segments();
let low32 = std::net::Ipv4Addr::new(
(seg[6] >> 8) as u8,
seg[6] as u8,
(seg[7] >> 8) as u8,
seg[7] as u8,
);
if seg[..6] == [0; 6] && !v6.is_loopback() && !v6.is_unspecified()
|| seg[..6] == [0x64, 0xff9b, 0, 0, 0, 0]
{
return is_blocked_ipv4(low32);
}
if seg[0] == 0x2002 {
let v4 = std::net::Ipv4Addr::new(
(seg[1] >> 8) as u8,
seg[1] as u8,
(seg[2] >> 8) as u8,
seg[2] as u8,
);
return is_blocked_ipv4(v4);
}
let seg0 = seg[0];
(seg0 == 0x64 && seg[1] == 0xff9b && seg[2] == 1) || v6.is_loopback()
|| v6.is_unspecified()
|| v6.is_multicast()
|| (seg0 & 0xfe00) == 0xfc00 || (seg0 & 0xffc0) == 0xfe80 }
}
}
fn is_blocked_ipv4(v4: std::net::Ipv4Addr) -> bool {
v4.is_loopback()
|| v4.is_private()
|| v4.is_link_local()
|| v4.is_unspecified()
|| v4.is_broadcast()
|| v4.is_multicast()
|| (v4.octets()[0] == 100 && (v4.octets()[1] & 0xc0) == 64) || v4.octets()[0] == 0 || v4.octets()[..3] == [192, 0, 0] || (v4.octets()[0] == 198 && (v4.octets()[1] & 0xfe) == 18) || v4.octets()[0] >= 240 }
struct GuardedResolver {
allow_private: bool,
}
impl reqwest::dns::Resolve for GuardedResolver {
fn resolve(&self, name: reqwest::dns::Name) -> reqwest::dns::Resolving {
let allow_private = self.allow_private;
let host = name.as_str().to_string();
Box::pin(async move {
let addrs: Vec<std::net::SocketAddr> =
tokio::net::lookup_host((host.as_str(), 0)).await?.collect();
if allow_private {
return Ok(Box::new(addrs.into_iter()) as reqwest::dns::Addrs);
}
let public: Vec<std::net::SocketAddr> = addrs
.into_iter()
.filter(|a| !is_blocked_ip(a.ip()))
.collect();
if public.is_empty() {
return Err(Box::from(format!(
"{host}: dns resolution returned only blocked/private addresses"
))
as Box<dyn std::error::Error + Send + Sync>);
}
Ok(Box::new(public.into_iter()) as reqwest::dns::Addrs)
})
}
}
pub(crate) fn build_http_client(allow_private: bool) -> reqwest::Result<reqwest::Client> {
reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.timeout(HTTP_TIMEOUT)
.dns_resolver(Arc::new(GuardedResolver { allow_private }))
.no_proxy()
.build()
}
pub(crate) fn build_engine() -> wasmtime::Result<Engine> {
let mut cfg = Config::new();
cfg.epoch_interruption(true)
.consume_fuel(false)
.cranelift_opt_level(wasmtime::OptLevel::Speed)
.parallel_compilation(true);
Engine::new(&cfg)
}
pub(crate) const EPOCH_TICK_MS: u64 = 1;
pub(crate) struct EpochTicker {
stop: Arc<AtomicBool>,
handle: Option<std::thread::JoinHandle<()>>,
}
impl EpochTicker {
pub fn spawn(engine: Engine) -> Result<Self, crate::Error> {
let stop = Arc::new(AtomicBool::new(false));
let stop_thread = stop.clone();
let handle = std::thread::Builder::new()
.name("warpline-epoch-ticker".into())
.spawn(move || {
let tick = Duration::from_millis(EPOCH_TICK_MS);
let mut next = std::time::Instant::now();
while !stop_thread.load(Ordering::Relaxed) {
next += tick;
let now = std::time::Instant::now();
if next > now {
std::thread::sleep(next - now);
} else if now - next > tick * 10 {
next = now;
}
engine.increment_epoch();
}
})
.map_err(|e| crate::Error::Internal(format!("spawn epoch ticker thread: {e}")))?;
Ok(Self {
stop,
handle: Some(handle),
})
}
}
#[cfg(test)]
impl EpochTicker {
pub(crate) fn stop_flag(&self) -> Arc<AtomicBool> {
self.stop.clone()
}
}
impl Drop for EpochTicker {
fn drop(&mut self) {
self.stop.store(true, Ordering::Relaxed);
if let Some(handle) = self.handle.take() {
let _ = handle.join();
}
}
}
pub(crate) fn build_linker(engine: &Engine) -> wasmtime::Result<Linker<HostCtx>> {
let mut linker = Linker::new(engine);
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
Handler::add_to_linker::<HostCtx, HasSelf<HostCtx>>(&mut linker, |ctx| ctx)?;
Ok(linker)
}
pub(crate) fn instantiate_pre(
linker: &Linker<HostCtx>,
component: &Component,
) -> wasmtime::Result<HandlerPre<HostCtx>> {
let instance_pre = linker.instantiate_pre(component)?;
HandlerPre::new(instance_pre)
}
pub(crate) fn typecheck_component(
linker: &Linker<HostCtx>,
component: &Component,
) -> wasmtime::Result<()> {
instantiate_pre(linker, component)?;
Ok(())
}
fn truncate_utf8(s: &str, max_bytes: usize) -> &str {
if s.len() <= max_bytes {
return s;
}
let mut end = max_bytes;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
impl warpline::host::kv::Host for HostCtx {
async fn get(&mut self, key: String) -> wasmtime::Result<Option<Vec<u8>>> {
if key.len() > MAX_KV_KEY_BYTES {
wasmtime::bail!("kv key exceeds {MAX_KV_KEY_BYTES} byte cap");
}
Ok(self.kv.get(&self.tenant_id, &key).await?)
}
async fn put(&mut self, key: String, value: Vec<u8>) -> wasmtime::Result<()> {
if key.len() > MAX_KV_KEY_BYTES {
wasmtime::bail!("kv key exceeds {MAX_KV_KEY_BYTES} byte cap");
}
if value.len() > MAX_KV_VALUE_BYTES {
wasmtime::bail!("kv value exceeds {MAX_KV_VALUE_BYTES} byte cap");
}
self.kv_put_count += 1;
if self.kv_put_count > MAX_KV_PUTS_PER_INVOCATION {
wasmtime::bail!("kv put count exceeds {MAX_KV_PUTS_PER_INVOCATION} per invocation");
}
self.kv_put_bytes += key.len() + value.len();
if self.kv_put_bytes > MAX_KV_PUT_BYTES_PER_INVOCATION {
wasmtime::bail!(
"kv put bytes exceeds {MAX_KV_PUT_BYTES_PER_INVOCATION} per invocation"
);
}
self.kv.put(&self.tenant_id, &key, value).await?;
Ok(())
}
}
impl warpline::host::log::Host for HostCtx {
async fn emit(&mut self, level: String, msg: String) -> wasmtime::Result<()> {
if self.log_line_count >= MAX_LOG_LINES_PER_INVOCATION
|| self.log_bytes >= MAX_LOG_BYTES_PER_INVOCATION
{
if !self.log_suppressed_notified {
self.log_suppressed_notified = true;
tracing::warn!(
tenant = self.tenant_id.as_str(),
func = self.fn_name.as_str(),
"log output suppressed: per-invocation limit exceeded"
);
}
return Ok(());
}
let msg = truncate_utf8(&msg, MAX_LOG_MSG_BYTES);
self.log_line_count += 1;
self.log_bytes += msg.len();
let tenant = self.tenant_id.as_str();
let func = self.fn_name.as_str();
match level.to_ascii_lowercase().as_str() {
"trace" => tracing::trace!(tenant, func, msg, "guest log"),
"debug" => tracing::debug!(tenant, func, msg, "guest log"),
"warn" | "warning" => tracing::warn!(tenant, func, msg, "guest log"),
"error" => tracing::error!(tenant, func, msg, "guest log"),
_ => tracing::info!(tenant, func, msg, "guest log"),
}
Ok(())
}
}
impl warpline::host::http_out::Host for HostCtx {
async fn fetch(
&mut self,
req: warpline::host::http_out::Request,
) -> wasmtime::Result<Result<warpline::host::http_out::Response, String>> {
let allowed_hosts = self.allowed_hosts.clone();
let allow_private = self.allow_private_egress;
let client = self.http_client.clone();
Ok(http_fetch(allowed_hosts, allow_private, client, req).await)
}
}
async fn http_fetch(
allowed_hosts: Vec<String>,
allow_private: bool,
client: reqwest::Client,
req: warpline::host::http_out::Request,
) -> Result<warpline::host::http_out::Response, String> {
let url = url::Url::parse(&req.url).map_err(|e| format!("invalid url: {e}"))?;
match url.scheme() {
"http" | "https" => {}
other => return Err(format!("scheme {other} not allowed")),
}
let host = url.host_str().unwrap_or_default().trim_end_matches('.');
let allowed = allowed_hosts
.iter()
.any(|allowed| allowed.trim_end_matches('.') == host);
if !allowed {
return Err(format!("host {host} not allowed"));
}
if !allow_private {
let literal_ip = match url.host() {
Some(url::Host::Ipv4(v4)) => Some(IpAddr::V4(v4)),
Some(url::Host::Ipv6(v6)) => Some(IpAddr::V6(v6)),
_ => None,
};
if let Some(ip) = literal_ip {
if is_blocked_ip(ip) {
return Err(format!("host {ip} is a blocked/private address"));
}
}
}
let method = reqwest::Method::from_bytes(req.method.as_bytes())
.map_err(|e| format!("invalid method: {e}"))?;
let mut resp = client
.request(method, url)
.body(req.body)
.send()
.await
.map_err(|e| format!("request failed: {e}"))?;
let status = resp.status().as_u16();
let mut body = Vec::new();
while let Some(chunk) = resp
.chunk()
.await
.map_err(|e| format!("body read failed: {e}"))?
{
if body.len() + chunk.len() > MAX_HTTP_BODY_BYTES {
return Err(format!(
"response body exceeds {MAX_HTTP_BODY_BYTES} byte cap"
));
}
body.extend_from_slice(&chunk);
}
Ok(warpline::host::http_out::Response { status, body })
}
const INSTANTIATE_GRACE_TICKS: u64 = 2;
fn call_limit(ticks_used_by_instantiation: u64, budget_ticks: u64) -> u64 {
ticks_used_by_instantiation
.min(INSTANTIATE_GRACE_TICKS)
.saturating_add(budget_ticks)
}
#[derive(Debug)]
struct CpuBudgetExceededMarker;
impl std::fmt::Display for CpuBudgetExceededMarker {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("cpu budget exceeded")
}
}
impl std::error::Error for CpuBudgetExceededMarker {}
#[derive(Debug)]
pub(crate) enum Failure {
CpuBudget,
MemCap { cap_bytes: usize },
WallClock,
Trap(wasmtime::Error),
}
#[derive(Debug)]
pub(crate) struct Run {
pub result: Result<Vec<u8>, Failure>,
pub mem_peak: usize,
}
fn classify(err: wasmtime::Error, store: &Store<HostCtx>) -> Failure {
let is_cpu = err.downcast_ref::<CpuBudgetExceededMarker>().is_some()
|| matches!(
err.downcast_ref::<wasmtime::Trap>(),
Some(t) if *t == wasmtime::Trap::Interrupt
);
if is_cpu {
return Failure::CpuBudget;
}
let limiter = &store.data().limiter;
if limiter.cap_hit {
return Failure::MemCap {
cap_bytes: limiter.mem_cap_bytes,
};
}
Failure::Trap(err)
}
pub(crate) async fn run(
pre: &HandlerPre<HostCtx>,
ctx: HostCtx,
input: &[u8],
cpu_budget_ms: u64,
ticks: Arc<AtomicU64>,
) -> Run {
let wall_budget = Duration::from_millis(cpu_budget_ms) + HTTP_TIMEOUT + Duration::from_secs(1);
let budget_ticks = (cpu_budget_ms / EPOCH_TICK_MS).max(1);
let call = async move {
let mut store = Store::new(pre.engine(), ctx);
store.limiter(|c| &mut c.limiter as &mut dyn ResourceLimiter);
let limit = Arc::new(AtomicU64::new(
budget_ticks.saturating_add(INSTANTIATE_GRACE_TICKS),
));
let (ticks_cb, limit_cb) = (ticks.clone(), limit.clone());
store.epoch_deadline_callback(move |_store| {
let n = ticks_cb.fetch_add(1, Ordering::Relaxed) + 1;
if n > limit_cb.load(Ordering::Relaxed) {
Err(wasmtime::Error::new(CpuBudgetExceededMarker))
} else {
Ok(wasmtime::UpdateDeadline::Yield(1))
}
});
store.set_epoch_deadline(1);
let outcome = async {
let bindings = pre.instantiate_async(&mut store).await?;
limit.store(
call_limit(ticks.load(Ordering::Relaxed), budget_ticks),
Ordering::Relaxed,
);
bindings.call_handle(&mut store, input).await
}
.await;
let mem_peak = store.data().limiter.peak_bytes;
let result = outcome.map_err(|e| classify(e, &store));
Run { result, mem_peak }
};
match tokio::time::timeout(wall_budget, call).await {
Ok(run) => run,
Err(_elapsed) => Run {
result: Err(Failure::WallClock),
mem_peak: 0,
},
}
}
impl std::fmt::Debug for EpochTicker {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EpochTicker").finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests {
use super::is_blocked_ip;
use std::net::IpAddr;
#[test]
fn is_blocked_ip_classification_table() {
let cases: &[(&str, bool)] = &[
("127.0.0.1", true),
("127.255.255.255", true),
("::1", true),
("10.0.0.1", true),
("172.16.0.1", true),
("172.31.255.255", true),
("192.168.1.1", true),
("169.254.1.1", true),
("fe80::1", true),
("0.0.0.0", true),
("::", true),
("255.255.255.255", true),
("100.64.0.1", true),
("100.127.255.255", true),
("100.63.255.255", false),
("100.128.0.0", false),
("fc00::1", true),
("fd12:3456::1", true),
("224.0.0.1", true),
("ff02::1", true),
("::ffff:127.0.0.1", true),
("::ffff:10.0.0.1", true),
("::10.0.0.1", true),
("::8.8.8.8", false),
("64:ff9b::a00:1", true),
("64:ff9b::808:808", false),
("64:ff9b:1::1", true),
("2002:a00:1::", true),
("2002:808:808::", false),
("0.1.2.3", true),
("192.0.0.8", true),
("198.18.0.1", true),
("198.20.0.1", false),
("240.0.0.1", true),
("8.8.8.8", false),
("1.1.1.1", false),
("93.184.216.34", false),
("2001:4860:4860::8888", false),
("::ffff:8.8.8.8", false),
("172.15.255.255", false),
("172.32.0.0", false),
];
for (ip, expected) in cases {
let parsed: IpAddr = ip.parse().expect("valid ip literal in test table");
assert_eq!(
is_blocked_ip(parsed),
*expected,
"is_blocked_ip({ip}) expected {expected}"
);
}
}
#[test]
fn call_limit_forgives_at_most_the_grace() {
use super::{call_limit, INSTANTIATE_GRACE_TICKS as G};
for budget in [1u64, 10, 100] {
for used in 0..=(budget + G + 1) {
let total = call_limit(used, budget);
assert!(total <= budget + G, "budget {budget} used {used}");
assert!(total >= budget, "budget {budget} used {used}");
}
assert_eq!(call_limit(0, budget), budget);
assert_eq!(call_limit(1, budget), budget + 1);
assert_eq!(call_limit(2, budget), budget + 2);
assert_eq!(call_limit(50, budget), budget + 2);
}
assert_eq!(G, 2, "the exact values above assume a grace of 2 ticks");
}
}