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

fix: truncate the api key when logging #2828

Merged
merged 6 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 20 additions & 0 deletions packages/config/src/__tests__/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { truncateApiKey } from '../api.truncate.js';
import o from 'ospec';

o.spec('ApiKeyTruncate', () => {
o('should truncate apikeys', () => {
o(truncateApiKey('c01h3e17kjsw5evq8ndjxbda80e')).equals('cbda80e');
o(truncateApiKey('d01h3e17kjsw5evq8ndjxbda80e')).equals('dbda80e');
});

o('should not truncate invalid api keys', () => {
o(truncateApiKey([{ hello: 'world' }])).deepEquals('invalid');
o(truncateApiKey(null)).equals('invalid');
o(truncateApiKey(1)).equals('invalid');
});

o('should not truncate truncated', () => {
o(truncateApiKey('cbda80e')).equals('cbda80e');
o(truncateApiKey('dbda80e')).equals('dbda80e');
});
});
7 changes: 7 additions & 0 deletions packages/config/src/api.truncate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** Truncate a API key to the last 6 digits */
export function truncateApiKey(x: unknown): string | undefined {
if (typeof x !== 'string') return 'invalid';
if (x.startsWith('c')) return 'c' + x.slice(x.length - 6);
if (x.startsWith('d')) return 'd' + x.slice(x.length - 6);
return 'invalid';
blacha marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export { ConfigJson, zoomLevelsFromWmts } from './json/json.config.js';
export { standardizeLayerName } from './json/name.convertor.js';
export { base58, isBase58 } from './base58.js';
export { sha256base58, ensureBase58 } from './base58.node.js';
export { truncateApiKey } from './api.truncate.js';
4 changes: 3 additions & 1 deletion packages/lambda-tiler/src/util/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Const, isValidApiKey } from '@basemaps/shared';
import { getImageFormat } from '@basemaps/tiler';
import { LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
import { TileXyzGet } from '../routes/tile.xyz';
import { truncateApiKey } from '@basemaps/config';

export interface TileXyz {
tile: { x: number; y: number; z: number };
Expand All @@ -25,7 +26,8 @@ export const Validate = {
const valid = isValidApiKey(apiKey);

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

Expand Down