Skip to content

Commit 3396593

Browse files
authored
fix: truncate the api key when logging (#2828)
Prevents some logs having a full valid api key logged to them while preserving some ability to lookup the apikey to figure our who the request was triggered by.
1 parent db3ff1b commit 3396593

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

packages/lambda-tiler/src/util/validate.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ImageFormat, Projection, TileMatrixSet, TileMatrixSets, VectorFormat } from '@basemaps/geo';
2-
import { Const, isValidApiKey } from '@basemaps/shared';
2+
import { Const, isValidApiKey, truncateApiKey } from '@basemaps/shared';
33
import { getImageFormat } from '@basemaps/tiler';
44
import { LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
55
import { TileXyzGet } from '../routes/tile.xyz';
@@ -25,7 +25,8 @@ export const Validate = {
2525
const valid = isValidApiKey(apiKey);
2626

2727
if (!valid.valid) throw new LambdaHttpResponse(400, 'API Key Invalid: ' + valid.message);
28-
req.set('api', apiKey);
28+
// Truncate the API Key so we are not logging the full key
29+
req.set('api', truncateApiKey(apiKey));
2930
return apiKey as string;
3031
},
3132

packages/shared/src/__tests__/api.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import o from 'ospec';
2-
import { getApiKey, OneDayMs } from '../api.js';
3-
import { ulid, decodeTime, encodeTime } from 'ulid';
2+
import { decodeTime, encodeTime, ulid } from 'ulid';
3+
import { getApiKey, OneDayMs, truncateApiKey } from '../api.js';
44

55
declare const global: {
66
localStorage?: { getItem: (a: string) => string | null; setItem: (k: string, v: string) => void };
@@ -53,3 +53,21 @@ o.spec('ApiKey', () => {
5353
o(Date.now() - decodeTime(newApiKey.slice(1).toUpperCase()) < 1000).equals(true);
5454
});
5555
});
56+
57+
o.spec('ApiKeyTruncate', () => {
58+
o('should truncate apikeys', () => {
59+
o(truncateApiKey('c01h3e17kjsw5evq8ndjxbda80e')).equals('cbda80e');
60+
o(truncateApiKey('d01h3e17kjsw5evq8ndjxbda80e')).equals('dbda80e');
61+
});
62+
63+
o('should not truncate invalid api keys', () => {
64+
o(truncateApiKey([{ hello: 'world' }])).deepEquals('invalid');
65+
o(truncateApiKey(null)).equals('invalid');
66+
o(truncateApiKey(1)).equals('invalid');
67+
});
68+
69+
o('should not truncate truncated', () => {
70+
o(truncateApiKey('cbda80e')).equals('cbda80e');
71+
o(truncateApiKey('dbda80e')).equals('dbda80e');
72+
});
73+
});

packages/shared/src/api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ export function isValidApiKey(apiKey?: string | null): ApiKeyStatus {
5656

5757
return { valid: true, key: apiKey };
5858
}
59+
60+
/** Truncate a API key to the last 6 digits */
61+
export function truncateApiKey(x: unknown): string | undefined {
62+
if (typeof x !== 'string') return 'invalid';
63+
if (x.startsWith('c')) return 'c' + x.slice(x.length - 6);
64+
if (x.startsWith('d')) return 'd' + x.slice(x.length - 6);
65+
return 'invalid';
66+
}

0 commit comments

Comments
 (0)