fix: scrub Bedrock gateway credentials at startup

This commit is contained in:
James Khor
2026-11-23 11:13:40 +08:00
parent 3b367c1433
commit 8f16794a97
3 changed files with 23 additions and 1 deletions
+2 -1
View File
@@ -5,7 +5,7 @@ import "os"
// secretEnv lists the credentials read from the environment only at startup:
// ARTEX_PG_DSN and ARTEX_PG_PASSWORD by NewManager (the password is a separate
// variable so Docker Compose does not paste it into a URL), ARTEX_SECRET_KEY,
// and the API keys by agent.FromEnv inside New.
// and the LLM API keys and custom headers by agent.FromEnv inside New.
// agent.FromEnv also reads ARTEX_LLM_PROXY and ARTEX_LLM_BASE_URL: both are URLs that
// can carry credentials in their userinfo (http://user:pass@proxy:3128). The archive
// store also captures ARTEX_ARCHIVE_S3_* credentials during New, before the scrub,
@@ -19,6 +19,7 @@ import "os"
// the environment: the scrub does not hide them from child processes.
var secretEnv = []string{
"ARTEX_PG_DSN", "ARTEX_PG_PASSWORD", "ARTEX_SECRET_KEY", "ANTHROPIC_API_KEY", "OPENAI_API_KEY",
"ARTEX_LLM_API_KEY", "ARTEX_LLM_CUSTOM_HEADERS",
"ARTEX_LLM_PROXY", "ARTEX_LLM_BASE_URL",
"ARTEX_ARCHIVE_S3_ACCESS_KEY_ID", "ARTEX_ARCHIVE_S3_SECRET_ACCESS_KEY", "ARTEX_ARCHIVE_S3_SESSION_TOKEN",
"AWS_ACCESS_KEY_ID", "AWS_ACCESS_KEY", "AWS_SECRET_ACCESS_KEY", "AWS_SECRET_KEY", "AWS_SESSION_TOKEN",
+18
View File
@@ -39,6 +39,24 @@ func TestScrubSecretEnvHidesSecretsFromChildren(t *testing.T) {
}
}
func TestScrubSecretEnvClearsBedrockGatewayCredentials(t *testing.T) {
// Preserve every startup secret that ScrubSecretEnv may unset for later tests.
for _, k := range secretEnv {
t.Setenv(k, os.Getenv(k))
}
// Set these independently of secretEnv so the test catches missing entries.
t.Setenv("ARTEX_LLM_API_KEY", "synthetic-gateway-token")
t.Setenv("ARTEX_LLM_CUSTOM_HEADERS", "X-Example-Secret: synthetic-header-value")
ScrubSecretEnv()
for _, k := range []string{"ARTEX_LLM_API_KEY", "ARTEX_LLM_CUSTOM_HEADERS"} {
if _, ok := os.LookupEnv(k); ok {
t.Errorf("%s is still set after ScrubSecretEnv", k)
}
}
}
// New must apply the options it receives. An earlier version accepted
// ...Option and never called them, so the binary kept the secrets in the
// environment while the unit test above still passed.
+3
View File
@@ -55,6 +55,9 @@ func TestSealedEnvironmentHandoff(t *testing.T) {
"ARTEX_SECRET_KEY": "",
"ANTHROPIC_API_KEY": "sk-ant-test",
"OPENAI_API_KEY": "=leading-equals",
// The Bedrock gateway reads a bearer token and supports multiline headers.
"ARTEX_LLM_API_KEY": "synthetic-gateway-token",
"ARTEX_LLM_CUSTOM_HEADERS": "X-Example-Secret: synthetic-header-value\nX-Example-Route: demo",
// Proxy and endpoint URLs can carry credentials in their userinfo.
"ARTEX_LLM_PROXY": "http://proxyuser:proxypass@proxy.internal:3128",
"ARTEX_LLM_BASE_URL": "https://apiuser:apipass@llm.internal/v1",