Skip to content

Commit 507b73a

Browse files
authored
feat(agentapi): use wildcard alllowed hosts (#320)
Since coder/agentapi#49 was merged, agentapi by default only accepts requests with the `Host` header set to localhost, 127.0.0.1, or [::1]. In Coder, agentapi is served behind a reverse proxy as a workspace app, so we need to use a wildcard `AGENTAPI_ALLOWED_HOSTS` for agentapi-based modules to continue working. This PR updates the claude code and agentapi modules, and a subsequent PR will update modules that are based on the agentapi module.
1 parent 814f765 commit 507b73a

File tree

8 files changed

+45
-4
lines changed

8 files changed

+45
-4
lines changed

registry/coder/modules/agentapi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The AgentAPI module is a building block for modules that need to run an AgentAPI
1616
```tf
1717
module "agentapi" {
1818
source = "registry.coder.com/coder/agentapi/coder"
19-
version = "1.1.0"
19+
version = "1.1.1"
2020
2121
agent_id = var.agent_id
2222
web_app_slug = local.app_slug

registry/coder/modules/agentapi/main.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,17 @@ describe("agentapi", async () => {
236236
}
237237
}
238238
});
239+
240+
test("agentapi-allowed-hosts", async () => {
241+
// verify that the agentapi binary has access to the AGENTAPI_ALLOWED_HOSTS environment variable
242+
// set in main.sh
243+
const { id } = await setup();
244+
await execModuleScript(id);
245+
await expectAgentAPIStarted(id);
246+
const agentApiStartLog = await readFileContainer(
247+
id,
248+
"/home/coder/agentapi-mock.log",
249+
);
250+
expect(agentApiStartLog).toContain("AGENTAPI_ALLOWED_HOSTS: *");
251+
});
239252
});

registry/coder/modules/agentapi/scripts/main.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,7 @@ export LC_ALL=en_US.UTF-8
9595
cd "${WORKDIR}"
9696

9797
export AGENTAPI_CHAT_BASE_PATH="${AGENTAPI_CHAT_BASE_PATH:-}"
98+
# Disable host header check since AgentAPI is proxied by Coder (which does its own validation)
99+
export AGENTAPI_ALLOWED_HOSTS="*"
98100
nohup "$module_path/scripts/agentapi-start.sh" true "${AGENTAPI_PORT}" &>"$module_path/agentapi-start.log" &
99101
"$module_path/scripts/agentapi-wait-for-start.sh" "${AGENTAPI_PORT}"

registry/coder/modules/agentapi/testdata/agentapi-mock.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env node
22

33
const http = require("http");
4+
const fs = require("fs");
45
const args = process.argv.slice(2);
56
const portIdx = args.findIndex((arg) => arg === "--port") + 1;
67
const port = portIdx ? args[portIdx] : 3284;
78

89
console.log(`starting server on port ${port}`);
10+
fs.writeFileSync("/home/coder/agentapi-mock.log", `AGENTAPI_ALLOWED_HOSTS: ${process.env.AGENTAPI_ALLOWED_HOSTS}`);
911

1012
http
1113
.createServer(function (_request, response) {

registry/coder/modules/claude-code/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Run the [Claude Code](https://docs.anthropic.com/en/docs/agents-and-tools/claude
1313
```tf
1414
module "claude-code" {
1515
source = "registry.coder.com/coder/claude-code/coder"
16-
version = "2.0.6"
16+
version = "2.0.7"
1717
agent_id = coder_agent.example.id
1818
folder = "/home/coder"
1919
install_claude_code = true
@@ -84,7 +84,7 @@ resource "coder_agent" "main" {
8484
module "claude-code" {
8585
count = data.coder_workspace.me.start_count
8686
source = "registry.coder.com/coder/claude-code/coder"
87-
version = "2.0.6"
87+
version = "2.0.7"
8888
agent_id = coder_agent.example.id
8989
folder = "/home/coder"
9090
install_claude_code = true
@@ -102,7 +102,7 @@ Run Claude Code as a standalone app in your workspace. This will install Claude
102102
```tf
103103
module "claude-code" {
104104
source = "registry.coder.com/coder/claude-code/coder"
105-
version = "2.0.6"
105+
version = "2.0.7"
106106
agent_id = coder_agent.example.id
107107
folder = "/home/coder"
108108
install_claude_code = true

registry/coder/modules/claude-code/main.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import path from "path";
1010
import {
1111
execContainer,
1212
findResourceInstance,
13+
readFileContainer,
1314
removeContainer,
1415
runContainer,
1516
runTerraformApply,
@@ -319,4 +320,21 @@ describe("claude-code", async () => {
319320
agentApiUrl: "http://localhost:3284",
320321
});
321322
});
323+
324+
// verify that the agentapi binary has access to the AGENTAPI_ALLOWED_HOSTS environment variable
325+
// set in main.tf
326+
test("agentapi-allowed-hosts", async () => {
327+
const { id } = await setup();
328+
329+
const respModuleScript = await execModuleScript(id);
330+
expect(respModuleScript.exitCode).toBe(0);
331+
332+
await expectAgentAPIStarted(id);
333+
334+
const agentApiStartLog = await readFileContainer(
335+
id,
336+
"/home/coder/agentapi-mock.log",
337+
);
338+
expect(agentApiStartLog).toContain("AGENTAPI_ALLOWED_HOSTS: *");
339+
});
322340
});

registry/coder/modules/claude-code/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ resource "coder_script" "claude_code" {
241241
export LC_ALL=en_US.UTF-8
242242
243243
cd "${local.workdir}"
244+
245+
# Disable host header check since AgentAPI is proxied by Coder (which does its own validation)
246+
export AGENTAPI_ALLOWED_HOSTS="*"
247+
244248
nohup "$module_path/scripts/agentapi-start.sh" use_prompt &> "$module_path/agentapi-start.log" &
245249
"$module_path/scripts/agentapi-wait-for-start.sh"
246250
EOT

registry/coder/modules/claude-code/testdata/agentapi-mock.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ if (
2020
process.exit(1);
2121
}
2222

23+
fs.writeFileSync("/home/coder/agentapi-mock.log", `AGENTAPI_ALLOWED_HOSTS: ${process.env.AGENTAPI_ALLOWED_HOSTS}`);
24+
2325
console.log(`starting server on port ${port}`);
2426

2527
http

0 commit comments

Comments
 (0)