🔐 feat(auth): add CloudFront signed cookie support#12235
Open
AtefBellaaj wants to merge 1 commit intofeat/cloudfront-cdn-strategy-phase1from
Open
🔐 feat(auth): add CloudFront signed cookie support#12235AtefBellaaj wants to merge 1 commit intofeat/cloudfront-cdn-strategy-phase1from
AtefBellaaj wants to merge 1 commit intofeat/cloudfront-cdn-strategy-phase1from
Conversation
2294f32 to
198cc5d
Compare
- Integrate CloudFront signed cookies into authentication flow for secure CDN access.
- Set cookies on login (setAuthTokens, setOpenIDAuthTokens) when imageSigning="cookies".
- Clear cookies on logout (clearCloudFrontCookies) to prevent post-logout CDN access.
- Add cookieDomain and cookieExpiry config options with validation requiring shared parent domain.
- Tighten schema: cookieDomain requires min(1), leading-dot validation, cookieExpiry capped at 604800s (7 days).
- Return false from initializeCloudFront when imageSigning="cookies" but signing keys are absent.
- Refactor cloudfront-cookies.ts: single config read, remove isCloudFrontCookiesEnabled export,
add DEFAULT_COOKIE_EXPIRY fallback const for robustness against missing Zod defaults.
- Move cloudfront-cookies.ts from auth/ to cdn/ layer; cookies are CDN access grants, not auth tokens.
- Fix clearCloudFrontCookies: add httpOnly/secure/sameSite security attributes and try-catch error guard.
- Add REQUIRED_CF_COOKIES validation: assert all 3 cookies present before setting any; log error on missing key.
- Scope CloudFront policy resource and cookie path to /images (was /* and /); limits CDN grant to image content.
- Fix trailing-slash regex to /\/+$/ to handle multiple slashes; upgrade signing failure log to error level.
- Clarify urlExpiry in librechat.example.yaml as reserved for future signed-URL mode.
198cc5d to
85ab1b1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds CloudFront signed cookie support for secure CDN image delivery. When
imageSigning: "cookies"is configured, authentication endpoints automatically set CloudFront signed cookies, allowing browsers to access protected CDN images without per-URL signatures. Cookies are scoped to/images/*and cleared on logout to prevent post-logout CDN access.This is part of the CloudFront CDN file strategy implementation. Signed cookies are set during login (
setAuthTokens,setOpenIDAuthTokens) and cleared on logout. They require a shared parent domain between the API and CloudFront distribution (e.g., API atapi.example.com, CDN atcdn.example.com, withcookieDomain: ".example.com").Key changes:
setCloudFrontCookiesandclearCloudFrontCookiesinpackages/api/src/cdn/cloudfront-cookies.ts/images/*(not wildcard) since only images are served via CloudFront cookiespath: '/images'so browsers only send cookies for image requestssecure,httpOnly,sameSite: 'none') on both set and clear operationsclearCloudFrontCookieson logout to revoke CDN access (with internal error handling)cookieDomain(required for cookies mode, must start with.) andcookieExpiryconfig optionscookieDomainvalidated to start with.,cookieExpirycapped at604800s(7 days)cookieDomainis set whenimageSigning: "cookies"falsefrominitializeCloudFrontwhenimageSigning="cookies"but signing keys are absentDEFAULT_COOKIE_EXPIRYfallback const for robustness when Zod defaults are not applied to raw YAML configurlExpiryinlibrechat.example.yamlas reserved for future signed-URL modeChange Type
Testing
imageSigning: "cookies"and required env vars (CLOUDFRONT_KEY_PAIR_ID,CLOUDFRONT_PRIVATE_KEY)cookieDomainto shared parent domain (must start with., e.g..example.com)CloudFront-Policy,CloudFront-Signature, andCloudFront-Key-Pair-Idcookies are set withpath=/images/images/*succeed with cookiesUnit tests added for:
setCloudFrontCookiescalled during both auth flowssetCloudFrontCookiesreturnsfalsewhen AWS SDK returns empty or partial cookiessetCloudFrontCookiessets cookies with correct security attributes and pathclearCloudFrontCookiescalled on logout with matching security attributesclearCloudFrontCookieshandles errors gracefully without failing logoutcookieDomainnot starting with.cookieDomainwhen cookies enabledinitializeCloudFrontreturnsfalsewhen cookies mode lacks signing keyssetCloudFrontCookieshandles missingcookieExpirygracefully viaDEFAULT_COOKIE_EXPIRYChecklist