mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
fix(maps): stop the photo endpoint answering 404 for photo-less places (#1727)
Opening one trip fires a 404 per place without a photo, 30 to 60 of them from one IP in seconds. CrowdSec and fail2ban read that as scanning; the reporter had three users banned within five minutes. A place having no photo is not an error and no longer answers as one. OSM places were also sent to Google as node:/way:/relation: ids, which can only return 400 INVALID_ARGUMENT — a billed round-trip and about 1.3s per place. getPlacePhoto already guarded coords: this way; the OSM prefixes now get the same treatment, as does getPlaceDetailsExpanded, which had no guard. The negative cache separates a place having no photo from a failed provider call instead of expiring both after five minutes and replaying the storm.
This commit is contained in:
@@ -160,6 +160,9 @@ export class MapsController {
|
||||
if (!placeId.startsWith('coords:') && this.maps.photosDisabled()) {
|
||||
return { photoUrl: null };
|
||||
}
|
||||
// A place with no photo resolves to the same { photoUrl: null } body. It is an
|
||||
// empty result, not a missing resource, and one 404 per photo-less place gets
|
||||
// the user's IP banned by any 404-rate IPS in front of TREK (#1727).
|
||||
try {
|
||||
return await this.maps.photo(user.id, placeId, parseFloat(lat as string), parseFloat(lng as string), name);
|
||||
} catch (err: unknown) {
|
||||
@@ -173,24 +176,37 @@ export class MapsController {
|
||||
placePhotoBytes(@Param('placeId') placeId: string, @Res() res: Response): void {
|
||||
const fp = this.maps.photoBytesPath(placeId);
|
||||
if (!fp) {
|
||||
res.status(404).json({ error: 'Photo not cached' });
|
||||
// Same reasoning as the JSON endpoint above, and the bigger half of #1727:
|
||||
// places keep this URL in image_url, so a trip render asks for one photo per
|
||||
// place and every entry the cache sweep dropped answered 404 — a whole map
|
||||
// worth of them from one IP in one second. An empty 204 leaves the <img>
|
||||
// with nothing to show, exactly like the 404 did.
|
||||
this.emptyPhoto(res);
|
||||
return;
|
||||
}
|
||||
// Stream the cached file directly instead of res.sendFile(): the send library
|
||||
// bundled under @nestjs/platform-express rejects absolute Windows paths (drive
|
||||
// letter, no `root`) with a NotFoundError that surfaced as an unhandled 500,
|
||||
// even though the file exists. A plain read stream serves the bytes
|
||||
// cross-platform; a read error still yields the legacy 404. Cached photos are
|
||||
// always JPEG (placePhotoCache writes `<hash>.jpg`).
|
||||
// cross-platform. Cached photos are always JPEG (placePhotoCache writes
|
||||
// `<hash>.jpg`).
|
||||
res.set('Cache-Control', 'public, max-age=2592000, immutable');
|
||||
res.type('image/jpeg');
|
||||
const stream = createReadStream(fp);
|
||||
stream.on('error', () => {
|
||||
if (!res.headersSent) res.status(404).json({ error: 'Photo not cached' });
|
||||
if (!res.headersSent) this.emptyPhoto(res);
|
||||
});
|
||||
stream.pipe(res);
|
||||
}
|
||||
|
||||
// 204 for "no bytes to serve". Overrides the immutable Cache-Control the hit
|
||||
// path already set — a photo that reappears in the cache must not stay hidden
|
||||
// behind a month-old empty response.
|
||||
private emptyPhoto(res: Response): void {
|
||||
res.set('Cache-Control', 'no-store');
|
||||
res.status(204).end();
|
||||
}
|
||||
|
||||
@Get('reverse')
|
||||
async reverse(
|
||||
@Query('lat') lat?: string,
|
||||
|
||||
Reference in New Issue
Block a user