Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(serve): warn instead of refusing an unauthenticated bind in a container

AkitaOnRailsb3d1c222d514
diff --git a/SECURITY.md b/SECURITY.md
index dd5131e8..dad4ac06 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -37,7 +37,17 @@ what the project is and is not designed to defend against.
The server fails closed before serving unauthenticated non-loopback HTTP.
`--allow-insecure-no-auth` is a deliberate dangerous override for an
- intentional plain-HTTP LAN deployment. Authentication does not encrypt
+ intentional plain-HTTP LAN deployment.
+
+ **Inside a container this check warns instead of refusing.** Publishing a
+ port with `-p` requires binding `0.0.0.0` in the namespace, so the bind
+ address says nothing about reachability there — that is decided by the
+ host-side publish spec, which the process cannot see. In a container the
+ thing to check is your own `-p`: `-p 127.0.0.1:49374:49374` is loopback-only
+ and safe without a token; anything broader needs `AI_MEMORY_AUTH_TOKEN`.
+ Note the `Host` allowlist is not a substitute — it defends against DNS
+ rebinding, where a browser sets the header, and a client that can route to
+ the port sets `Host` freely. Authentication does not encrypt
bearer tokens, so use a TLS reverse proxy for traffic beyond loopback; see
[`docs/https-via-proxy.md`](docs/https-via-proxy.md).
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rs
index 3810ffa5..97aee09d 100644
--- a/crates/ai-memory-cli/src/commands/serve.rs
+++ b/crates/ai-memory-cli/src/commands/serve.rs
@@ -94,15 +94,62 @@ fn configured(value: Option<&String>) -> bool {
value.is_some_and(|v| !v.trim().is_empty())
}
+/// What the bound address tells us about network exposure.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum HttpExposure {
+ /// Loopback-only, or authenticated. Nothing to warn about.
+ Safe,
+ /// Unauthenticated on a non-loopback address, allowed because the
+ /// operator passed `--allow-insecure-no-auth`.
+ InsecureByOverride,
+ /// Unauthenticated on a non-loopback address inside a container, where
+ /// the bind address is not evidence either way. See
+ /// [`validate_http_exposure`].
+ UndeterminedInContainer,
+}
+
+/// Detect that this process is running inside a container.
+///
+/// `/.dockerenv` is created by Docker, `/run/.containerenv` by Podman. The
+/// official image also sets `AI_MEMORY_IN_CONTAINER`, so the signal survives
+/// runtimes that create neither file.
+fn running_in_container() -> bool {
+ if std::env::var("AI_MEMORY_IN_CONTAINER").is_ok_and(|v| !v.trim().is_empty()) {
+ return true;
+ }
+ Path::new("/.dockerenv").exists() || Path::new("/run/.containerenv").exists()
+}
+
/// Refuse accidental unauthenticated network exposure after the listener has
/// selected its actual local address (which may differ from the bind input).
+///
+/// The check reads the bind address as evidence of reachability. That
+/// inference holds on a host, but **not** inside a container: publishing a
+/// port with `-p` requires binding `0.0.0.0` inside the namespace, and
+/// whether that port reaches the network is decided by the host-side publish
+/// spec — `-p 127.0.0.1:49374:49374` versus `-p 0.0.0.0:49374:49374` — which
+/// the process cannot observe. Refusing there is a false positive that took
+/// down the documented Quick Start container (#407), so containers get a
+/// loud warning instead.
+///
+/// Note this is deliberately not backstopped by the `Host` allowlist: that
+/// allowlist defends against DNS rebinding, where a browser sets the header.
+/// A client that can route to the port sets `Host` freely, so it is not an
+/// access control and cannot substitute for a token here.
fn validate_http_exposure(
local_addr: SocketAddr,
auth_enabled: bool,
allow_insecure_no_auth: bool,
-) -> Result<()> {
- if local_addr.ip().is_loopback() || auth_enabled || allow_insecure_no_auth {
- return Ok(());
+ containerized: bool,
+) -> Result<HttpExposure> {
+ if local_addr.ip().is_loopback() || auth_enabled {
+ return Ok(HttpExposure::Safe);
+ }
+ if allow_insecure_no_auth {
+ return Ok(HttpExposure::InsecureByOverride);
+ }
+ if containerized {
+ return Ok(HttpExposure::UndeterminedInContainer);
}
anyhow::bail!(
@@ -787,20 +834,35 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {
let local_addr = listener
.local_addr()
.context("reading bound HTTP listener address")?;
- validate_http_exposure(local_addr, auth_enabled, args.allow_insecure_no_auth)?;
+ let exposure = validate_http_exposure(
+ local_addr,
+ auth_enabled,
+ args.allow_insecure_no_auth,
+ running_in_container(),
+ )?;
info!(
%local_addr,
auth = auth_enabled,
body_limit_mb = MAX_BODY_BYTES / 1024 / 1024,
"MCP HTTP server ready (POST /mcp, POST /hook, Ctrl-C to stop)",
);
- if !auth_enabled && !local_addr.ip().is_loopback() {
+ if exposure == HttpExposure::InsecureByOverride {
tracing::warn!(
%local_addr,
"starting unauthenticated plain HTTP on a non-loopback address because \
--allow-insecure-no-auth was supplied — anyone on the network can call \
destructive MCP tools"
);
+ } else if exposure == HttpExposure::UndeterminedInContainer {
+ tracing::warn!(
+ %local_addr,
+ "no AI_MEMORY_AUTH_TOKEN configured. Inside a container the bind address \
+ cannot show whether this port reaches the network — that is decided by \
+ the host publish spec. If you published it with `-p 127.0.0.1:49374:49374` \
+ you are fine; if you published it on 0.0.0.0 or to a LAN address, anyone \
+ on the network can call destructive MCP tools. Generate a token with \
+ `ai-memory generate-auth-token` and set AI_MEMORY_AUTH_TOKEN"
+ );
} else if auth_enabled && !local_addr.ip().is_loopback() {
// Auth IS configured but the server is reachable from
// the network on plain HTTP. The bearer token (and
@@ -1656,9 +1718,13 @@ mod tests {
let local_addr: SocketAddr = address.parse().expect("valid test address");
for auth_enabled in [false, true] {
for allow_override in [false, true] {
+ // On a host, the bind address IS the evidence: an
+ // unauthenticated non-loopback bind is refused unless
+ // the operator overrode it.
let allowed = loopback || auth_enabled || allow_override;
assert_eq!(
- validate_http_exposure(local_addr, auth_enabled, allow_override).is_ok(),
+ validate_http_exposure(local_addr, auth_enabled, allow_override, false)
+ .is_ok(),
allowed,
"address={address}, auth={auth_enabled}, override={allow_override}"
);
@@ -1667,6 +1733,46 @@ mod tests {
}
}
+ /// Regression for #407. The published image binds `0.0.0.0` because that
+ /// is the only way `-p` publishing works, so the host-side rule above
+ /// refused every container started from the documented Quick Start and
+ /// left it crash-looping under `--restart unless-stopped`.
+ #[test]
+ fn containers_warn_instead_of_refusing_because_the_bind_proves_nothing() {
+ let quick_start: SocketAddr = "0.0.0.0:49374".parse().expect("valid test address");
+
+ // The exact Quick Start shape: no token, no override, in a container.
+ assert_eq!(
+ validate_http_exposure(quick_start, false, false, true).expect("must not refuse"),
+ HttpExposure::UndeterminedInContainer,
+ );
+
+ // Identical inputs on a host still refuse — the carve-out is scoped
+ // to the container case and does not soften the host rule.
+ assert!(validate_http_exposure(quick_start, false, false, false).is_err());
+
+ // A container is not a blanket downgrade: with auth configured the
+ // verdict is Safe, so the operator gets no spurious warning.
+ assert_eq!(
+ validate_http_exposure(quick_start, true, false, true).expect("auth is fine"),
+ HttpExposure::Safe,
+ );
+
+ // An explicit override still reports as an override, not as the
+ // container case, so the startup log keeps naming the real reason.
+ assert_eq!(
+ validate_http_exposure(quick_start, false, true, true).expect("override is fine"),
+ HttpExposure::InsecureByOverride,
+ );
+
+ // Loopback inside a container is plain Safe.
+ let loopback: SocketAddr = "127.0.0.1:49374".parse().expect("valid test address");
+ assert_eq!(
+ validate_http_exposure(loopback, false, false, true).expect("loopback is fine"),
+ HttpExposure::Safe,
+ );
+ }
+
#[test]
fn trusted_proxy_configuration_requires_distinct_credentials() {
let missing_root = AuthSettings {
diff --git a/docker/Dockerfile b/docker/Dockerfile
index 2a34c492..f73c87c6 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -53,6 +53,12 @@ ENV AI_MEMORY_DATA_DIR=/data
# exposed/homelab deployments still set their own `AI_MEMORY_ALLOWED_HOSTS`,
# which replaces this value entirely.
ENV AI_MEMORY_ALLOWED_HOSTS="localhost,127.0.0.1,::1,host.docker.internal"
+# Tell the server it is containerised, so the unauthenticated-bind guard in
+# `serve` knows the `0.0.0.0` bind below is a namespace requirement rather
+# than evidence of network exposure (issue #407). `/.dockerenv` covers Docker
+# and `/run/.containerenv` covers Podman; this makes the signal explicit and
+# runtime-independent.
+ENV AI_MEMORY_IN_CONTAINER=1
WORKDIR /data
EXPOSE 49374
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
diff --git a/docs/install.md b/docs/install.md
index fb281c55..70aff2b6 100644
--- a/docs/install.md
+++ b/docs/install.md
@@ -1715,6 +1715,12 @@ accepts requests. `--allow-insecure-no-auth` can override that refusal only
for an intentional dangerous plain-HTTP deployment; prefer
`AI_MEMORY_AUTH_TOKEN` or loopback instead.
+In a *container* that refusal becomes a warning, because the container must
+bind `0.0.0.0` internally for `-p` to work at all and cannot see which host
+address you published to. The `-p` above is therefore doing the real work: it
+is what keeps this container loopback-only. If you change it to publish on a
+LAN address, set `AI_MEMORY_AUTH_TOKEN` as well.
+
Then wire up the agent CLI. Both commands default to no auth and
`http://127.0.0.1:49374` - no extra flags needed for the local case: