Skip to content

Commit

Permalink
Gradually check if format is ASAR/Pickle.
Browse files Browse the repository at this point in the history
Move the test in the "256-byte" group.
  • Loading branch information
Borewit committed Jul 28, 2020
1 parent ae93869 commit ee51d0d
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,24 @@ async function _fromTokenizer(tokenizer) {
};
}

if (check([0x04, 0x00, 0x00, 0x00]) && buffer.length >= 16) { // Rough & quick check Pickle/ASAR
const jsonSize = buffer.readUInt32LE(12);
if (jsonSize > 12 && jsonSize < 240 && buffer.length >= jsonSize + 16) {
try {
const header = buffer.slice(16, jsonSize + 16).toString();
const json = JSON.parse(header);
// Check if Pickle is ASAR
if (json.files) { // Final check, assuring Pickle/ASAR format
return {
ext: 'asar',
mime: 'application/x-asar'
};
}
} catch (_) {
}
}
}

if (
check([0x30, 0x30, 0x30, 0x30, 0x30, 0x30], {offset: 148, mask: [0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8]}) && // Valid tar checksum
tarHeaderChecksumMatches(buffer)
Expand Down Expand Up @@ -1355,21 +1373,6 @@ async function _fromTokenizer(tokenizer) {
}
}
}

// Check for Asar
if (checkString('{"files":{', {offset: 16})) {
try {
// Check header
const jsonSize = buffer.readUInt32LE(12);
const header = buffer.slice(16, jsonSize + 16).toString();
JSON.parse(header);

return {
ext: 'asar',
mime: 'application/x-asar'
};
} catch (_) {}
}
}

const stream = readableStream => new Promise((resolve, reject) => {
Expand All @@ -1382,7 +1385,8 @@ const stream = readableStream => new Promise((resolve, reject) => {
const pass = new stream.PassThrough();
let outputStream;
if (stream.pipeline) {
outputStream = stream.pipeline(readableStream, pass, () => {});
outputStream = stream.pipeline(readableStream, pass, () => {
});
} else {
outputStream = readableStream.pipe(pass);
}
Expand Down

0 comments on commit ee51d0d

Please sign in to comment.