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

feat: add headers redacter #18

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/lib/base-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ import {AppConfig} from '../types';

export const NODEKIT_BASE_CONFIG: AppConfig = {
nkDefaultSensitiveKeys: ['authorization', 'cookie', 'set-cookie', 'password'],
nkDefaultSensitiveHeaders: ['authorization', 'cookie', 'set-cookie', 'password'],
nkDefaultHeadersWithSensitiveUrls: ['referer'],
nkDefaultSensitiveQueryParams: [],
};
31 changes: 31 additions & 0 deletions src/lib/utils/redact-sensitive-headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {IncomingHttpHeaders} from 'http';
import {Dict} from '../../types';
import {prepareSensitiveKeysRedacter} from './redact-sensitive-keys';

export type SensitiveHeadersRedacter = (inputHeaders: Dict | IncomingHttpHeaders) => Dict;

export default function prepareSensitiveHeadersRedacter(
sensitiveHeaders: Array<string> = [],
headersWithSensitiveUrls: Array<string> = [],
redactSensitiveQueryParams: (input: string) => string = (input) => input,
isDevMode = false,
) {
const redactSensitiveHeaders: SensitiveHeadersRedacter = (inputHeaders) => {
if (isDevMode) {
return inputHeaders;
}

const redactSensitiveKeys = prepareSensitiveKeysRedacter(sensitiveHeaders);

const result = redactSensitiveKeys(inputHeaders);

Object.keys(result).forEach((headerName) => {
if (headersWithSensitiveUrls.includes(headerName.toLowerCase())) {
result[headerName] = redactSensitiveQueryParams(result[headerName] as string);
}
});

return result;
};
return redactSensitiveHeaders;
}
47 changes: 47 additions & 0 deletions src/lib/utils/redact-sensitive-query-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {URL} from 'url';
import {Dict} from '../../types';
import {prepareSensitiveKeysRedacter} from './redact-sensitive-keys';

export type SensitiveQueryParamsRedacter = (input?: string) => string;

export default function prepareSensitiveQueryParamsRedacter(
sensitiveQueryParams: Array<string> = [],
isDevMode = false,
) {
const redactSensitiveQueryParams: SensitiveQueryParamsRedacter = (input = ''): string => {
if (isDevMode || !input) {
return input;
}

if (sensitiveQueryParams.length === 0) {
return input;
}

const defaultBase = 'http://127.0.0.1';
const parsedUrl = new URL(input, defaultBase);

const matchedSensitiveQueryParams = sensitiveQueryParams.reduce<Dict>((acc, key) => {
if (parsedUrl.searchParams.has(key)) {
acc[key] = parsedUrl.searchParams.get(key);
}
return acc;
}, {});

if (Object.keys(matchedSensitiveQueryParams).length === 0) {
return input;
}

const redactSensitiveKeys = prepareSensitiveKeysRedacter(sensitiveQueryParams);
const redactedSensitiveQueryParams = redactSensitiveKeys(matchedSensitiveQueryParams);

Object.keys(redactedSensitiveQueryParams).forEach((key) => {
parsedUrl.searchParams.set(key, redactedSensitiveQueryParams[key] as string);
});

const resultUrl = parsedUrl.toString();

return resultUrl.startsWith(defaultBase) ? resultUrl.replace(defaultBase, '') : resultUrl;
};

return redactSensitiveQueryParams;
}
26 changes: 26 additions & 0 deletions src/nodekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import {
} from './lib/utils/redact-sensitive-keys';
import {prepareClickhouseClient} from './lib/telemetry/clickhouse';
import {DynamicConfigSetup, DynamicConfigPoller} from './lib/dynamic-config-poller';
import prepareSensitiveHeadersRedacter, {
SensitiveHeadersRedacter,
} from './lib/utils/redact-sensitive-headers';
import prepareSensitiveQueryParamsRedacter, {
SensitiveQueryParamsRedacter,
} from './lib/utils/redact-sensitive-query-params';

interface InitOptions {
disableDotEnv?: boolean;
Expand All @@ -26,6 +32,8 @@ export class NodeKit {

utils: {
redactSensitiveKeys: SensitiveKeysRedacter;
redactSensitiveQueryParams: SensitiveQueryParamsRedacter;
redactSensitiveHeaders: SensitiveHeadersRedacter;
isTrueEnvValue: (arg: string) => boolean;
};

Expand Down Expand Up @@ -60,10 +68,28 @@ export class NodeKit {
devMode: appDevMode,
});

const redactSensitiveQueryParams = prepareSensitiveQueryParamsRedacter(
this.config.nkDefaultSensitiveQueryParams?.concat(
this.config.appSensitiveQueryParams || [],
),
appDevMode,
);

const redactSensitiveHeaders = prepareSensitiveHeadersRedacter(
this.config.nkDefaultSensitiveHeaders?.concat(this.config.appSensitiveHeaders || []),
this.config.nkDefaultHeadersWithSensitiveUrls?.concat(
this.config.appHeadersWithSensitiveUrls || [],
),
redactSensitiveQueryParams,
appDevMode,
);

this.utils = {
redactSensitiveKeys: prepareSensitiveKeysRedacter(
this.config.nkDefaultSensitiveKeys?.concat(this.config.appSensitiveKeys || []),
),
redactSensitiveHeaders,
redactSensitiveQueryParams,
isTrueEnvValue,
};

Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ export interface AppConfig {
appDevMode?: boolean;

nkDefaultSensitiveKeys?: string[];
nkDefaultSensitiveHeaders?: string[];
nkDefaultHeadersWithSensitiveUrls?: string[];
nkDefaultSensitiveQueryParams?: string[];
appSensitiveKeys?: string[];
appSensitiveHeaders?: string[];
appHeadersWithSensitiveUrls?: string[];
appSensitiveQueryParams?: string[];

appTracingEnabled?: boolean;
appTracingServiceName?: string;
Expand Down