From a53b9583d59a40640a83c34595db3b86d756ba6c Mon Sep 17 00:00:00 2001 From: Jose Camargo Date: Mon, 19 Oct 2026 21:15:16 -0300 Subject: [PATCH] fix: redact record IDs in JSON shape keys Signed-off-by: Jose Camargo --- .../scripts/page/api-shapes.js | 22 +++++++++---- tests/page-scripts.test.mjs | 32 +++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/skills/reverse-engineering-saas/scripts/page/api-shapes.js b/skills/reverse-engineering-saas/scripts/page/api-shapes.js index 2a824b1..e27635c 100644 --- a/skills/reverse-engineering-saas/scripts/page/api-shapes.js +++ b/skills/reverse-engineering-saas/scripts/page/api-shapes.js @@ -77,7 +77,15 @@ const typeOf = (value) => (value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value); - // Shape: primitives become "string", objects keep keys, arrays merge their first items. + // Redact recognizable record IDs, including IDs embedded in map keys. Ordinary + // schema fields such as oauth2 and sha256 remain useful. This is a heuristic; + // arbitrary names or opaque alphabetic keys cannot be classified reliably. + const shapeKey = (key) => + /^\d+$/.test(key) || /\d{4,}/.test(key) || + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(key) + ? ':id' : key; + + // Shape: primitives become "string", objects redact ID keys, arrays merge their first items. const shapeOf = (value, depth = 0, path = '', enums = null) => { const type = typeOf(value); if (type === 'array') { @@ -87,15 +95,17 @@ } if (type === 'object') { if (depth >= MAX_DEPTH) return '{…}'; - const out = {}; + const out = Object.create(null); const keys = Object.keys(value); for (const key of keys.slice(0, MAX_KEYS)) { const child = value[key]; + const safeKey = shapeKey(key); + const childPath = `${path}.${safeKey}`; if (enums && re.api?.enumSamples !== false && typeof child === 'string' && ENUM_KEY.test(key) && looksLikeEnum(child)) { - const set = (enums[`${path}.${key}`] = enums[`${path}.${key}`] || new Set()); + const set = (enums[childPath] = enums[childPath] || new Set()); if (set.size < MAX_ENUM_VALUES) set.add(child); } - out[key] = shapeOf(child, depth + 1, `${path}.${key}`, enums); + out[safeKey] = mergeShapes(out[safeKey], shapeOf(child, depth + 1, childPath, enums)); } if (keys.length > MAX_KEYS) out['…'] = `${keys.length - MAX_KEYS} more keys`; return out; @@ -112,8 +122,8 @@ } if (Array.isArray(a) && Array.isArray(b)) return a.length && b.length ? [mergeShapes(a[0], b[0])] : a.length ? a : b; if (a && b && typeof a === 'object' && typeof b === 'object' && !Array.isArray(a) && !Array.isArray(b)) { - const out = { ...a }; - for (const key of Object.keys(b)) out[key] = key in a ? mergeShapes(a[key], b[key]) : b[key]; + const out = Object.assign(Object.create(null), a); + for (const key of Object.keys(b)) out[key] = Object.hasOwn(a, key) ? mergeShapes(a[key], b[key]) : b[key]; return out; } return 'mixed'; diff --git a/tests/page-scripts.test.mjs b/tests/page-scripts.test.mjs index d240203..ef6b27c 100644 --- a/tests/page-scripts.test.mjs +++ b/tests/page-scripts.test.mjs @@ -195,3 +195,35 @@ test('annotate outlines regions and reports missing selectors', async () => { await page.evaluate(() => window.__re.annotate.clear()); assert.equal(await page.evaluate(() => document.getElementById('__re-annotate')), null); }); + + +test('api-shapes redacts record keys and merges their shapes and enum paths', async () => { + const ids = ['900000000001', '86abc1234', '123e4567-e89b-12d3-a456-426614174000', 'workspace_900000000002']; + const payload = { + records: Object.fromEntries(ids.map((id, index) => [id, { status: index ? 'done' : 'open', value: index ? 1 : 'private value' }])), + sha256: 'private digest', oauth2: true, + }; + await page.route('http://fixture.test/key-map', (route) => route.fulfill({ json: payload })); + await page.evaluate(script('api-shapes.js')); + try { + await page.evaluate(async (body) => { + window.__re.api.clear(); + await fetch('/key-map', { method: 'POST', body: JSON.stringify(body) }); + }, payload); + await page.waitForFunction(() => window.__re.api.count() === 1); + const [entry] = await page.evaluate(() => window.__re.api.dump()); + for (const shape of [entry.request, entry.response]) { + assert.deepEqual(Object.keys(shape.records), [':id']); + assert.equal(shape.records[':id'].value, 'number|string'); + assert.equal(shape.sha256, 'string'); + assert.equal(shape.oauth2, 'boolean'); + } + assert.deepEqual(entry.enumSamples['response.records.:id.status'].sort(), ['done', 'open']); + assert.deepEqual(entry.enumSamples['request.records.:id.status'].sort(), ['done', 'open']); + const output = JSON.stringify(entry); + for (const secret of [...ids, 'private value', 'private digest']) assert.ok(!output.includes(secret), `leaked ${secret}`); + } finally { + await page.evaluate(() => window.__re.api.uninstall()); + await page.unroute('http://fixture.test/key-map'); + } +});