Repository navigation
fix: rate limiting behind proxies, error leaks and test type-checks, release 1.1.1 - #3
Merged
Merged
Conversation
Generated apps never set trust proxy, so behind a load balancer req.ip was the load balancer's address. Gatekeeper then counted every client together, and the whole service got 100 requests a minute. TRUST_PROXY takes a hop count or the proxies' addresses. It defaults to 0 and refuses true, which would let any client pick its own IP. CI now checks that two clients behind a proxy get separate limits.
Gatekeeper was added above /api/health, so a load balancer's health checks shared the limit with user traffic, and a busy minute could mark healthy servers as down. The health check now comes first and Gatekeeper right after it. CI checks that a client over the limit still gets 200 from the health check. The checks for missing rate limit headers now use if and exit, because a negated command in the middle of a script doesn't fail the step.
Unknown errors were already replaced with "Internal Server Error" in production, but an ApiError sent its message as is, even one marked non-operational, like ApiError.internal(). A message such as "Query failed on postgres://user:pass@..." went straight to the client. In production a non-operational ApiError now sends only the status text. Operational errors keep their message, and nothing changes outside production.
tsconfig.json left out tests/, and Vitest strips types without checking them, so a type error in a test passed both "npm run typecheck" and CI. tsconfig.json now covers src/ and tests/ for type-checking and editors, and the build uses tsconfig.build.json, which compiles only src/. The Dockerfile copies both.
PORT took any positive number, so 3000.5 or 99999 passed validation and failed later when the server tried to listen. It now has to be a whole number from 1 to 65535, and anything else stops the app at startup with the usual env error.
8 of 12 tasks
8 of 12 tasks
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.
Description
Five fixes to the generated project, mostly about what happens once it's deployed behind a load balancer, plus the 1.1.1 release.
Type of Change
Changes Made
TRUST_PROXY. Generated apps trusted no proxy, so behind a load balancerreq.ipwas the load balancer's address and Gatekeeper counted every client together. I sent 105 requests from 105 differentX-Forwarded-Foraddresses and the last 6 got 429.TRUST_PROXYtakes a hop count or the proxies' addresses, defaults to0and refusestrue.ApiError.internal('...')sent its message to the client. In production those now send only the status text.tsconfig.jsonnow coverstests/, and the build usestsconfig.build.json.PORThas to be a whole number from 1 to 65535.TRUST_PROXYand the 1.1.1 changelog entry.CI also changed: it boots with
TRUST_PROXY=1and checks that one client over the limit gets 429 while a second client still gets through, and that the first client still gets 200 from the health check. The checks for missing rate limit headers useif ... exit 1now, because a!command in the middle of the script never failed the step.Testing
npx tsx src/index.ts test-project)npm run dev)npm test)npm run lintwith no errorsEvery commit passes lint, typecheck, tests and the build. At every commit, a project generated with and without Gatekeeper passes typecheck, build, tests with coverage, lint, the format check and the boot checks from ci.yml.
The new proxy check fails on a project generated from main (the second client gets 429), and the new error test fails against the old error handler. Docker isn't installed on my machine, so the image build is left to CI. The builder stage's files build fine on their own.
Checklist