Skip to content

Commit 4da1a0a

Browse files
committed
fix(apps): keep backend runtime context private
1 parent 30ef62c commit 4da1a0a

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

packages/plugins/apps/src/backend/virtual-entry.test.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,6 @@ describe('Backend Functions - generateVirtualEntryContent', () => {
3838
expect(result).toContain('export async function main($)');
3939
});
4040

41-
test('Should set globalThis.$ = $', () => {
42-
const result = generateVirtualEntryContent(
43-
'myHandler',
44-
'/src/handler.ts',
45-
PROJECT_ROOT,
46-
);
47-
expect(result).toContain('globalThis.$ = $');
48-
});
49-
5041
test('Should read args from $.backendFunctionArgs (no source-text substitution)', () => {
5142
const result = generateVirtualEntryContent(
5243
'myHandler',
@@ -176,8 +167,7 @@ describe('Backend Functions - args round-trip via $.backendFunctionArgs', () =>
176167
.join('\n')
177168
// Replace the call site with a $-bound handler we control.
178169
.replace(/await myHandler\(\.\.\.args\)/, 'await $.__handler(...args)');
179-
// The generated code declares globalThis.$, which has no effect in this
180-
// sandbox but is harmless. Wrap the body so we can return main().
170+
// Wrap the executable body so we can return main().
181171
const wrapper = `${body}\nreturn main($);`;
182172
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
183173
const fn = new Function(
@@ -229,4 +219,18 @@ describe('Backend Functions - args round-trip via $.backendFunctionArgs', () =>
229219
await main({});
230220
expect(received).toEqual([]);
231221
});
222+
223+
test('Should keep the runtime context out of customer global scope', async () => {
224+
const source = generateVirtualEntryContent('myHandler', '/src/handler.ts', PROJECT_ROOT);
225+
let exposedContext: unknown;
226+
const handler = () => {
227+
exposedContext = Reflect.get(global, '$');
228+
return 'ok';
229+
};
230+
const main = buildMainFromSource(source, handler);
231+
232+
await main({ backendFunctionArgs: [], privateRuntimeValue: 'secret' });
233+
234+
expect(exposedContext).toBeUndefined();
235+
});
232236
});

packages/plugins/apps/src/backend/virtual-entry.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ export function generateVirtualEntryContent(
2828
lines.push('');
2929
lines.push('/** @param {import("./context.types").Context} $ */');
3030
lines.push('export async function main($) {');
31-
lines.push(' globalThis.$ = $;');
32-
lines.push('');
3331
lines.push(` // Register the $.Actions-based implementation for executeAction`);
3432
lines.push(SET_EXECUTE_ACTION_SNIPPET);
3533
lines.push('');

0 commit comments

Comments
 (0)