audit: recover interrupted IndexedDB restores and harden codec edge cases

- Treat an existing destination database with no object stores (when the
  source has stores) as an interrupted upgrade: delete and retry instead of
  skipping it forever. Verified by killing Chrome mid-restore; the next run
  restored all 49,990 synthetic records.
- Keep snapshot error indices aligned with the actual database index.
- Look up Error constructors through a Map: a site-controlled Error.name of
  "constructor"/"__proto__"/"toString" could corrupt the value or abort the DB.
- Brand-check Number/String/Boolean wrappers instead of toString sniffing so
  Symbol.toStringTag cannot change a plain object's type; accept ordinary
  class instances as plain objects (prototype dropped, matching structured
  clone) while still rejecting platform-object tags.
- Preserve AggregateError.errors and ImageData.colorSpace.
- Document the empty-container retry, the 120 s per-origin timeout and the
  additional clone types.
This commit is contained in:
souta
2026-10-23 21:24:55 +09:00
parent 487282b5bd
commit 4f630de25f
5 changed files with 130 additions and 29 deletions
+21
View File
@@ -112,6 +112,27 @@ async function main(){
const afterInvalid=await verify.evaluate(()=>indexedDB.databases());
assert(!afterInvalid.some(x=>x.name==='portage-invalid-record-test'),
'Aborted restore must not leave a schema-only database');
// An interrupted upgrade can leave an empty version-1 database. The next
// run must detect that (no object stores) and retry it instead of
// reporting a permanent skip.
await verify.evaluate(()=>new Promise((ok,no)=>{
const r=indexedDB.open('portage-interrupted-test',1);
r.onsuccess=()=>{r.result.close();ok();};r.onerror=()=>no(r.error);
}));
const recovered=await restoreOrigin(verify,{databases:[{
name:'portage-interrupted-test',version:1,stores:[{
name:'data',keyPath:null,autoIncrement:false,indexes:[],
records:[[{t:'string',v:'k'},{t:'string',v:'v'}]]
}]
}]});
assert.equal(recovered.restored,1,JSON.stringify(recovered));
assert.equal(recovered.retried,1);
const recoveredValue=await verify.evaluate(async()=>{
const db=await new Promise((ok,no)=>{const r=indexedDB.open('portage-interrupted-test');r.onsuccess=()=>ok(r.result);r.onerror=()=>no(r.error)});
const value=await new Promise((ok,no)=>{const q=db.transaction('data','readonly').objectStore('data').get('k');q.onsuccess=()=>ok(q.result);q.onerror=()=>no(q.error)});
db.close();return value;
});
assert.equal(recoveredValue,'v');
}
await verify.close();await browser.close();browser=null;
assert.deepEqual(hits,before,'No real site traffic including verification');
+21
View File
@@ -31,6 +31,27 @@ const {encode,decode}=require('../tools/indexeddb_codec.js');
assert(decode(JSON.parse(JSON.stringify(await encode(Object(42))))) instanceof Number);
assert.equal(decode(JSON.parse(JSON.stringify(await encode(Object('x'))))).valueOf(),'x');
assert.equal(decode(JSON.parse(JSON.stringify(await encode(Object(false))))).valueOf(),false);
// Site-controlled Error.name must not be used as a plain-object key.
for(const name of ['constructor','__proto__','toString']){
const named=new Error('named');named.name=name;
const restored=decode(JSON.parse(JSON.stringify(await encode(named))));
assert(restored instanceof Error,name);
assert.equal(restored.message,'named');
}
if(typeof AggregateError!=='undefined'){
const aggregate=new AggregateError([new TypeError('first'),new Error('second')],'many');
const restored=decode(JSON.parse(JSON.stringify(await encode(aggregate))));
assert(restored instanceof AggregateError);
assert.equal(restored.message,'many');
assert.deepEqual(restored.errors.map((error)=>error.message),['first','second']);
}
// Symbol.toStringTag spoofing must not turn a plain object into a wrapper.
class Spoofed{constructor(){this.x=1;}get [Symbol.toStringTag](){return 'Number';}valueOf(){return 7;}}
await assert.rejects(()=>encode(new Spoofed()),/Unsupported IndexedDB object prototype/);
class Plainish{constructor(){this.x=1;}}
const plainish=decode(JSON.parse(JSON.stringify(await encode(new Plainish()))));
assert.equal(plainish.x,1);
assert.equal(Object.getPrototypeOf(plainish),Object.prototype);
// Typed arrays carry the source byte order; decoding the same big-endian
// payload on a little-endian host must still produce the original values.