Repository navigation
fix(core): bound the workflow meta evaluation #9136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d249cc
fix(core): bound the workflow meta evaluation
qqqys 8a4b227
fix(core): harden bounded meta serialization
qqqys aec7704
fix(core): contain workflow meta evaluation
qqqys e49a3b5
fix(core): bound workflow meta microtasks
qqqys 908ea66
fix(core): isolate workflow meta evaluation
qqqys ab0ba69
fix(core): isolate workflow evaluator startup options
qqqys c09ce9b
Merge branch 'main' into fix/workflow-meta-eval-bounds
qqqys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -231,7 +231,7 @@ describe('extractAndStripMeta', () => { | |
| it('rejects meta that references an unknown identifier', () => { | ||
| const src = `export const meta = { name: totallyUnknown, description: 'd' }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| /totallyUnknown is not defined/, | ||
| ); | ||
| }); | ||
|
|
||
|
|
@@ -266,6 +266,353 @@ describe('extractAndStripMeta', () => { | |
| expect(() => extractAndStripMeta(src)).toThrow(/unbalanced/i); | ||
| }); | ||
|
|
||
| // The meta literal is model-authored source, and every caller reaches it on | ||
| // a path where a wedged thread is unrecoverable: the run path (before the | ||
| // sandbox's own 30s body timeout is armed) and, in follow-up work, the tool | ||
| // confirmation dialog and the saved-workflow palette. A field value that | ||
| // never returns must surface as an ordinary malformed-meta error. | ||
| // | ||
| // Each case asserts a generous wall-clock bound rather than a precise one, | ||
| // so the assertion stays stable on a loaded CI runner. Without the bound | ||
| // these hang the worker until vitest's own timeout kills it. | ||
| describe('bounded evaluation', () => { | ||
| const BOUND_MS = 5_000; | ||
|
|
||
| function timed(fn: () => unknown): number { | ||
| const startedAt = Date.now(); | ||
| try { | ||
| fn(); | ||
| } catch { | ||
| /* the throw is asserted separately */ | ||
| } | ||
| return Date.now() - startedAt; | ||
| } | ||
|
|
||
| it('bounds a field value that loops on evaluation', () => { | ||
| const src = `export const meta = { name: (function () { while (true) {} })(), description: 'd' }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| ); | ||
| expect(timed(() => extractAndStripMeta(src))).toBeLessThan(BOUND_MS); | ||
| }); | ||
|
|
||
| // The case a timeout on the literal's own evaluation does NOT catch: a | ||
| // getter defers its work to property-read time, so the literal itself | ||
| // evaluates instantly and only spins when the value is walked. Walking on | ||
| // the host would run it on the host thread, unbounded. | ||
| it('bounds a getter that loops when the value is walked', () => { | ||
| const src = `export const meta = { name: 'x', description: 'd', get phases() { while (true) {} } }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| ); | ||
| expect(timed(() => extractAndStripMeta(src))).toBeLessThan(BOUND_MS); | ||
| }); | ||
|
|
||
| it('bounds a getter nested inside phases', () => { | ||
| const src = `export const meta = { name: 'x', description: 'd', phases: [{ get title() { while (true) {} } }] }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| ); | ||
| expect(timed(() => extractAndStripMeta(src))).toBeLessThan(BOUND_MS); | ||
| }); | ||
|
|
||
| it('refuses a meta literal that exceeds the serialized size cap', () => { | ||
| const src = `export const meta = { name: 'x'.repeat(200000), description: 'd' }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| ); | ||
| }); | ||
|
|
||
| it('enforces the cap after JSON escaping', () => { | ||
| const src = `export const meta = { name: '\\0'.repeat(20000), description: 'd' }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| ); | ||
| }); | ||
|
|
||
| it('enforces the cap for string-free containers', () => { | ||
| const src = `export const meta = { name: 'x', description: 'd', phases: Array.from({ length: 40000 }, () => ({})) }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| ); | ||
| }); | ||
|
|
||
| it('stops walking a holey array after the size budget is exhausted', () => { | ||
| const src = `export const meta = { name: 'x', description: 'd', phases: new Array(1e8) }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /meta literal is too large/, | ||
| ); | ||
| expect(timed(() => extractAndStripMeta(src))).toBeLessThan(BOUND_MS); | ||
| }); | ||
|
|
||
| it('bounds a looping then getter', () => { | ||
| const src = `export const meta = { name: 'x', description: 'd', extra: { get then() { while (true) {} } } }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| ); | ||
| expect(timed(() => extractAndStripMeta(src))).toBeLessThan(BOUND_MS); | ||
| }); | ||
|
|
||
| it('bounds a Promise subclass with a hostile species getter', () => { | ||
| const src = `export const meta = { | ||
| name: 'x', | ||
| description: 'd', | ||
| extra: new (class extends Promise { | ||
| static get [Symbol.species]() { while (true) {} } | ||
| })((resolve) => resolve(1)), | ||
| }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| ); | ||
| expect(timed(() => extractAndStripMeta(src))).toBeLessThan(BOUND_MS); | ||
| }); | ||
|
|
||
| it('does not invoke a phases iterator', () => { | ||
| const src = `export const meta = { | ||
| name: 'x', | ||
| description: 'd', | ||
| phases: Object.assign([{ title: 'one' }], { | ||
| [Symbol.iterator]: function () { while (true) {} }, | ||
| }), | ||
| }\nreturn 1`; | ||
| expect(extractAndStripMeta(src).meta?.phases).toEqual([{ title: 'one' }]); | ||
| }); | ||
|
|
||
| it('leaves a well-formed meta literal unaffected', () => { | ||
| const src = `export const meta = { name: 'w', description: 'd', phases: [{ title: 'One' }] }\nreturn 1`; | ||
| const { meta } = extractAndStripMeta(src); | ||
| expect(meta).toEqual({ | ||
| name: 'w', | ||
| description: 'd', | ||
| phases: [{ title: 'One' }], | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [ | ||
| 'literal timeout', | ||
| `export const meta = { name: (function () { Promise.reject(new Error('boom')); while (true) {} })(), description: 'd' }\nreturn 1`, | ||
| ], | ||
| [ | ||
| 'async literal timeout', | ||
| `export const meta = { name: (function () { (async () => { throw new Error('boom'); })(); while (true) {} })(), description: 'd' }\nreturn 1`, | ||
| ], | ||
| [ | ||
| 'proxy timeout before a sibling rejection', | ||
| `export const meta = { name: 'x', description: 'd', blocked: new Proxy({}, { ownKeys() { while (true) {} } }), rejected: Promise.reject(new Error('boom')) }\nreturn 1`, | ||
| ], | ||
| [ | ||
| 'throwing getter after creating a rejection', | ||
| `export const meta = { name: 'x', description: 'd', get extra() { Promise.reject(new Error('boom')); throw new Error('getter'); } }\nreturn 1`, | ||
| ], | ||
| [ | ||
| 'looping getter after creating a rejection', | ||
| `export const meta = { name: 'x', description: 'd', get extra() { Promise.reject(new Error('boom')); while (true) {} } }\nreturn 1`, | ||
| ], | ||
| ])( | ||
| 'contains rejected Promises when %s aborts evaluation', | ||
| async (_name, src) => { | ||
| const unhandled: unknown[] = []; | ||
| const onUnhandled = (reason: unknown) => unhandled.push(reason); | ||
| process.on('unhandledRejection', onUnhandled); | ||
| try { | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /failed to evaluate meta object literal/, | ||
| ); | ||
| await new Promise<void>((resolve) => setImmediate(resolve)); | ||
| expect(unhandled).toEqual([]); | ||
| } finally { | ||
| process.off('unhandledRejection', onUnhandled); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| it.each([ | ||
| [ | ||
| 'symbol key', | ||
| `export const meta = { name: 'x', description: 'd', [Symbol('hidden')]: Promise.reject(new Error('boom')) }\nreturn 1`, | ||
| ], | ||
| [ | ||
| 'prototype', | ||
| `export const meta = { __proto__: { hidden: Promise.reject(new Error('boom')) }, name: 'x', description: 'd' }\nreturn 1`, | ||
| ], | ||
| [ | ||
| 'function property', | ||
| `export const meta = { name: 'x', description: 'd', extra: Object.assign(function () {}, { hidden: Promise.reject(new Error('boom')) }) }\nreturn 1`, | ||
| ], | ||
| [ | ||
| 'species sabotage', | ||
| `export const meta = { name: 'x', description: 'd', extra: (function () { const p = Promise.reject(new Error('boom')); p.constructor = { [Symbol.species]: function () { throw new Error('species'); } }; return p; })() }\nreturn 1`, | ||
| ], | ||
| [ | ||
| 'Promise proxy', | ||
| `export const meta = { name: 'x', description: 'd', extra: new Proxy(Promise.reject(new Error('boom')), {}) }\nreturn 1`, | ||
| ], | ||
| [ | ||
| 'non-enumerable property', | ||
| `export const meta = { name: 'x', description: 'd', extra: Object.defineProperty({}, 'hidden', { value: Promise.reject(new Error('boom')) }) }\nreturn 1`, | ||
| ], | ||
| ])('rejects a Promise hidden behind a %s', async (_name, src) => { | ||
| const unhandled: unknown[] = []; | ||
| const onUnhandled = (reason: unknown) => unhandled.push(reason); | ||
| process.on('unhandledRejection', onUnhandled); | ||
| try { | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /meta values must not be Promises/, | ||
| ); | ||
| await new Promise<void>((resolve) => setImmediate(resolve)); | ||
| expect(unhandled).toEqual([]); | ||
| } finally { | ||
| process.off('unhandledRejection', onUnhandled); | ||
| } | ||
| }); | ||
|
|
||
| it('uses the Promise diagnostic for a top-level thenable', () => { | ||
| const src = `export const meta = { then: () => {}, name: 'x', description: 'd' }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /meta values must not be Promises/, | ||
| ); | ||
| }); | ||
|
|
||
| // The literal is evaluated as its own program, so it never shares a lexical | ||
| // scope with the serializer that walks it. Interpolating it into the | ||
| // serializer's scope would let it read the helpers and overwrite the flag | ||
| // that decides whether a thenable was found — i.e. disarm the check that | ||
| // keeps a stray rejected Promise from killing the host process. | ||
| it('meta source cannot observe or mutate the serializer scope', () => { | ||
| const src = `export const meta = { name: String(typeof copy) + ':' + String(typeof hasThenable), description: 'd' }\nreturn 1`; | ||
| const { meta } = extractAndStripMeta(src); | ||
| expect(meta?.name).toBe('undefined:undefined'); | ||
| }); | ||
|
|
||
| it('a thenable stays rejected even when the literal predefines the flag name', () => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修复:测试 fixture 现显式设置 hasThenable: false,覆盖预定义标志仍不能隐藏 Promise 的场景。验证:workflow-sandbox.test.ts 167/167 通过。 |
||
| const src = `export const meta = { name: 'x', description: 'd', hasThenable: false, phases: Promise.resolve(1) }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /meta values must not be Promises/, | ||
| ); | ||
| }); | ||
|
|
||
| it('isolates serializer intrinsics from the meta literal', () => { | ||
| const src = `export const meta = { | ||
| name: (JSON.stringify = () => ({ toString() { return '@'; } }), 'x'), | ||
| description: 'd', | ||
| }\nreturn 1`; | ||
| expect(extractAndStripMeta(src).meta).toEqual({ | ||
| name: 'x', | ||
| description: 'd', | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects Promises after the literal mutates serializer helpers', () => { | ||
| const src = `export const meta = { | ||
| name: ( | ||
| Object.keys = () => ['name', 'description'], | ||
| Promise.prototype.then = () => undefined, | ||
| 'x' | ||
| ), | ||
| description: 'd', | ||
| extra: Promise.resolve(1), | ||
| }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /meta values must not be Promises/, | ||
| ); | ||
| }); | ||
|
|
||
| it('does not expose host helpers to serializer getters', () => { | ||
| const src = `export const meta = { | ||
| name: 'x', | ||
| description: 'd', | ||
| extra: Object.defineProperty({}, 'value', { enumerable: true, get: function () { | ||
| const serializerGlobal = arguments.callee.caller.constructor('return globalThis')(); | ||
| if (typeof serializerGlobal.copy !== 'undefined') throw new Error('host helper exposed'); | ||
| return 'safe'; | ||
| } }), | ||
| }\nreturn 1`; | ||
| expect(extractAndStripMeta(src).meta).toEqual({ | ||
| name: 'x', | ||
| description: 'd', | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects serializer-envelope forgery through Object.prototype', () => { | ||
| const src = `export const meta = { | ||
| name: 'x', | ||
| description: 'd', | ||
| phases: Promise.resolve(1), | ||
| extra: Object.defineProperty({}, 'value', { enumerable: true, get: function () { | ||
| const serializerGlobal = arguments.callee.caller.constructor('return globalThis')(); | ||
| serializerGlobal.Object.prototype.toJSON = () => ({ | ||
| hasThenable: false, | ||
| tooLarge: false, | ||
| value: { name: 'forged', description: 'forged' }, | ||
| }); | ||
| return 'safe'; | ||
| } }), | ||
| }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow(); | ||
| }); | ||
|
|
||
| it('rejects an oversized payload after attempted envelope forgery', () => { | ||
| const src = `export const meta = { | ||
| name: 'x', | ||
| description: 'd', | ||
| get whenToUse() { | ||
| const serializerGlobal = arguments.callee.caller.constructor('return globalThis')(); | ||
| serializerGlobal.Object.prototype.toJSON = () => ({ | ||
| hasThenable: false, | ||
| tooLarge: false, | ||
| value: { name: 'forged', description: 'forged', whenToUse: 'A'.repeat(10 * 1024 * 1024) }, | ||
| }); | ||
| return 'A'.repeat(10 * 1024 * 1024); | ||
| }, | ||
| }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow(); | ||
| }); | ||
|
|
||
| it('prefers a Promise error after the size budget is exceeded', () => { | ||
| const src = `export const meta = { | ||
| name: 'x'.repeat(200000), | ||
| description: 'd', | ||
| extra: Promise.resolve(1), | ||
| }\nreturn 1`; | ||
| expect(() => extractAndStripMeta(src)).toThrow( | ||
| /meta values must not be Promises/, | ||
| ); | ||
| }); | ||
|
|
||
| it('copies shared phase objects at each array position', () => { | ||
| const src = `export const meta = { | ||
| name: 'x', | ||
| description: 'd', | ||
| phases: (function () { | ||
| const phase = { title: 'one' }; | ||
| return [phase, phase]; | ||
| })(), | ||
| }\nreturn 1`; | ||
| expect(extractAndStripMeta(src).meta?.phases).toEqual([ | ||
| { title: 'one' }, | ||
| { title: 'one' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [ | ||
| `export const meta = { name: 'x', description: 'd', whenToUse: () => {} }\nreturn 1`, | ||
| /meta.whenToUse must be a string/, | ||
| ], | ||
| [ | ||
| `export const meta = { name: 'x', description: 'd', phases: [{ title: 'one', detail: Symbol('x') }] }\nreturn 1`, | ||
| /meta.phases\[\].detail must be a string/, | ||
| ], | ||
| [ | ||
| `export const meta = { name: 'x', description: 'd', phases: [{ title: 'one', model: 1n }] }\nreturn 1`, | ||
| /meta.phases\[\].model must be a string/, | ||
| ], | ||
| ])('preserves invalid optional field types for validation', (src, error) => { | ||
| expect(() => extractAndStripMeta(src)).toThrow(error); | ||
| }); | ||
|
|
||
| // P4a adversarial review (HIGH × 3 lenses): the docstring at | ||
| // workflow-sandbox.ts:283-294 promises the returned meta is HOST-realm — | ||
| // a per-field copy that defends against T1/T8/T14-style vm-realm escape | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修复:新增无限循环 then getter 的超时回归,以及 phases 自定义 Symbol.iterator 不被调用的回归。验证:workflow-sandbox.test.ts 167/167 通过。