-
Notifications
You must be signed in to change notification settings - Fork 0
feat: superadmin powers, ownership, runtime settings, API keys, rate-limiting (auth PR B) #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * A tiny in-memory fixed-window rate limiter. Deployment is single-instance, so a | ||
| * process-local map is sufficient (a multi-instance deployment would need a | ||
| * shared store like Redis — a follow-up). Buckets expire lazily and the map is | ||
| * pruned when it grows, so spoofed keys can't leak memory unbounded. | ||
| */ | ||
| export interface RateLimiterOptions { | ||
| /** Window length in milliseconds. */ | ||
| windowMs: number; | ||
| /** Max hits allowed per key within a window. */ | ||
| max: number; | ||
| } | ||
|
|
||
| export interface RateLimiter { | ||
| /** Record a hit for `key`; returns false when the key is over its limit. */ | ||
| hit(key: string): boolean; | ||
| } | ||
|
|
||
| /** Cap on tracked keys — prune expired buckets before exceeding it. */ | ||
| const MAX_TRACKED_KEYS = 10_000; | ||
|
|
||
| /** Build a fixed-window {@link RateLimiter}. */ | ||
| export function createRateLimiter({ windowMs, max }: RateLimiterOptions): RateLimiter { | ||
| const buckets = new Map<string, { count: number; resetAt: number }>(); | ||
|
|
||
| function prune(now: number): void { | ||
| for (const [key, bucket] of buckets) { | ||
| if (bucket.resetAt <= now) buckets.delete(key); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| hit(key) { | ||
| const now = Date.now(); | ||
| let bucket = buckets.get(key); | ||
| if (!bucket || bucket.resetAt <= now) { | ||
| if (buckets.size >= MAX_TRACKED_KEYS) { | ||
| prune(now); | ||
| // If pruning freed nothing (all buckets live), refuse rather than grow | ||
| // the map without bound — fail closed under a many-distinct-IP flood. | ||
| if (buckets.size >= MAX_TRACKED_KEYS) return false; | ||
| } | ||
| bucket = { count: 0, resetAt: now + windowMs }; | ||
| buckets.set(key, bucket); | ||
| } | ||
| bucket.count += 1; | ||
| return bucket.count <= max; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * The slice of Bun's `Server` we need — structural so we don't depend on the | ||
| * generic `Server<WebSocketData>` shape Elysia hands us. | ||
| */ | ||
| interface IpResolver { | ||
| requestIP(request: Request): { address: string } | null; | ||
| } | ||
|
|
||
| /** | ||
| * Client IP for rate-limit keying. The socket address is authoritative and | ||
| * un-spoofable; `X-Forwarded-For` is only consulted when `trustProxy` is set, | ||
| * because XFF is client-supplied and would otherwise let an attacker rotate it to | ||
| * bypass the auth throttles. Enable `trustProxy` only behind a reverse proxy that | ||
| * overwrites the header (see `TRUST_PROXY`). | ||
| */ | ||
| export function clientIp(request: Request, server: IpResolver | null, trustProxy: boolean): string { | ||
| if (trustProxy) { | ||
| const forwarded = request.headers.get("x-forwarded-for"); | ||
| const first = forwarded?.split(",")[0]?.trim(); | ||
| if (first) return first; | ||
| } | ||
| return server?.requestIP(request)?.address ?? "unknown"; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.