Skip to content
Closed
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
15 changes: 14 additions & 1 deletion typescript/service-worker/src/sw/http_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export async function handleRequest(request: Request): Promise<Response> {
if (maybeCanisterId) {
try {
const replicaUrl = new URL(url.origin);
const agent = new HttpAgent({ host: replicaUrl.toString() });
const agent = new HttpAgent({ host: replicaUrl.toString(), fetch: self.fetch.bind(self) });
const actor = Actor.createActor(canisterIdlFactory, {
agent,
canisterId: maybeCanisterId,
Expand All @@ -256,19 +256,28 @@ export async function handleRequest(request: Request): Promise<Response> {
let certificate: ArrayBuffer | undefined;
let tree: ArrayBuffer | undefined;
let encoding = '';
let chunk_tree: ArrayBuffer | undefined;
let chunk_index = 0;
for (const [key, value] of httpResponse.headers) {
switch (key.trim().toLowerCase()) {
case 'ic-certificate':
{
const fields = value.split(/,/);
for (const f of fields) {
const [_0, name, b64Value] = [...f.match(/^(.*)=:(.*):$/)].map(x => x.trim());
if (name === 'chunk_index') {
chunk_index = parseInt(b64Value);
continue;
}

const value = base64Arraybuffer.decode(b64Value);

if (name === 'certificate') {
certificate = value;
} else if (name === 'tree') {
tree = value;
} else if (name === 'chunk_tree') {
chunk_tree = value;
}
}
}
Expand All @@ -293,6 +302,8 @@ export async function handleRequest(request: Request): Promise<Response> {
body.buffer,
certificate,
tree,
chunk_tree,
chunk_index,
agent,
shouldFetchRootKey,
);
Expand All @@ -306,6 +317,8 @@ export async function handleRequest(request: Request): Promise<Response> {
identity.buffer,
certificate,
tree,
chunk_tree,
chunk_index,
agent,
isLocal,
);
Expand Down
23 changes: 22 additions & 1 deletion typescript/service-worker/src/sw/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export async function validateBody(
body: ArrayBuffer,
certificate: ArrayBuffer,
tree: ArrayBuffer,
chunk_tree: ArrayBuffer | undefined,
chunk_index: number,
agent: HttpAgent,
shouldFetchRootKey = false,
): Promise<boolean> {
Expand Down Expand Up @@ -70,7 +72,26 @@ export async function validateBody(
return false;
}

return !!treeSha && equal(sha, treeSha);
if (!chunk_tree) {
return !!treeSha && equal(sha, treeSha); // backward compatibility
}

const hashChunkTree: HashTree = cbor.decode(new Uint8Array(chunk_tree));
const chunkWitnessHash = lookup_path([chunk_index.toString()], hashChunkTree);

if (!chunkWitnessHash) {
console.error(`Unable to lookup index path of chunk witness index=${chunk_index}`);
return false;
}

if (!equal(sha, chunkWitnessHash)) {
console.error(`Body hash does not equal witness chunk hash index=${chunk_index}`);
return false;
}

const reconstructedTreeSha = await reconstruct(hashChunkTree);

return equal(reconstructedTreeSha, treeSha);
}

function equal(buf1: ArrayBuffer, buf2: ArrayBuffer): boolean {
Expand Down