chore: format

This commit is contained in:
ThatOneCalculator 2023-11-26 12:06:21 -08:00
parent 3272b908c6
commit 2f7b3070ee
No known key found for this signature in database
GPG Key ID: 8703CACD01000000
27 changed files with 206 additions and 185 deletions

View File

@ -116,7 +116,9 @@ export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
} }
if (httpSignatureValidated) { if (httpSignatureValidated) {
if (!verifySignature(signature, authUser.key)) return `skip: Invalid HTTP signature`; if (!verifySignature(signature, authUser.key)) {
return "skip: Invalid HTTP signature";
}
} }
// また、signatureのsignerは、activity.actorと一致する必要がある // また、signatureのsignerは、activity.actorと一致する必要がある

View File

@ -36,7 +36,9 @@ export async function checkFetch(req: IncomingMessage): Promise<number> {
let signature; let signature;
try { try {
signature = httpSignature.parseRequest(req, { headers: ["(request-target)", "host", "date"] }); signature = httpSignature.parseRequest(req, {
headers: ["(request-target)", "host", "date"],
});
} catch (e) { } catch (e) {
return 401; return 401;
} }
@ -138,21 +140,38 @@ export async function getSignatureUser(req: IncomingMessage): Promise<{
return await dbResolver.getAuthUserFromApId(getApId(keyId.toString())); return await dbResolver.getAuthUserFromApId(getApId(keyId.toString()));
} }
export function verifySignature(sig: IParsedSignature, key: UserPublickey): boolean { export function verifySignature(
if (!['hs2019', 'rsa-sha256'].includes(sig.algorithm.toLowerCase())) return false; sig: IParsedSignature,
key: UserPublickey,
): boolean {
if (!["hs2019", "rsa-sha256"].includes(sig.algorithm.toLowerCase()))
return false;
try { try {
return verify('rsa-sha256', Buffer.from(sig.signingString, 'utf8'), key.keyPem, Buffer.from(sig.params.signature, 'base64')); return verify(
} "rsa-sha256",
catch { Buffer.from(sig.signingString, "utf8"),
key.keyPem,
Buffer.from(sig.params.signature, "base64"),
);
} catch {
// Algo not supported // Algo not supported
return false; return false;
} }
} }
export function verifyDigest(body: string, digest: string | string[] | undefined): boolean { export function verifyDigest(
body: string,
digest: string | string[] | undefined,
): boolean {
digest = toSingle(digest); digest = toSingle(digest);
if (body == null || digest == null || !digest.toLowerCase().startsWith('sha-256=')) if (
body == null ||
digest == null ||
!digest.toLowerCase().startsWith("sha-256=")
)
return false; return false;
return createHash('sha256').update(body).digest('base64') === digest.substring(8); return (
createHash("sha256").update(body).digest("base64") === digest.substring(8)
);
} }

View File

@ -50,7 +50,9 @@ function inbox(ctx: Router.RouterContext) {
let signature; let signature;
try { try {
signature = httpSignature.parseRequest(ctx.req, { headers: ['(request-target)', 'digest', 'host', 'date'] }); signature = httpSignature.parseRequest(ctx.req, {
headers: ["(request-target)", "digest", "host", "date"],
});
} catch (e) { } catch (e) {
ctx.status = 401; ctx.status = 401;
return; return;
@ -93,8 +95,7 @@ async function parseJsonBodyOrFail(ctx: Router.RouterContext, next: Koa.Next) {
try { try {
await koaBodyParser(ctx, next); await koaBodyParser(ctx, next);
} } catch {
catch {
ctx.status = 400; ctx.status = 400;
return; return;
} }

View File

@ -25,8 +25,7 @@ const assets = `${_dirname}/../../server/file/assets/`;
const MAX_BYTE_RANGES = 10; const MAX_BYTE_RANGES = 10;
const commonReadableHandlerGenerator = const commonReadableHandlerGenerator =
(ctx: Koa.Context) => (ctx: Koa.Context) => (e: Error): void => {
(e: Error): void => {
serverLogger.error(e); serverLogger.error(e);
ctx.status = 500; ctx.status = 500;
ctx.set("Cache-Control", "max-age=300"); ctx.set("Cache-Control", "max-age=300");