mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 02:34:21 +00:00
feat(api): rate-limit /api/v1 and report when a trip last changed
Two gaps from the first pass, both about a client that polls. Rate limiting is keyed by user rather than by IP. A self-hosted integration and its owner's browser routinely leave the same address, and an IP bucket would let one starve the other; 120 requests a minute is generous for a sync surface and still bounded, so a runaway poll degrades its own integration instead of the instance. updated_at now ships with every trip, and its docblock says plainly what it is not: a content timestamp. It moves when the trip's own fields change — title, dates, currency, cover — and stays put when a place, a day or a note is edited, because the child tables carry no updated_at of their own. That also rules out the ?since= filter this was meant to enable: it would silently hide every trip whose itinerary changed, which is worse than no filter at all. Making it honest needs a schema change, not an API field, so the field is documented for what it is and the filter is left out.
This commit is contained in:
@@ -9,6 +9,8 @@ import {
|
||||
} from '@trek/shared';
|
||||
import { ApiTokenGuard } from './api-token.guard';
|
||||
import { PublicApiService } from './public-api.service';
|
||||
import { RateLimitService } from '../common/rate-limit.service';
|
||||
import type { User } from '../../types';
|
||||
|
||||
/**
|
||||
* `/api/v1` — the versioned, read-only surface for third-party integrations.
|
||||
@@ -24,15 +26,40 @@ import { PublicApiService } from './public-api.service';
|
||||
* missing feature: it would need scope enforcement this surface does not implement
|
||||
* yet, and a token that reads everything is a very different thing from one that
|
||||
* can also delete a trip.
|
||||
*
|
||||
* Rate limited per token rather than per IP: a self-hosted integration and its
|
||||
* user's browser routinely share an address, and limiting by IP would let one
|
||||
* starve the other. The budget is generous — this is a sync surface, not a login
|
||||
* form — but bounded, so a runaway poll degrades its own integration instead of
|
||||
* the instance.
|
||||
*/
|
||||
const RATE_WINDOW_MS = 60_000;
|
||||
const RATE_MAX_PER_MINUTE = 120;
|
||||
|
||||
@Controller('api/v1')
|
||||
@UseGuards(ApiTokenGuard)
|
||||
export class PublicApiController {
|
||||
constructor(private readonly api: PublicApiService) {}
|
||||
constructor(
|
||||
private readonly api: PublicApiService,
|
||||
private readonly rl: RateLimitService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Keyed by token id, not by IP. Falls back to the user id when a token somehow
|
||||
* carries none, so the limiter can never end up with a shared bucket.
|
||||
*/
|
||||
private limit(req: Request): void {
|
||||
const user = req.user as User | undefined;
|
||||
const key = `user:${user?.id ?? 'unknown'}`;
|
||||
if (!this.rl.check('public-api', key, RATE_MAX_PER_MINUTE, RATE_WINDOW_MS, Date.now())) {
|
||||
throw new HttpException({ error: 'Too many requests. Please slow down.' }, 429);
|
||||
}
|
||||
}
|
||||
|
||||
/** Every trip the token's owner can reach, without itineraries. */
|
||||
@Get('trips')
|
||||
listTrips(@Req() req: Request): PublicApiTripList {
|
||||
this.limit(req);
|
||||
return { trips: this.api.listTrips(requireUserId(req)) };
|
||||
}
|
||||
|
||||
@@ -49,6 +76,7 @@ export class PublicApiController {
|
||||
@Param('id') id: string,
|
||||
@Query('include') include?: string,
|
||||
): PublicApiTrip {
|
||||
this.limit(req);
|
||||
const tripId = parseTripId(id);
|
||||
const trip = this.api.getTrip(tripId, requireUserId(req), parseInclude(include));
|
||||
if (!trip) {
|
||||
|
||||
Reference in New Issue
Block a user