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

Write rate limits #1578

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
28 changes: 28 additions & 0 deletions packages/pds/src/api/com/atproto/repo/applyWrites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InvalidRequestError, AuthRequiredError } from '@atproto/xrpc-server'
import { prepareCreate, prepareDelete, prepareUpdate } from '../../../../repo'
import { Server } from '../../../../lexicon'
import {
HandlerInput,
isCreate,
isUpdate,
isDelete,
Expand All @@ -15,9 +16,36 @@ import {
import AppContext from '../../../../context'
import { ConcurrentWriteError } from '../../../../services/repo'

const ratelimitPoints = ({ input }: { input: HandlerInput }) => {
let points = 0
for (const op of input.body.writes) {
if (isCreate(op)) {
points += 3
} else if (isUpdate(op)) {
points += 2
} else {
points += 1
}
}
return points
}

export default function (server: Server, ctx: AppContext) {
server.com.atproto.repo.applyWrites({
auth: ctx.accessVerifierCheckTakedown,
rateLimit: [
{
name: 'repo-write-hour',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: ratelimitPoints,
},
{
name: 'repo-write-day',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: ratelimitPoints,
},
],

handler: async ({ input, auth }) => {
const tx = input.body
const { repo, validate, swapCommit } = tx
Expand Down
12 changes: 12 additions & 0 deletions packages/pds/src/api/com/atproto/repo/createRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ import { ConcurrentWriteError } from '../../../../services/repo'
export default function (server: Server, ctx: AppContext) {
server.com.atproto.repo.createRecord({
auth: ctx.accessVerifierCheckTakedown,
rateLimit: [
{
name: 'repo-write-hour',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 3,
},
{
name: 'repo-write-day',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 3,
},
],
handler: async ({ input, auth }) => {
const { repo, collection, rkey, record, swapCommit, validate } =
input.body
Expand Down
12 changes: 12 additions & 0 deletions packages/pds/src/api/com/atproto/repo/deleteRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import { ConcurrentWriteError } from '../../../../services/repo'
export default function (server: Server, ctx: AppContext) {
server.com.atproto.repo.deleteRecord({
auth: ctx.accessVerifierCheckTakedown,
rateLimit: [
{
name: 'repo-write-hour',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 1,
},
{
name: 'repo-write-day',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 1,
},
],
handler: async ({ input, auth }) => {
const { repo, collection, rkey, swapCommit, swapRecord } = input.body
const did = await ctx.services.account(ctx.db).getDidForActor(repo)
Expand Down
12 changes: 12 additions & 0 deletions packages/pds/src/api/com/atproto/repo/putRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ import { ConcurrentWriteError } from '../../../../services/repo'
export default function (server: Server, ctx: AppContext) {
server.com.atproto.repo.putRecord({
auth: ctx.accessVerifierCheckTakedown,
rateLimit: [
{
name: 'repo-write-hour',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 2,
},
{
name: 'repo-write-day',
calcKey: ({ auth }) => auth.credentials.did,
calcPoints: () => 2,
},
],
handler: async ({ auth, input }) => {
const {
repo,
Expand Down
15 changes: 14 additions & 1 deletion packages/pds/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
RateLimiterOpts,
Options as XrpcServerOptions,
} from '@atproto/xrpc-server'
import { MINUTE } from '@atproto/common'
import { DAY, HOUR, MINUTE } from '@atproto/common'
import * as appviewConsumers from './app-view/event-stream/consumers'
import inProcessAppView from './app-view/api'
import API from './api'
Expand Down 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 Expand Up @@ -285,6 +286,18 @@ export class PDS {
points: 3000,
},
],
shared: [
{
name: 'repo-write-hour',
durationMs: HOUR,
points: 5000, // creates=3, puts=2, deletes=1
},
{
name: 'repo-write-day',
durationMs: DAY,
points: 35000, // creates=3, puts=2, deletes=1
},
],
}
}

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
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
}

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