2022-09-05 23:14:43 +03:00
|
|
|
import assert from "assert";
|
|
|
|
|
|
|
|
import {
|
|
|
|
lock,
|
|
|
|
computeHmac,
|
|
|
|
keccak256, ripemd160, sha256, sha512,
|
2022-10-01 02:56:13 +03:00
|
|
|
pbkdf2, scrypt, scryptSync,
|
|
|
|
randomBytes
|
2022-09-05 23:14:43 +03:00
|
|
|
} from "../index.js";
|
|
|
|
|
|
|
|
interface Algorithm {
|
2022-10-01 02:56:13 +03:00
|
|
|
(...args: Array<any>): string | Uint8Array | Promise<string>;
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
register: (func: any) => void;
|
|
|
|
lock: () => void;
|
|
|
|
_: (...args: Array<any>) => any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TestCase {
|
|
|
|
name: string;
|
|
|
|
params: Array<any>;
|
|
|
|
algorithm: Algorithm;
|
|
|
|
hijackTag: string;
|
2022-10-01 02:56:13 +03:00
|
|
|
postCheck?: (value: any) => boolean;
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
describe("test registration", function() {
|
|
|
|
|
|
|
|
let hijack = "";
|
|
|
|
function getHijack(algo: string) {
|
|
|
|
return function(...args: Array<any>) {
|
|
|
|
hijack = `hijacked ${ algo }: ${ JSON.stringify(args) }`;
|
|
|
|
return "0x42";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const tests: Array<TestCase> = [
|
|
|
|
{
|
|
|
|
name: "keccak256",
|
|
|
|
params: [ "0x" ],
|
|
|
|
hijackTag: 'hijacked keccak256: [{}]',
|
|
|
|
algorithm: keccak256
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sha256",
|
|
|
|
params: [ "0x" ],
|
|
|
|
hijackTag: 'hijacked sha256: [{}]',
|
|
|
|
algorithm: sha256
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sha512",
|
|
|
|
params: [ "0x" ],
|
|
|
|
hijackTag: 'hijacked sha512: [{}]',
|
|
|
|
algorithm: sha512
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ripemd160",
|
|
|
|
params: [ "0x" ],
|
|
|
|
hijackTag: 'hijacked ripemd160: [{}]',
|
|
|
|
algorithm: ripemd160
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pbkdf2",
|
|
|
|
params: [ "0x", "0x", 1024, 32, "sha256" ],
|
|
|
|
hijackTag: 'hijacked pbkdf2: [{},{},1024,32,"sha256"]',
|
|
|
|
algorithm: pbkdf2
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "scryptSync",
|
|
|
|
params: [ "0x", "0x", 1024, 8, 1, 32 ],
|
|
|
|
hijackTag: 'hijacked scryptSync: [{},{},1024,8,1,32]',
|
|
|
|
algorithm: scryptSync
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "scrypt",
|
|
|
|
params: [ "0x", "0x", 1024, 8, 1, 32 ],
|
|
|
|
hijackTag: 'hijacked scrypt: [{},{},1024,8,1,32,null]',
|
|
|
|
algorithm: scrypt
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "computeHmac",
|
|
|
|
params: [ "sha256", "0x", "0x" ],
|
|
|
|
hijackTag: 'hijacked computeHmac: ["sha256",{},{}]',
|
|
|
|
algorithm: computeHmac
|
|
|
|
},
|
2022-10-01 02:56:13 +03:00
|
|
|
{
|
|
|
|
name: "randomBytes",
|
|
|
|
params: [ 32 ],
|
|
|
|
hijackTag: "hijacked randomBytes: [32]",
|
|
|
|
algorithm: randomBytes,
|
|
|
|
postCheck: (value: any) => {
|
|
|
|
return (value instanceof Uint8Array && value.length === 32);
|
|
|
|
}
|
|
|
|
}
|
2022-09-05 23:14:43 +03:00
|
|
|
];
|
|
|
|
|
2022-10-01 02:56:13 +03:00
|
|
|
tests.forEach(({ name, params, hijackTag, algorithm, postCheck }) => {
|
2022-09-05 23:14:43 +03:00
|
|
|
it(`swaps in hijacked callback: ${ name }`, async function() {
|
|
|
|
const initial = await algorithm(...params);
|
|
|
|
|
|
|
|
algorithm.register(getHijack(name));
|
|
|
|
|
|
|
|
assert.equal(await algorithm(...params), "0x42");
|
|
|
|
assert.equal(hijack, hijackTag);
|
|
|
|
|
|
|
|
algorithm.register(algorithm._);
|
2022-10-01 02:56:13 +03:00
|
|
|
if (postCheck) {
|
|
|
|
assert.ok(postCheck(await algorithm(...params)));
|
|
|
|
} else {
|
|
|
|
assert.equal(await algorithm(...params), initial);
|
|
|
|
}
|
2022-09-05 23:14:43 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("prevents swapping after locked", function() {
|
|
|
|
lock();
|
|
|
|
|
|
|
|
tests.forEach(({ name, params, hijackTag, algorithm }) => {
|
|
|
|
assert.throws(function() {
|
|
|
|
algorithm.register(getHijack("test"));
|
|
|
|
}, function(error: any) {
|
|
|
|
return (error.message === `${ name } is locked`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|