Skip to content

Commit

Permalink
fix: use raw body for verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Dec 5, 2023
1 parent 6e3c9df commit 4539594
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export class BaseSlashCreator extends (EventEmitter as any as new () => TypedEve
const signature = treq.headers['x-signature-ed25519'] as string;
const timestamp = treq.headers['x-signature-timestamp'] as string;

const verified = await this._verify(JSON.stringify(treq.body), signature, timestamp);
const verified = await this._verify(treq.rawBody || JSON.stringify(treq.body), signature, timestamp);

if (!verified) {
this.emit('debug', 'A request failed to be verified');
Expand Down
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export interface TransformedRequest {
request: any;
/** The response class from a Server, depending on what server it is. */
response: any;
/** The raw string of the body. */
rawBody?: string;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/servers/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export class BunServer extends Server {
headers: Object.fromEntries(request.headers.entries()),
body: body ? JSON.parse(body) : body,
request,
response: null
response: null,
rawBody: body
},
async (response) => {
if (response.files) {
Expand Down
3 changes: 2 additions & 1 deletion src/servers/cfworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export class CloudflareWorkerServer extends Server {
headers: Object.fromEntries(request.headers.entries()),
body: body ? JSON.parse(body) : body,
request,
response: null
response: null,
rawBody: body
},
async (response) => {
if (response.files) {
Expand Down
54 changes: 34 additions & 20 deletions src/servers/fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,41 @@ export class FastifyServer extends Server {

/** @private */
createEndpoint(path: string, handler: ServerRequestHandler) {
this.app.post(path, (req: any, res: any) =>
handler(
{
headers: req.headers,
body: req.body,
request: req,
response: res
},
async (response) => {
res.status(response.status || 200);
if (response.headers) res.headers(response.headers);
if (response.files) {
const data = new MultipartData();
res.header('Content-Type', 'multipart/form-data; boundary=' + data.boundary);
for (const i in response.files) data.attach(`files[${i}]`, response.files[i].file, response.files[i].name);
data.attach('payload_json', JSON.stringify(response.body));
res.send(Buffer.concat(data.finish()));
} else res.send(response.body);
this.app.register(async (app: any) => {
// Capture and set the raw payload with a scoped parser
app.addContentTypeParser(
'application/json',
{ parseAs: 'string', asString: true },
(request: any, payload: any, done: any) => {
request.rawBody = payload;
app.getDefaultJsonParser('remove', 'remove')(request, payload, done);
}
)
);
);

app.post(path, (req: any, res: any) =>
handler(
{
headers: req.headers,
body: req.body,
request: req,
response: res,
rawBody: req.rawBody
},
async (response) => {
res.status(response.status || 200);
if (response.headers) res.headers(response.headers);
if (response.files) {
const data = new MultipartData();
res.header('Content-Type', 'multipart/form-data; boundary=' + data.boundary);
for (const i in response.files)
data.attach(`files[${i}]`, response.files[i].file, response.files[i].name);
data.attach('payload_json', JSON.stringify(response.body));
res.send(Buffer.concat(data.finish()));
} else res.send(response.body);
}
)
);
});
}

/** @private */
Expand Down
3 changes: 2 additions & 1 deletion src/servers/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export class AWSLambdaServer extends Server {
headers: splitHeaders(event.headers),
body: event.body ? JSON.parse(event.body) : {},
request: event,
response: callback
response: callback,
rawBody: event.body ?? ''
},
async (response) => {
const responseHeaders = joinHeaders(response.headers);
Expand Down

0 comments on commit 4539594

Please sign in to comment.