Skip to content

Commit 8aad3ed

Browse files
authored
fix: isolate API token from user code scope (#25)
* security: isolate API token from user code scope Move user code execution from evaluate() into a separate private #run() method. The apiToken parameter is only accessible within evaluate(), where it gets captured by the cloudflare.request() closure and stored on a private class field. User code in #run() can access the cloudflare helper but not the raw token value. * test: update assertions for token isolation and GraphQL path fix
1 parent 61e7f4b commit 8aad3ed

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/executor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ const apiBase = ${JSON.stringify(apiBase)};
2424
const accountId = ${JSON.stringify(accountId)};
2525
2626
export default class CodeExecutor extends WorkerEntrypoint {
27+
#cloudflare = null;
28+
2729
async evaluate(apiToken) {
28-
const cloudflare = {
30+
this.#cloudflare = {
2931
async request(options) {
3032
const { method, path, query, body, contentType, rawBody } = options;
3133
@@ -114,6 +116,11 @@ export default class CodeExecutor extends WorkerEntrypoint {
114116
}
115117
};
116118
119+
return this.#run();
120+
}
121+
122+
async #run() {
123+
const cloudflare = this.#cloudflare;
117124
try {
118125
const result = await (${code})();
119126
return { result, err: undefined };

src/tests/executor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe('GraphQL Support', () => {
220220
const workerConfig = loaderCall.mock.calls[0][1]()
221221
const workerCode = workerConfig.modules['worker.js']
222222

223-
expect(workerCode).toContain('const cloudflare = {')
223+
expect(workerCode).toContain('this.#cloudflare = {')
224224
expect(workerCode).toContain('async request(options)')
225225
})
226226

0 commit comments

Comments
 (0)