update match to return false for unsupported, null, empty, or invalid hash algorithms

This commit is contained in:
Nebel 2024-09-18 16:14:32 +09:00
parent f8aff38a6b
commit 47228186a0
Signed by: nebel
GPG key ID: 79807D08C6EF6460
2 changed files with 9 additions and 5 deletions

View file

@ -70,7 +70,11 @@ export class IntegrityMetadata implements IntegrityMetadataLike {
}); });
} }
match({ alg, val }: IntegrityMetadataLike): boolean { match(integrity: IntegrityMetadataLike | string | null | undefined): boolean {
const { alg, val } = new IntegrityMetadata(integrity);
if (!alg) return false;
if (!val) return false;
if (!(alg in supportedHashAlgorithms)) return false;
return alg === this.alg && val === this.val; return alg === this.alg && val === this.val;
} }

View file

@ -44,7 +44,7 @@ test("if the hash values are different, return false", function () {
); );
}); });
test.todo("if the hash algorithm is unsupported, return false", function () { test("if the hash algorithm is unsupported, return false", function () {
const integrityMetadata = new IntegrityMetadata( const integrityMetadata = new IntegrityMetadata(
"sha1-lDpwLQbzRZmu4fjajvn3KWAx1pk=", "sha1-lDpwLQbzRZmu4fjajvn3KWAx1pk=",
); );
@ -57,7 +57,7 @@ test.todo("if the hash algorithm is unsupported, return false", function () {
); );
}); });
test.todo("if null, return false", function () { test("if null, return false", function () {
const integrityMetadata = new IntegrityMetadata(null); const integrityMetadata = new IntegrityMetadata(null);
assert.strictEqual( assert.strictEqual(
@ -66,13 +66,13 @@ test.todo("if null, return false", function () {
); );
}); });
test.todo("if empty, return false", function () { test("if empty, return false", function () {
const integrityMetadata = new IntegrityMetadata(""); const integrityMetadata = new IntegrityMetadata("");
assert.strictEqual(integrityMetadata.match(new IntegrityMetadata("")), false); assert.strictEqual(integrityMetadata.match(new IntegrityMetadata("")), false);
}); });
test.todo("if invalid value, return false", function () { test("if invalid value, return false", function () {
const integrityMetadata = new IntegrityMetadata("md5\0/..invalid-value"); const integrityMetadata = new IntegrityMetadata("md5\0/..invalid-value");
assert.strictEqual( assert.strictEqual(