diff --git a/pm-stack/private-laptop-bridge/smoke-upload.mjs b/pm-stack/private-laptop-bridge/smoke-upload.mjs index 63d6ec7d2..1258fee30 100644 --- a/pm-stack/private-laptop-bridge/smoke-upload.mjs +++ b/pm-stack/private-laptop-bridge/smoke-upload.mjs @@ -160,7 +160,6 @@ function sanitizedBuildMetadata(buildConfig) { ? Object.fromEntries(Object.entries(value).map(([key, item]) => [key, item === null ? 'null' : Array.isArray(item) ? 'array' : typeof item])) : typeof value; const context = buildConfig?.buildContext; - const mode = context?.mode; return JSON.stringify({ rootKeys: buildConfig && typeof buildConfig === 'object' && !Array.isArray(buildConfig) ? Object.keys(buildConfig).sort() : [], rootTypes: types(buildConfig), @@ -171,15 +170,20 @@ function sanitizedBuildMetadata(buildConfig) { buildContextKeys: context && typeof context === 'object' && !Array.isArray(context) ? Object.keys(context).sort() : [], buildContextTypes: types(context), isPreview: typeof context?.isPreview === 'boolean' ? context.isPreview : '', - mode: mode === '' ? '' : mode === 'production' || mode === 'development' ? mode : '', + mode: context && Object.hasOwn(context, 'mode') ? (context.mode === '' ? '' : '') : '', }); } export function assertPrebuiltSmokePayload({ buildConfig, workerConfig, workerNames, bundleFiles }) { const exactKeys = (value, expected) => value && typeof value === 'object' && !Array.isArray(value) && same(Object.keys(value).sort(), [...expected].sort()); + const context = buildConfig?.buildContext; + // Pinned cf emits no mode locally when CF_MODE is unset, but an empty + // mode in the hosted workflow where CF_MODE is explicitly set to ''. + const knownContext = exactKeys(context, ['isPreview']) || + (exactKeys(context, ['isPreview', 'mode']) && context.mode === ''); if (!exactKeys(buildConfig, ['producer', 'buildContext']) || !exactKeys(buildConfig.producer, ['name', 'version']) || buildConfig.producer.name !== 'wrangler' || buildConfig.producer.version !== '4.166.0' || - !exactKeys(buildConfig.buildContext, ['isPreview', 'mode']) || buildConfig.buildContext.isPreview !== false || buildConfig.buildContext.mode !== '') { + !knownContext || context.isPreview !== false) { fail(`prebuilt Build Output producer or context is not the pinned non-preview build; sanitized metadata=${sanitizedBuildMetadata(buildConfig)}`); } if (!Array.isArray(workerNames) || workerNames.length !== 1 || workerNames[0] !== 'default') fail('prebuilt Build Output must contain only its default Worker'); diff --git a/pm-stack/private-laptop-bridge/smoke-upload.test.mjs b/pm-stack/private-laptop-bridge/smoke-upload.test.mjs index 17274cd16..bf979f5b2 100644 --- a/pm-stack/private-laptop-bridge/smoke-upload.test.mjs +++ b/pm-stack/private-laptop-bridge/smoke-upload.test.mjs @@ -175,7 +175,8 @@ function builtSmokeFixture() { END_MS: { type: 'text', value: '' }, }; return { - buildConfig: { producer: { name: 'wrangler', version: '4.166.0' }, buildContext: { isPreview: false, mode: '' } }, + // Unset CF_MODE on a local build omits mode from the pinned output. + buildConfig: { producer: { name: 'wrangler', version: '4.166.0' }, buildContext: { isPreview: false } }, workerNames: ['default'], bundleFiles: ['smoke.js', 'smoke.js.map'], workerConfig: { @@ -186,6 +187,32 @@ function builtSmokeFixture() { }; } +test('pinned Build Output accepts only the local and CI non-preview context shapes', () => { + // The hosted workflow sets CF_MODE explicitly to the empty string. + for (const context of [{ isPreview: false }, { isPreview: false, mode: '' }]) { + const fixture = builtSmokeFixture(); + fixture.buildConfig.buildContext = context; + assert.equal(assertPrebuiltSmokePayload(fixture), true); + } + for (const context of [ + null, [], {}, { mode: '' }, { isPreview: 'false' }, + { isPreview: true }, { isPreview: true, mode: '' }, + { isPreview: false, mode: null }, { isPreview: false, mode: false }, + { isPreview: false, mode: 0 }, { isPreview: false, mode: 'production' }, + { isPreview: false, mode: 'synthetic-secret-value' }, + { isPreview: false, extra: 'synthetic-secret-value' }, + { isPreview: false, mode: '', extra: 'synthetic-secret-value' }, + ]) { + const invalid = builtSmokeFixture(); + invalid.buildConfig.buildContext = context; + assert.throws(() => assertPrebuiltSmokePayload(invalid), error => { + assert.match(error.message, /sanitized metadata=/); + assert.doesNotMatch(error.message, /synthetic-secret-value/); + return true; + }); + } +}); + test('effective Build Output contract excludes routes, domains, triggers, previews, DOs, and extra executable modules', () => { const fixture = builtSmokeFixture(); assert.equal(assertPrebuiltSmokePayload(fixture), true);