Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions skills/reverse-engineering-saas/scripts/page/api-shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@

const typeOf = (value) => (value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value);

// Shape: primitives become "string<iso-date>", 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<iso-date>", objects redact ID keys, arrays merge their first items.
const shapeOf = (value, depth = 0, path = '', enums = null) => {
const type = typeOf(value);
if (type === 'array') {
Expand All @@ -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;
Expand All @@ -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';
Expand Down
32 changes: 32 additions & 0 deletions tests/page-scripts.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});