Skip to content
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

Get rate limit ip correctly #1577

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/pds/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class PDS {
)

const app = express()
app.set('trust proxy', true)
app.use(cors())
app.use(loggerMiddleware)
app.use(compression())
Expand Down
2 changes: 1 addition & 1 deletion packages/xrpc-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export * from './stream'
export * from './rate-limiter'

export type { ServerTiming } from './util'
export { getReqIp, serverTimingHeader, ServerTimer } from './util'
export { serverTimingHeader, ServerTimer } from './util'
3 changes: 1 addition & 2 deletions packages/xrpc-server/src/rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
RateLimiterStatus,
XRPCReqContext,
} from './types'
import { getReqIp } from './util'

export type RateLimiterOpts = {
keyPrefix: string
Expand Down Expand Up @@ -155,5 +154,5 @@ export const getTightestLimit = (
return lowest
}

const defaultKey: CalcKeyFn = (ctx: XRPCReqContext) => getReqIp(ctx.req)
const defaultKey: CalcKeyFn = (ctx: XRPCReqContext) => ctx.req.ip
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we drop a note nearby here re: app.set('trust proxy', true)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good call 👍

const defaultPoints: CalcPointsFn = () => 1
4 changes: 0 additions & 4 deletions packages/xrpc-server/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,6 @@ function decodeBodyStream(
return stream
}

export const getReqIp = (req: express.Request): string => {
return req.ips.at(-1) ?? req.ip
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of the issue here is that req.ips was empty since 'trust proxy' was false, so fell back to req.ip, which was the raw socket address (again since 'trust proxy' was false).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup totally 👍

altho this would still need to grab the first ip in the array i believe (req.at(0)) which is exactly what req.ip does so i figured i'd just simplify it

}

export function serverTimingHeader(timings: ServerTiming[]) {
return timings
.map((timing) => {
Expand Down