Skip to content

Commit d931d52

Browse files
committed
0.0.0-alpha.9
1 parent 4a8d584 commit d931d52

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

dist/index.cjs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ __export(src_exports, {
3838
SignatureHeaderClockInvalidError: () => SignatureHeaderClockInvalidError,
3939
SignatureHeaderContentLackedError: () => SignatureHeaderContentLackedError,
4040
SignatureHeaderNotFoundError: () => SignatureHeaderNotFoundError,
41-
SignatureMissmatchWithProvidedAlgorithmError: () => SignatureMissmatchWithProvidedAlgorithmError,
4241
checkClockSkew: () => checkClockSkew,
4342
detectAndVerifyAlgorithm: () => detectAndVerifyAlgorithm,
4443
digestHeaderRegEx: () => digestHeaderRegEx,
@@ -578,18 +577,18 @@ function verifyDigestHeader(request, rawBody, failOnNoDigest = true, errorLogger
578577
}
579578

580579
// src/shared/verify.ts
581-
var SignatureMissmatchWithProvidedAlgorithmError = class extends Error {
582-
constructor(providedAlgorithm, detectedAlgorithm, realKeyType) {
583-
super(`Provided algorithm does not match the public key type: provided=${detectedAlgorithm}(${providedAlgorithm}}, real=${realKeyType}`);
584-
}
585-
};
586-
function detectAndVerifyAlgorithm(algorithm, publicKey) {
580+
function buildErrorMessage(providedAlgorithm, detectedAlgorithm, realKeyType) {
581+
return `Provided algorithm does not match the public key type: provided=${detectedAlgorithm}(${providedAlgorithm}}, real=${realKeyType}`;
582+
}
583+
function detectAndVerifyAlgorithm(algorithm, publicKey, errorLogger) {
587584
algorithm = algorithm?.toLowerCase();
588585
const realKeyType = publicKey.asymmetricKeyType;
589586
if (algorithm && algorithm !== "hs2019" && realKeyType) {
590587
const providedKeyAlgorithm = algorithm.split("-")[0];
591588
if (providedKeyAlgorithm !== realKeyType.toLowerCase() && !(providedKeyAlgorithm === "ecdsa" && realKeyType === "ec")) {
592-
throw new SignatureMissmatchWithProvidedAlgorithmError(algorithm, providedKeyAlgorithm, realKeyType);
589+
if (errorLogger)
590+
errorLogger(buildErrorMessage(providedKeyAlgorithm, realKeyType, realKeyType));
591+
return null;
593592
}
594593
}
595594
if (algorithm === "ed25519" || algorithm === "ed25519-sha512" || realKeyType === "ed25519") {
@@ -622,7 +621,9 @@ function detectAndVerifyAlgorithm(algorithm, publicKey) {
622621
hashAlg: algoSplitted.length === 1 ? null : algoSplitted[algoSplitted.length - 1]
623622
};
624623
}
625-
throw new Error("Algorithm not found");
624+
if (errorLogger)
625+
errorLogger("Algorithm is not detected");
626+
return null;
626627
}
627628

628629
// src/draft/verify.ts
@@ -631,6 +632,8 @@ function verifyDraftSignature(parsed, publicKeyPem, errorLogger) {
631632
const publicKey = crypto4.createPublicKey(publicKeyPem);
632633
try {
633634
const detected = detectAndVerifyAlgorithm(parsed.params.algorithm, publicKey);
635+
if (!detected)
636+
return false;
634637
return crypto4.verify(detected.hashAlg, Buffer.from(parsed.signingString), publicKey, Buffer.from(parsed.params.signature, "base64"));
635638
} catch (e) {
636639
if (errorLogger)
@@ -648,7 +651,6 @@ function verifyDraftSignature(parsed, publicKeyPem, errorLogger) {
648651
SignatureHeaderClockInvalidError,
649652
SignatureHeaderContentLackedError,
650653
SignatureHeaderNotFoundError,
651-
SignatureMissmatchWithProvidedAlgorithmError,
652654
checkClockSkew,
653655
detectAndVerifyAlgorithm,
654656
digestHeaderRegEx,

dist/index.mjs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -505,18 +505,18 @@ function verifyDigestHeader(request, rawBody, failOnNoDigest = true, errorLogger
505505
}
506506

507507
// src/shared/verify.ts
508-
var SignatureMissmatchWithProvidedAlgorithmError = class extends Error {
509-
constructor(providedAlgorithm, detectedAlgorithm, realKeyType) {
510-
super(`Provided algorithm does not match the public key type: provided=${detectedAlgorithm}(${providedAlgorithm}}, real=${realKeyType}`);
511-
}
512-
};
513-
function detectAndVerifyAlgorithm(algorithm, publicKey) {
508+
function buildErrorMessage(providedAlgorithm, detectedAlgorithm, realKeyType) {
509+
return `Provided algorithm does not match the public key type: provided=${detectedAlgorithm}(${providedAlgorithm}}, real=${realKeyType}`;
510+
}
511+
function detectAndVerifyAlgorithm(algorithm, publicKey, errorLogger) {
514512
algorithm = algorithm?.toLowerCase();
515513
const realKeyType = publicKey.asymmetricKeyType;
516514
if (algorithm && algorithm !== "hs2019" && realKeyType) {
517515
const providedKeyAlgorithm = algorithm.split("-")[0];
518516
if (providedKeyAlgorithm !== realKeyType.toLowerCase() && !(providedKeyAlgorithm === "ecdsa" && realKeyType === "ec")) {
519-
throw new SignatureMissmatchWithProvidedAlgorithmError(algorithm, providedKeyAlgorithm, realKeyType);
517+
if (errorLogger)
518+
errorLogger(buildErrorMessage(providedKeyAlgorithm, realKeyType, realKeyType));
519+
return null;
520520
}
521521
}
522522
if (algorithm === "ed25519" || algorithm === "ed25519-sha512" || realKeyType === "ed25519") {
@@ -549,7 +549,9 @@ function detectAndVerifyAlgorithm(algorithm, publicKey) {
549549
hashAlg: algoSplitted.length === 1 ? null : algoSplitted[algoSplitted.length - 1]
550550
};
551551
}
552-
throw new Error("Algorithm not found");
552+
if (errorLogger)
553+
errorLogger("Algorithm is not detected");
554+
return null;
553555
}
554556

555557
// src/draft/verify.ts
@@ -558,6 +560,8 @@ function verifyDraftSignature(parsed, publicKeyPem, errorLogger) {
558560
const publicKey = crypto4.createPublicKey(publicKeyPem);
559561
try {
560562
const detected = detectAndVerifyAlgorithm(parsed.params.algorithm, publicKey);
563+
if (!detected)
564+
return false;
561565
return crypto4.verify(detected.hashAlg, Buffer.from(parsed.signingString), publicKey, Buffer.from(parsed.params.signature, "base64"));
562566
} catch (e) {
563567
if (errorLogger)
@@ -574,7 +578,6 @@ export {
574578
SignatureHeaderClockInvalidError,
575579
SignatureHeaderContentLackedError,
576580
SignatureHeaderNotFoundError,
577-
SignatureMissmatchWithProvidedAlgorithmError,
578581
checkClockSkew,
579582
detectAndVerifyAlgorithm,
580583
digestHeaderRegEx,

dist/shared/verify.d.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
/// <reference types="node" />
22
import * as crypto from 'node:crypto';
33
import type { SignatureHashAlgorithm } from '../types.js';
4-
export declare class SignatureMissmatchWithProvidedAlgorithmError extends Error {
5-
constructor(providedAlgorithm: string, detectedAlgorithm: string, realKeyType: string);
6-
}
74
/**
85
* ヘッダーのアルゴリズムから鍵とハッシュアルゴリズムを認識する
96
* 提供されたアルゴリズムと呼び出しの公開鍵の種類が一致しない場合はエラーを投げる
107
* @param algorithm ヘッダーのアルゴリズム
11-
* @param key 実際の公開鍵
8+
* @param publicKey 実際の公開鍵
129
*/
13-
export declare function detectAndVerifyAlgorithm(algorithm: string | undefined, publicKey: crypto.KeyObject): {
10+
export declare function detectAndVerifyAlgorithm(algorithm: string | undefined, publicKey: crypto.KeyObject, errorLogger?: ((message: any) => any)): {
1411
keyAlg: crypto.KeyType;
1512
hashAlg: SignatureHashAlgorithm | null;
16-
};
13+
} | null;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@misskey-dev/node-http-message-signatures",
3-
"version": "0.0.0-alpha.8",
3+
"version": "0.0.0-alpha.9",
44
"description": "",
55
"type": "module",
66
"keywords": [

0 commit comments

Comments
 (0)