fix(core,express): forward serviceAuthorization from getSeamlessUser#107
Merged
Conversation
GetSeamlessUserOptions did not declare serviceAuthorization and core's authFetch call never passed it. The Express adapter still computed the service token and passed it, but an `as GetSeamlessUserOptions` cast on the option literal dropped it with no type error. Every getSeamlessUser call sent x-seamless-client-ip with no service token, and the auth server ignores the forwarded IP unless a valid service token accompanies it, so rate limiting, lockout, and anomaly detection saw the adapter's egress IP instead of the end user's. Add serviceAuthorization to GetSeamlessUserOptions and forward it in the authFetch call. Remove the cast in the adapter and make authorization optional, which is what the adapter already passed: the user token is resolved from req.cookiePayload or req.user, both unset when the function is called outside the auth router or requireAuth guard. The required type was only satisfied by the same cast that hid the dropped service token. Add getSeamlessUser coverage to both packages, asserting the upstream request carries both the service token and the client IP header and that the resolved user shape is returned. Neither suite exercised the function before, which is why the regression shipped. Closes #99
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.
Closes #99.
Regression
getSeamlessUsersilently droppedserviceAuthorization, so the client IP it forwards was ignored by the auth server. This re-broke what #90 fixed.GetSeamlessUserOptions(added in #92) did not declareserviceAuthorization, and core'sauthFetchcall never passed it. The Express adapter still computed the service token and passed it, but anas GetSeamlessUserOptionscast on the option literal discarded it with no type error. EverygetSeamlessUsercall therefore sentx-seamless-client-ipwith no accompanying service token, and the auth server ignores the forwarded IP unless a valid service token rides with it (confirmed in the API atsrc/middleware/trustedClientIp.ts:53-58, which callsnext()without applying the IP when the service-token header is absent). Rate limiting, lockout, and anomaly detection attributed those requests to the adapter's egress IP instead of the end user's.Fix
serviceAuthorization?: stringtoGetSeamlessUserOptionsand forward it in theauthFetchcall.as GetSeamlessUserOptionscast in the adapter.GetSeamlessUserOptions.authorizationoptional. That is what the adapter already passed: it resolves the user token fromreq.cookiePayloadorreq.user, both unset whengetSeamlessUseris called outside the auth router or therequireAuthguard. The required type was only satisfiable by the same cast that hid the dropped service token, so removing the cast surfaced this.Tests
Neither package exercised
getSeamlessUserbefore, which is why the regression shipped. This adds coverage to both, mockingglobal.fetchand importing from builtdist/:packages/core/tests/getSeamlessUser.test.js): the upstream request carries the service token and the client IP together; returns the canonical user; honors a custom cookie name; omits the service header when none is supplied; returns null (without hitting the network) for a missing cookie, a cookie signed by another secret, or an upstream rejection; rejects a weak secret.packages/express/tests/getSeamlessUser.test.js): the minted service token is HS256 with theiss/aud/subthe API requires; the client IP is forwarded alongside it; the user token stays inAuthorizationonly; a custom access cookie name, blankettrust proxy, and a caller-supplied resolver behave correctly; the service token is omitted when noserviceSecretis configured.I verified both suites fail with the core forward reverted (core: 1 failure; express: 3 failures on the header assertions), so they genuinely guard the regression rather than passing regardless.
Cast audit
I audited the ~38 other option-object casts (
as any/as X) acrosscreateServer.ts,middleware/ensureCookies.ts, andhandlers/*against their target interfaces. None currently drops a live option the way this one did (the only excess property anywhere is an inertforwardedClientIp: undefinedthat the middleware recomputes per-request). They are the same construct that hid this bug and worth removing, but the change is a ~38-site refactor with no behavior change, so I kept this PR focused on the regression and tests and filed the cleanup as #106.Verification
pnpm buildclean.pnpm test: core 80 passed, express 111 passed.Adds a changeset (patch, core and express).