( email: string, category: string, token: string, )
| 12 | } |
| 13 | |
| 14 | export function verifyUnsubscribeToken( |
| 15 | email: string, |
| 16 | category: string, |
| 17 | token: string, |
| 18 | ): boolean { |
| 19 | const expectedToken = generateUnsubscribeToken(email, category); |
| 20 | const tokenBuffer = Buffer.from(token, 'hex'); |
| 21 | const expectedBuffer = Buffer.from(expectedToken, 'hex'); |
| 22 | |
| 23 | // Handle length mismatch safely to avoid timing leaks |
| 24 | if (tokenBuffer.length !== expectedBuffer.length) { |
| 25 | // Compare against zero-filled buffer of same length as token to maintain constant time |
| 26 | const zeroBuffer = Buffer.alloc(tokenBuffer.length); |
| 27 | timingSafeEqual(tokenBuffer, zeroBuffer); |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | return timingSafeEqual(tokenBuffer, expectedBuffer); |
| 32 | } |
| 33 | |
| 34 | export function getUnsubscribeUrl(email: string, category: string): string { |
| 35 | const token = generateUnsubscribeToken(email, category); |
no test coverage detected