| 1 | export const MIN_SECRET_CHARS = 32; |
| 2 | export const MAX_SECRET_CHARS = 512; |
| 3 | export const MIN_SECRET_DISTINCT_CHARS = 8; |
| 4 | |
| 5 | const LONE_SURROGATE = /\p{Cs}/u; |
| 6 | |
| 7 | export const MAINTAINER_TOKEN_UNAVAILABLE = |
| 8 | `Maintainer sign-in is unavailable: MAINTAINER_TOKEN must be set to a secret of at least ${MIN_SECRET_CHARS} characters ` + |
| 9 | `(at most ${MAX_SECRET_CHARS}, with at least ${MIN_SECRET_DISTINCT_CHARS} distinct characters; a shorter value repeated is refused).`; |
| 10 | |
| 11 | export const CRON_SECRET_UNAVAILABLE = |
| 12 | `manual trigger disabled: CRON_SECRET must be a secret of at least ${MIN_SECRET_CHARS} characters ` + |
| 13 | `(at most ${MAX_SECRET_CHARS}, with at least ${MIN_SECRET_DISTINCT_CHARS} distinct characters; a shorter value repeated is refused)`; |
| 14 | |
| 15 | export function secretStrongEnough(value: string | undefined): value is string { |
| 16 | if (typeof value !== "string" || value.length > MAX_SECRET_CHARS || LONE_SURROGATE.test(value)) return false; |
| 17 | const chars = [...value]; |
| 18 | if (chars.length < MIN_SECRET_CHARS || new Set(chars).size < MIN_SECRET_DISTINCT_CHARS) return false; |
| 19 | for (let period = 1; period <= value.length / 2; period++) { |
| 20 | if (value.slice(period) === value.slice(0, value.length - period)) return false; |
| 21 | } |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | export const maintainerTokenUsable = secretStrongEnough; |
| 26 |