Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
fix middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
gquadrati committed Mar 13, 2023
1 parent 776972e commit 031df65
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const constants = {
* @returns {string} contentDigest The 'Content-Digest' header value.
*/
export const generateDigestHeader = (
payload: Buffer,
payload: Buffer | string,
cipher: string
): string => {
// Validate the input payload
Expand Down Expand Up @@ -52,7 +52,7 @@ export const generateDigestHeader = (
*/
export const validateDigestHeader = (
contentDigestHeader: string,
body: Buffer
body: Buffer | string
): void => {
if (!contentDigestHeader) {
throw new Error("Content-Digest header missing");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("HttpMessageSignatureMiddleware", () => {
app: {
get: () => ({
bindings: {
req: { rawBody: Buffer.from(JSON.stringify(aValidPayload)) }
req: { rawBody: JSON.stringify(aValidPayload) }
}
})
},
Expand All @@ -40,7 +40,7 @@ describe("HttpMessageSignatureMiddleware", () => {
app: {
get: () => ({
bindings: {
req: { rawBody: Buffer.from(JSON.stringify(aValidPayload)) }
req: { rawBody: JSON.stringify(aValidPayload) }
}
})
},
Expand Down
31 changes: 20 additions & 11 deletions utils/middleware/http_message_signature_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,33 @@ export const LollipopHeadersForSignature = t.intersection([
]);

export const isValidDigestHeader = (
contentDigestHeader: string,
body: Buffer
contentDigestHeader: string | undefined,
body: Buffer | string
): boolean =>
pipe(
E.tryCatch(
() => crypto.validateDigestHeader(contentDigestHeader, body),
E.toError
contentDigestHeader,
E.fromNullable(new Error("Missing 'content-digest' header")),
E.chain(contentDigest =>
E.tryCatch(
() => crypto.validateDigestHeader(contentDigest, body),
E.toError
)
),
E.fold(constFalse, constTrue)
);

export const validateHttpSignature = (
request: express.Request,
assertionRef: AssertionRef,
publicKey: JwkPublicKey
publicKey: JwkPublicKey,
body?: string
): TE.TaskEither<Error, true> =>
pipe(
{
httpHeaders: request.headers,
url: request.url,
method: request.method,
body: request.body,
body,
verifier: {
keyMap: {
[assertionRef]: {
Expand Down Expand Up @@ -124,13 +129,16 @@ export const HttpMessageSignatureMiddleware = (): IRequestMiddleware<
),
E.filterOrElseW(
({ rawBody, lollipopHeaders }) =>
lollipopHeaders["content-digest"]
rawBody || lollipopHeaders["content-digest"]
? isValidDigestHeader(lollipopHeaders["content-digest"], rawBody)
: true,
() => ResponseErrorInternal("The body do not match the content digest")
() =>
ResponseErrorInternal(
"The content-digest is empty or do not match the body"
)
),
TE.fromEither,
TE.chainW(({ lollipopHeaders }) =>
TE.chainW(({ rawBody, lollipopHeaders }) =>
pipe(
lollipopHeaders["x-pagopa-lollipop-public-key"],
JwkPublicKeyFromToken.decode,
Expand All @@ -140,7 +148,8 @@ export const HttpMessageSignatureMiddleware = (): IRequestMiddleware<
validateHttpSignature(
request,
lollipopHeaders["x-pagopa-lollipop-assertion-ref"],
key
key,
rawBody
)
),
TE.mapLeft(error =>
Expand Down

0 comments on commit 031df65

Please sign in to comment.