Skip to content

Commit

Permalink
HTTP Signature検証のリファクタ2
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Nov 27, 2023
1 parent 229c516 commit f7c82eb
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/server/activitypub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,11 @@ async function inbox(ctx: Router.RouterContext) {
if (config.disableFederation) ctx.throw(404);

// parse body
const text = await coBody.text(ctx);

// check length
if (text.length > 65535) {
ctx.status = 413;
return;
}

// to json
const json = await JSON.parse(text);
ctx.request.body = json;
const { parsed, raw } = await coBody.json(ctx, {
limit: '64kb',
returnRawBody: true,
});
ctx.request.body = parsed;

let signature: httpSignature.IParsedSignature;

Expand Down Expand Up @@ -85,13 +79,13 @@ async function inbox(ctx: Router.RouterContext) {
const digestAlgo = match[1];
const digestExpected = match[2];

if (digestAlgo !== 'SHA-256') { // TODO: lc?
if (digestAlgo.toUpperCase() !== 'SHA-256') {
logger.warn(`inbox: unsupported algorithm`);
ctx.status = 401;
return;
}

const digestActual = crypto.createHash('sha256').update(text).digest('base64')
const digestActual = crypto.createHash('sha256').update(raw).digest('base64')

if (digestExpected !== digestActual) {
logger.warn(`inbox: digest missmatch`);
Expand Down

0 comments on commit f7c82eb

Please sign in to comment.