Skip to content

BE Auth Service Crypto package#1

Open
KailasMahavarkar wants to merge 1 commit intomainfrom
f-BE-48-auth-service-crypto-package
Open

BE Auth Service Crypto package#1
KailasMahavarkar wants to merge 1 commit intomainfrom
f-BE-48-auth-service-crypto-package

Conversation

@KailasMahavarkar
Copy link
Copy Markdown
Contributor

  • added crypto package

Copy link
Copy Markdown

@orkait-anupamsingh2004 orkait-anupamsingh2004 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviews

const { hash } = ALGORITHM_MAP[this.algorithm];

// Parse payload to get iat for issuedBefore checks
// This is a non-crypto operation, safe to do before verification
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The verifySignature method parses the payload JSON before verifying the cryptographic signature, but parsing untrusted, unverified JSON is a security anti-pattern in crypto libraries. A crafted payload could exploit JSON parser quirks. Consider restructuring to verify the signature first, then parse claims..This is not a critical vulnerability but it is and crypto anitpattern

}

// Import ERROR_MESSAGES for validateExpiration
import { ERROR_MESSAGES } from './constants';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would moving this import at the top be more conventional method of importing in typescript

onJti?: (jti: string) => boolean | Promise<boolean>;
}

export type JWTVerifyResult<T> = { valid: true; payload: T } | { valid: false; error: string }; No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JWTVerifyResult only has { valid: false; error: string } a plain string error. But JWKSVerifyResult has { valid: false; error: string; code: JWKSErrorCode } ....a more structured error code enum. This is an inconsistent API surface within the same package, Consumers using JWTService cannot programmatically handle error cases without parsing strings

adding an error code enum to JWTVerifyResult would be better for parity with the JWKS service

.setProtectedHeader({
alg: this.config.algorithm,
typ: JWT_HEADER.TYPE,
kid: this.primaryKid!,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oth sign methods use this.primaryKid! (non-null assertion) without a guard-->
While the constructor always sets this through fromPEM/fromKMS, the field is typed as string | null. If someone extends or modifies the class, this will throw a runtime error deep in JWT signing. Adding an explicit check or change the type to be non-nullable after construction

};
}

export interface VaultConfig {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VaultConfig declares token?: string (optional), but VaultKMSProvider's constructor immediately throws if token is missing (falling back to process.env.VAULT_TOKEN). The type signature is misleading....it suggests you can create a VaultConfig without a token, but that only works if the env var is set

Either documenting this in the type or making the type more explicit would be better

}

const { payload, protectedHeader } = successfulResult;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maxPayloadSize check happens after full signature verification (jwtVerify has already parsed and verified)...By this point, the expensive crypto verification is already done. If DoS prevention is the goal, consider checking raw token segment sizes before calling jwtVerify

@orkait-anupamsingh2004
Copy link
Copy Markdown

reviewed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants