Skip to content

Commit 6cf4946

Browse files
opencodeclaude
andcommitted
fix(opencode): guard embedded-docs map lookups against prototype-chain keys (TKT-391)
A request path of __proto__/constructor/etc. resolved through Object.prototype on the plain-object bundle map instead of a 404, since bracket lookup on a plain object walks the prototype chain. Object.hasOwn scopes both lookups to the map's own keys. No security consequence either way (Henry), but the wrong artifact is the wrong artifact -- guarding costs two words. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M1CJmqai46ihahdKMVNB27
1 parent c52118a commit 6cf4946

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎packages/opencode/src/server/shared/docs.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ function docsResponse(file: string, body: Uint8Array, status: number) {
3636
// not silently swapped for the homepage the way an SPA's index.html fallback would be.
3737
function lookup(embeddedWebDocs: Record<string, string>, requestPath: string) {
3838
const relative = requestPath.replace(/^\/docs\/?/, "").replace(/\/$/, "")
39-
const direct = embeddedWebDocs[relative]
40-
if (direct) return { file: direct, status: 200 }
41-
const indexed = embeddedWebDocs[relative ? `${relative}/index.html` : "index.html"]
42-
if (indexed) return { file: indexed, status: 200 }
39+
if (Object.hasOwn(embeddedWebDocs, relative)) return { file: embeddedWebDocs[relative], status: 200 }
40+
const indexKey = relative ? `${relative}/index.html` : "index.html"
41+
if (Object.hasOwn(embeddedWebDocs, indexKey)) return { file: embeddedWebDocs[indexKey], status: 200 }
4342
const notFoundPage = embeddedWebDocs["404.html"]
4443
if (notFoundPage) return { file: notFoundPage, status: 404 }
4544
return undefined

‎packages/opencode/test/server/httpapi-docs.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,31 @@ describe("HttpApi docs", () => {
116116
}),
117117
)
118118

119+
it.live("does not resolve prototype-chain properties for a request path", () =>
120+
Effect.gen(function* () {
121+
const fs = yield* FSUtil.Service
122+
let readPath: unknown
123+
const response = yield* serveEmbeddedDocsEffect(
124+
"/docs/__proto__/",
125+
{
126+
...fs,
127+
readFile: (path) => {
128+
readPath = path
129+
return Effect.succeed(new TextEncoder().encode("<html>not found</html>"))
130+
},
131+
},
132+
{ "404.html": "/$bunfs/root/404.html" },
133+
).pipe(Effect.map(HttpServerResponse.toWeb))
134+
135+
// Plain-object property lookup resolves __proto__/constructor/toString through the
136+
// prototype chain rather than the map's own keys -- guarded with Object.hasOwn.
137+
// Falls through to the branded 404 page like any other unmatched path, reading the
138+
// real 404.html file path rather than something derived from Object.prototype.
139+
expect(readPath).toBe("/$bunfs/root/404.html")
140+
expect(response.status).toBe(404)
141+
}),
142+
)
143+
119144
it.live("falls back to the bundle's own branded 404 page, served with a 404 status", () =>
120145
Effect.gen(function* () {
121146
const fs = yield* FSUtil.Service

0 commit comments

Comments
 (0)