diff --git a/src/api/base.ts b/src/api/base.ts index 569acd18..e5531ec6 100644 --- a/src/api/base.ts +++ b/src/api/base.ts @@ -13,9 +13,69 @@ interface FormattedAxiosError { error?: T; status?: number; } +// An axios error keeps the request's headers, the Authorization header with the API key +// included, in its config and in the live Node request object. Replace that header's value in +// the configs and in the request's raw header text, and turn `request` on the error and its +// response into a non-enumerable accessor (axios's own toJSON() already leaves it out). Printing, +// inspecting (with hidden properties too: an accessor shows as [Getter/Setter] without being +// called) or serializing the error then doesn't show the key. `error.request` stays readable +// and writable, and inspecting that object itself still shows the headers Node keeps inside it. +export function redactAuthorization(error: AxiosError): void { + for (const config of [error.config, error.response?.config]) { + const headers = config?.headers as Record | undefined; + if (!headers) { + continue; + } + for (const name of Object.keys(headers)) { + if (name.toLowerCase() === 'authorization') { + headers[name] = '[REDACTED]'; + } + } + } + const request = error.request as { _header?: unknown } | undefined; + if (request && typeof request._header === 'string') { + // Line by line, not by regex: a pattern with two adjacent wildcards can backtrack + // polynomially on long header text. + request._header = request._header + .split('\n') + .map((line) => + line.toLowerCase().startsWith('authorization:') + ? `Authorization: [REDACTED]${line.endsWith('\r') ? '\r' : ''}` + : line, + ) + .join('\n'); + } + for (const holder of [error, error.response]) { + if (holder?.request !== undefined) { + let held: unknown = holder.request; + Object.defineProperty(holder, 'request', { + get: () => held, + set: (value: unknown) => { + held = value; + }, + enumerable: false, + configurable: true, + }); + } + } +} + export class PermitApiError extends Error { - constructor(message: string, public originalError: AxiosError) { - super(message); + // Not enumerable, so logging or inspecting the error prints the message, not the whole + // axios error. Still readable and writable as before; its Authorization header is redacted. + public originalError!: AxiosError; + + constructor(message: string, originalError: AxiosError) { + // A failure without a response (a refused connection, a timeout) has no API message: + // axios's message says what happened, and carries no header. + super(message || originalError.message); + redactAuthorization(originalError); + Object.defineProperty(this, 'originalError', { + value: originalError, + enumerable: false, + writable: true, + configurable: true, + }); } public get formattedAxiosError(): FormattedAxiosError { diff --git a/src/config.ts b/src/config.ts index fa6b06d7..c5ccdc4c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -19,7 +19,9 @@ interface ILoggerConfig { label: string; /** - * Sets whether the SDK log output should be in JSON format. + * Has no effect: the SDK log output is always JSON. + * + * @deprecated pino 8 dropped the pretty-print option this used to set. */ json: boolean; } @@ -154,8 +156,10 @@ export class ConfigFactory { level: _.get(process.env, 'PERMIT_LOG_LEVEL', 'warn'), // Label added to logs label: _.get(process.env, 'PERMIT_LOG_LABEL', 'Permit.io'), - // When logging - dump full data to console as JSON - json: JSON.parse(_.get(process.env, 'PERMIT_LOG_JSON', 'false')), + // Read leniently: the option has no effect, so no value may stop the client starting. + json: ['true', '1'].includes( + _.get(process.env, 'PERMIT_LOG_JSON', 'false').trim().toLowerCase(), + ), }, multiTenancy: { defaultTenant: 'default', diff --git a/src/enforcement/enforcer.ts b/src/enforcement/enforcer.ts index f5218daf..95bba0d7 100644 --- a/src/enforcement/enforcer.ts +++ b/src/enforcement/enforcer.ts @@ -2,9 +2,11 @@ import axios, { AxiosInstance } from 'axios'; import { Logger } from 'pino'; import URL from 'url-parse'; +import { redactAuthorization } from '../api/base'; import { IPermitConfig } from '../config'; import { CheckConfig, Context, ContextStore } from '../utils/context'; import { AxiosLoggingInterceptor } from '../utils/http-logger'; +import { redactUrl } from '../utils/redact-url'; import { resolveRetryConfig } from '../utils/retry'; import { AxiosRetryInterceptor } from '../utils/retry-interceptor'; @@ -253,7 +255,7 @@ export class Enforcer implements IEnforcer { } throw new PermitConnectionError(`Permit SDK got error: \n ${error.message} \n and cannot connect to the PDP, please check your configuration and make sure the - PDP is running at ${this.config.pdp} and accepting requests. \n + PDP is running at ${redactUrl(this.config.pdp)} and accepting requests. \n Read more about setting up the PDP at https://docs.permit.io`); }); } @@ -350,7 +352,7 @@ export class Enforcer implements IEnforcer { } throw new PermitConnectionError(`Permit SDK got error: \n ${error.message} \n and cannot connect to the PDP, please check your configuration and make sure the - PDP is running at ${this.config.pdp} and accepting requests. \n + PDP is running at ${redactUrl(this.config.pdp)} and accepting requests. \n Read more about setting up the PDP at https://docs.permit.io`); }); } @@ -362,22 +364,31 @@ export class Enforcer implements IEnforcer { context: Context = {}, // default to empty context if not provided sdk = 'node', // default to "node" if not provided ): Promise { + // The query goes in the body, as for check(), and the key in the Authorization header. + // The resource keeps the tenant it names: the answer lists every tenant it is allowed in, + // so the default tenant is not filled in. + const resourceObj = isString(resource) ? Enforcer.resourceFromString(resource) : resource; + const input: ICheckInput = { + user: isString(user) ? { key: user } : user, + action, + resource: resourceObj, + context: this.contextStore.getDerivedContext(context), + }; try { - const response = await this.client.post('/allowed/all-tenants', { + const response = await this.client.post('allowed/all-tenants', input, { headers: { Authorization: `Bearer ${this.config.token}`, 'X-Permit-Sdk-Language': sdk, }, - params: { - user, - action, - resource, - context, - }, + timeout: this.config.timeout, }); - return response.data.allowedTenants.map((item) => item.tenant); + return response.data.allowed_tenants.map((item) => item.tenant); } catch (error) { - this.logger.error('Error fetching all tenants:', error); + // Rethrown as it is, so callers keep the error type they get today, less the key. + if (axios.isAxiosError(error)) { + redactAuthorization(error); + } + this.logger.error({ err: error }, 'Error fetching all tenants'); throw error; } } @@ -454,7 +465,9 @@ export class Enforcer implements IEnforcer { this.logger.error(`${errorMessage}\n${error}`); } throw new PermitConnectionError(`Permit SDK got error: \n ${error.message} \n - and cannot connect to the PDP, please check your configuration and make sure the PDP is running at ${this.config.pdp} and accepting requests. \n + and cannot connect to the PDP, please check your configuration and make sure the PDP is running at ${redactUrl( + this.config.pdp, + )} and accepting requests. \n Read more about setting up the PDP at https://docs.permit.io`); }); } diff --git a/src/enforcement/interfaces.ts b/src/enforcement/interfaces.ts index ad963e04..d27d5673 100644 --- a/src/enforcement/interfaces.ts +++ b/src/enforcement/interfaces.ts @@ -142,7 +142,7 @@ export interface AllTenantsCheckResponse { } export interface AllTenantsResponse { - allowedTenants: AllTenantsCheckResponse[]; + allowed_tenants: AllTenantsCheckResponse[]; } interface TenantPermissions { diff --git a/src/index.ts b/src/index.ts index a96af183..5968f5f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ import { import { LoggerFactory } from './logger'; import { CheckConfig, Context } from './utils/context'; import { AxiosLoggingInterceptor } from './utils/http-logger'; +import { redactUrl } from './utils/redact-url'; import { resolveRetryConfig } from './utils/retry'; import { AxiosRetryInterceptor } from './utils/retry-interceptor'; import { RecursivePartial } from './utils/types'; @@ -47,6 +48,23 @@ export interface IPermitClient extends IEnforcer { elements: IPermitElementsApi; } +// What the init line logs: the endpoints, without credentials, and the settings that change +// the SDK's behaviour. Not the config itself, whose token and axios instances stay out of logs. +function initLogFields(config: IPermitConfig): Record { + return { + pdp: redactUrl(config.pdp), + apiUrl: redactUrl(config.apiUrl), + timeout: config.timeout, + throwOnError: config.throwOnError, + multiTenancy: config.multiTenancy, + proxyFactsViaPdp: config.proxyFactsViaPdp, + factsSyncTimeout: config.factsSyncTimeout, + factsSyncTimeoutPolicy: config.factsSyncTimeoutPolicy, + retry: config.retry, + pdpRetry: config.pdpRetry, + }; +} + /** * The `Permit` class represents the main entry point for interacting with the Permit.io SDK. * The SDK constructor expects an object implementing the {@link IPermitConfig} interface. @@ -168,9 +186,7 @@ export class Permit implements IPermitClient { this.enforcer = new Enforcer(this.config, this.logger); this.elements = new ElementsClient(this.config, this.logger); - this.logger.debug( - `Permit.io SDK initialized with config:\n${JSON.stringify(this.config, undefined, 2)}`, - ); + this.logger.debug(initLogFields(this.config), 'Permit.io SDK initialized'); } /** @@ -226,12 +242,8 @@ export class Permit implements IPermitClient { context?: Context | undefined, sdk?: string | undefined, ): Promise { - try { - return await this.enforcer.checkAllTenants(user, action, resource, context, sdk); - } catch (error) { - this.logger.error('Error fetching all tenants:', error); - throw error; - } + // The enforcer logs a failure, once, before rethrowing it. + return await this.enforcer.checkAllTenants(user, action, resource, context, sdk); } /** diff --git a/src/logger.ts b/src/logger.ts index 48a13c25..0170ae5e 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,20 +1,65 @@ import util from 'util'; +import axios from 'axios'; import pino from 'pino'; import { IPermitConfig } from './config'; +import { redactUrl } from './utils/redact-url'; export function prettyConsoleLog(label: string, data: any) { console.log(label, util.inspect(data, false, 12, true)); } +// pino's standard error serializer copies every field of an error, and an axios error's +// config, request and response hold the Authorization header (the request even as raw +// header text). An axios error is logged as what identifies the failure, and nothing else. +export function serializeError(error: unknown): unknown { + if (axios.isAxiosError(error)) { + return { + type: error.name, + message: error.message, + code: error.code, + status: error.response?.status, + method: error.config?.method?.toUpperCase(), + url: error.config?.url == null ? undefined : redactUrl(String(error.config.url)), + }; + } + // Any other error is copied field by field. Node's ERR_INVALID_URL, which axios rejects with + // for a URL it can't parse, carries that whole URL in `input`. + const serialized = pino.stdSerializers.err(error as Error); + // A copy: for a value that isn't error-like, pino returns the caller's own object. + if (typeof serialized?.input === 'string') { + return { ...serialized, input: redactUrl(serialized.input) }; + } + return serialized; +} + export class LoggerFactory { - static createLogger(config: IPermitConfig): pino.Logger { - return pino({ + // `destination` defaults to stdout, as before; tests pass a stream to read the output. + static createLogger(config: IPermitConfig, destination?: pino.DestinationStream): pino.Logger { + const options: pino.LoggerOptions = { level: config.log.level, - prettyPrint: config.log.json ? { levelFirst: true } : false, base: { label: config.log.label }, timestamp: pino.stdTimeFunctions.isoTime, - }); + serializers: { err: serializeError }, + // A backstop for a credential that a log call passes in an object: a `token` field, or + // an `Authorization` / `authorization` header in a `headers` field, at the top level of + // that object or one level down. fast-redact has no recursive wildcard, so deeper fields + // and other spellings aren't covered; errors go through `serializeError` instead. + redact: { + paths: [ + 'token', + '*.token', + 'headers.Authorization', + 'headers.authorization', + '*.headers.Authorization', + '*.headers.authorization', + ], + censor: '[REDACTED]', + }, + }; + // pino 8 has no prettyPrint option and throws on a truthy one, so `log.json` no longer + // reaches pino: the output is JSON whatever it says, as it already was by default. + return destination ? pino(options, destination) : pino(options); } } diff --git a/src/tests/unit/init-log.spec.ts b/src/tests/unit/init-log.spec.ts new file mode 100644 index 00000000..cd2f64d5 --- /dev/null +++ b/src/tests/unit/init-log.spec.ts @@ -0,0 +1,106 @@ +import { spawnSync } from 'child_process'; +import path from 'path'; + +import test from 'ava'; + +// A token value that only this test uses. +const TOKEN = 'permit_key_init_log_test_0123456789abcdef'; + +// The SDK's logger writes to the process's stdout file descriptor, so run a client in a +// child process and read what it printed. The child gets no PERMIT_* variables, so the +// shell running the tests can't change the client's config. +function initLog(config: Record, env: Record = {}) { + const sdk = path.resolve(__dirname, '../../index.js'); + const script = ` + const { Permit } = require(${JSON.stringify(sdk)}); + new Permit(${JSON.stringify({ token: TOKEN, ...config })}); + `; + const childEnv: NodeJS.ProcessEnv = {}; + for (const [name, value] of Object.entries(process.env)) { + if (!name.startsWith('PERMIT_')) { + childEnv[name] = value; + } + } + const result = spawnSync(process.execPath, ['-e', script], { + encoding: 'utf8', + env: { ...childEnv, ...env }, + // A hung child fails here, with the reason, rather than at ava's inactivity timeout. + timeout: 20000, + }); + if (result.error || result.status !== 0) { + throw new Error( + `the client failed to start (status ${result.status}, signal ${result.signal}): ${ + result.error ?? result.stderr + }`, + ); + } + const output = result.stdout + result.stderr; + const records = result.stdout + .split('\n') + .filter((line) => line.trim().startsWith('{')) + .map((line) => JSON.parse(line)); + return { output, records }; +} + +test('the debug init line carries the endpoints and settings, nothing else', (t) => { + const { output, records } = initLog({ + pdp: 'http://pdp-user:pdp-password@pdp.example.com:7766/path?key=query-secret', + apiUrl: 'https://api.example.com/?sig=query-secret', + log: { level: 'debug' }, + // Set, so that every field the line carries is present. + timeout: 1234, + retry: { maxRetries: 2 }, + pdpRetry: { maxRetries: 1 }, + }); + + const init = records.find((record) => record.msg === 'Permit.io SDK initialized'); + t.truthy(init, output); + t.is(init.pdp, 'http://pdp.example.com:7766/path'); + t.is(init.apiUrl, 'https://api.example.com'); + t.is(init.timeout, 1234); + t.deepEqual(init.retry, { maxRetries: 2 }); + t.deepEqual(init.pdpRetry, { maxRetries: 1 }); + t.deepEqual( + Object.keys(init) + .filter((key) => !['level', 'time', 'label', 'msg'].includes(key)) + .sort(), + [ + 'apiUrl', + 'factsSyncTimeout', + 'factsSyncTimeoutPolicy', + 'multiTenancy', + 'pdp', + 'pdpRetry', + 'proxyFactsViaPdp', + 'retry', + 'throwOnError', + 'timeout', + ], + ); + for (const secret of [TOKEN, 'pdp-password', 'pdp-user', 'query-secret']) { + t.false(output.includes(secret), secret); + } +}); + +test('the init line keeps a PDP value without a scheme recognisable', (t) => { + const { output, records } = initLog({ + pdp: 'pdp-user:pdp-password@localhost:7766', + log: { level: 'debug' }, + }); + + const init = records.find((record) => record.msg === 'Permit.io SDK initialized'); + t.truthy(init, output); + t.is(init.pdp, 'localhost:7766'); + t.false(output.includes('pdp-password')); +}); + +test('the trace level does not print the token either', (t) => { + t.false(initLog({ log: { level: 'trace' } }).output.includes(TOKEN)); +}); + +test('log.json no longer stops the client from starting', (t) => { + t.notThrows(() => initLog({ log: { json: true } })); + for (const value of ['true', 'True', '', 'yes', '1']) { + t.notThrows(() => initLog({}, { PERMIT_LOG_JSON: value }), value); + } +}); diff --git a/src/tests/unit/key-exposure.spec.ts b/src/tests/unit/key-exposure.spec.ts new file mode 100644 index 00000000..c00e2afd --- /dev/null +++ b/src/tests/unit/key-exposure.spec.ts @@ -0,0 +1,300 @@ +import http from 'http'; +import { AddressInfo } from 'net'; +import util from 'util'; + +import test from 'ava'; +import axios, { AxiosError, AxiosHeaders, AxiosInstance, InternalAxiosRequestConfig } from 'axios'; + +import { PermitApiError } from '../../api/base'; +import { IPermitConfig } from '../../config'; +import { LoggerFactory } from '../../logger'; + +// A token value that only this test uses. +const TOKEN = 'permit_key_exposure_test_0123456789abcdef'; + +interface PermitInternals { + logger: unknown; + enforcer: { + client: AxiosInstance; + logger: unknown; + contextStore: { add: (context: Record) => void }; + }; + checkAllTenants: ( + user: string, + action: string, + resource: string, + context?: Record, + ) => Promise>; + api: { users: { get: (key: string) => Promise } }; +} + +// A server on 127.0.0.1 that answers with `handler`, so the errors below are the ones the +// Node http adapter really builds, request objects included. +async function withServer( + handler: http.RequestListener, + run: (url: string) => Promise, +): Promise { + const server = http.createServer(handler); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + try { + await run(`http://127.0.0.1:${(server.address() as AddressInfo).port}`); + } finally { + await new Promise((resolve) => server.close(resolve)); + } +} + +// A 127.0.0.1 server that drops every connection before any response. It stays listening +// while `run` uses it, so no other listener can take the port in between. +async function withDroppingServer(run: (url: string) => Promise): Promise { + const server = http.createServer(); + server.on('connection', (socket) => socket.destroy()); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + try { + await run(`http://127.0.0.1:${(server.address() as AddressInfo).port}`); + } finally { + await new Promise((resolve) => server.close(resolve)); + } +} + +function assertNoToken(t: { false: (actual: boolean, message?: string) => void }, err: unknown) { + t.false(util.inspect(err, { depth: 10 }).includes(TOKEN), 'util.inspect'); + // Hidden (non-enumerable) properties too, as some inspection modes show them. + t.false(util.inspect(err, { depth: 10, showHidden: true }).includes(TOKEN), 'showHidden'); + t.false(JSON.stringify(err).includes(TOKEN), 'JSON.stringify'); + t.false(String(err).includes(TOKEN), 'String'); +} + +test('checkAllTenants sends the query in the body and the key only in the header', async (t) => { + const { Permit } = await import('../../index'); + const permit = new Permit({ token: TOKEN, timeout: 4321 }) as unknown as PermitInternals; + permit.enforcer.contextStore.add({ region: 'eu' }); + let sent: InternalAxiosRequestConfig | undefined; + permit.enforcer.client.defaults.adapter = async (config: InternalAxiosRequestConfig) => { + sent = config; + return { + status: 200, + statusText: 'OK', + headers: {}, + config, + data: { allowed_tenants: [{ tenant: { key: 'tenant-1', attributes: {} } }] }, + }; + }; + + const tenants = await permit.checkAllTenants('user-1', 'read', 'document', { ip: '1.2.3.4' }); + + t.deepEqual( + tenants.map((tenant) => tenant.key), + ['tenant-1'], + ); + t.truthy(sent); + t.is(axios.getUri(sent), 'http://localhost:7766/allowed/all-tenants'); + t.is(sent?.method, 'post'); + t.is(sent?.timeout, 4321); + t.is(AxiosHeaders.from(sent?.headers).get('Authorization'), `Bearer ${TOKEN}`); + const body = JSON.parse(String(sent?.data)); + t.deepEqual(body, { + user: { key: 'user-1' }, + action: 'read', + resource: { type: 'document' }, + // The context store's context, merged with the call's. + context: { region: 'eu', ip: '1.2.3.4' }, + }); + t.false(String(sent?.data).includes(TOKEN)); +}); + +for (const failure of ['an error status', 'a dropped connection']) { + test(`checkAllTenants rethrows ${failure} without the key, and logs it once`, async (t) => { + const { Permit } = await import('../../index'); + const lines: string[] = []; + const logger = LoggerFactory.createLogger( + { log: { level: 'info', label: 'test', json: false } } as IPermitConfig, + { write: (line: string) => lines.push(line) }, + ); + const check = async (pdp: string) => { + const permit = new Permit({ token: TOKEN, pdp }) as unknown as PermitInternals; + permit.logger = logger; + permit.enforcer.logger = logger; + const err = await t.throwsAsync(permit.checkAllTenants('user-1', 'read', 'document')); + // The error callers got before: the axios error itself. + t.true(axios.isAxiosError(err)); + assertNoToken(t, err); + + const logged = lines + .map((line) => JSON.parse(line)) + .filter((record) => record.msg === 'Error fetching all tenants'); + t.is(logged.length, 1); + if (failure === 'an error status') { + t.is(logged[0].err.status, 500); + } + t.truthy(logged[0].err.code); + t.false(lines.join('').includes(TOKEN)); + + // The redaction edits that failed request's config, not the client's: the next call + // still sends the key. + let authorization: string | undefined; + await withServer( + (req, res) => { + authorization = req.headers.authorization; + res.setHeader('Content-Type', 'application/json'); + res.end('{"allowed_tenants":[]}'); + }, + async (url) => { + permit.enforcer.client.defaults.baseURL = `${url}/`; + t.deepEqual(await permit.checkAllTenants('user-1', 'read', 'document'), []); + }, + ); + t.is(authorization, `Bearer ${TOKEN}`); + }; + if (failure === 'an error status') { + await withServer((_req, res) => { + res.statusCode = 500; + res.end('{}'); + }, check); + } else { + await withDroppingServer(check); + } + }); +} + +test('a PDP connection error names the PDP URL without its credentials', async (t) => { + const { Permit, PermitConnectionError } = await import('../../index'); + await withDroppingServer(async (url) => { + const { host } = new URL(url); + const permit = new Permit({ + token: TOKEN, + pdp: `http://pdp-user:pdp-password@${host}/prefix?sig=query-secret`, + throwOnError: true, + }); + + for (const call of [ + () => permit.check('user-1', 'read', 'document'), + () => permit.bulkCheck([{ user: 'user-1', action: 'read', resource: 'document' }]), + () => permit.getUserPermissions('user-1'), + ]) { + const err = await t.throwsAsync(call(), { instanceOf: PermitConnectionError }); + t.true(err.message.includes(`PDP is running at http://${host}/prefix and`)); + for (const secret of [TOKEN, 'pdp-user', 'pdp-password', 'query-secret']) { + t.false(err.message.includes(secret), secret); + } + } + }); +}); + +// A REST API that grants an environment-level scope, and fails the call that follows with +// `fail`. +function apiServer(fail: (res: http.ServerResponse) => void): http.RequestListener { + return (req, res) => { + if (req.url === '/v2/api-key/scope') { + res.setHeader('Content-Type', 'application/json'); + res.end( + JSON.stringify({ organization_id: 'org', project_id: 'proj', environment_id: 'env' }), + ); + return; + } + fail(res); + }; +} + +async function apiError(t: { true: (actual: boolean) => void }, apiUrl: string) { + const { Permit } = await import('../../index'); + const permit = new Permit({ token: TOKEN, apiUrl }) as unknown as PermitInternals; + let caught: unknown; + try { + await permit.api.users.get('user-1'); + } catch (err) { + caught = err; + } + t.true(caught instanceof PermitApiError); + return caught as PermitApiError; +} + +test('a real API error status neither prints nor keeps the key', async (t) => { + await withServer( + apiServer((res) => { + res.statusCode = 500; + res.setHeader('Content-Type', 'application/json'); + res.end('{"message":"boom"}'); + }), + async (apiUrl) => { + const error = await apiError(t, apiUrl); + + t.is(error.message, 'boom'); + t.is(error.response?.status, 500); + assertNoToken(t, error); + assertNoToken(t, error.originalError); + t.false(JSON.stringify(error.formattedAxiosError).includes(TOKEN)); + }, + ); +}); + +test('a network failure surfaces as a PermitApiError that says why, without the key', async (t) => { + await withServer( + // The connection drops before any response. + apiServer((res) => res.socket?.destroy()), + async (apiUrl) => { + const error = await apiError(t, apiUrl); + + t.is(error.response, undefined); + t.truthy(error.message); + t.is(error.message, error.originalError.message); + assertNoToken(t, error); + assertNoToken(t, error.originalError); + }, + ); +}); + +test('an API error neither prints nor keeps the key', (t) => { + const config = { + method: 'get', + url: 'https://api.example.com/v2/facts/p/e/users/u', + headers: new AxiosHeaders({ Authorization: `Bearer ${TOKEN}` }), + } as InternalAxiosRequestConfig; + const request = { _header: `GET /v2 HTTP/1.1\r\nAuthorization: Bearer ${TOKEN}\r\nHost: x\r\n` }; + const response = { + status: 500, + statusText: 'Error', + data: { message: 'boom' }, + headers: {}, + config, + request, + }; + const axiosError = new AxiosError( + 'failed', + 'ERR_BAD_RESPONSE', + config, + request, + response as never, + ); + + const error = new PermitApiError('boom', axiosError); + + t.false(util.inspect(error, { depth: 10 }).includes(TOKEN)); + t.false(util.inspect(error.originalError, { depth: 10 }).includes(TOKEN)); + t.false(JSON.stringify(error.formattedAxiosError).includes(TOKEN)); + t.false(request._header.includes(TOKEN)); + // Still there, only not enumerable, so printing the error doesn't print the axios error. + t.is(error.originalError, axiosError); + t.is(error.request, request); + t.is(error.response?.status, 500); + t.false(Object.prototype.propertyIsEnumerable.call(error, 'originalError')); + t.false(Object.prototype.propertyIsEnumerable.call(axiosError, 'request')); + t.false(Object.prototype.propertyIsEnumerable.call(axiosError.response, 'request')); + // An accessor, so even inspecting hidden properties doesn't print the request. + t.false(util.inspect(axiosError, { depth: 10, showHidden: true }).includes('GET /v2')); + axiosError.request = { replaced: true }; + t.deepEqual(axiosError.request, { replaced: true }); + // And writable, as a public field was before. + const other = new AxiosError('other'); + error.originalError = other; + t.is(error.originalError, other); + t.false(Object.prototype.propertyIsEnumerable.call(error, 'originalError')); +}); + +test('a PermitApiError without an API message falls back to the axios message', (t) => { + const error = new PermitApiError( + undefined as unknown as string, + new AxiosError('timeout of 1ms'), + ); + + t.is(error.message, 'timeout of 1ms'); +}); diff --git a/src/tests/unit/logger.spec.ts b/src/tests/unit/logger.spec.ts new file mode 100644 index 00000000..4f6e4fb6 --- /dev/null +++ b/src/tests/unit/logger.spec.ts @@ -0,0 +1,324 @@ +import test from 'ava'; +import axios, { + AxiosError, + AxiosHeaders, + AxiosInstance, + CreateAxiosDefaults, + InternalAxiosRequestConfig, +} from 'axios'; +import pino from 'pino'; + +import { IPermitConfig } from '../../config'; +import { LoggerFactory, serializeError } from '../../logger'; +import { AxiosLoggingInterceptor } from '../../utils/http-logger'; + +// A token value that only this test uses. +const TOKEN = 'permit_key_logger_test_0123456789abcdef'; + +function axiosErrorWithTheToken(): AxiosError { + const config = { + method: 'post', + url: 'http://user:password@pdp.example.com:7766/allowed?sig=query-secret', + headers: new AxiosHeaders({ Authorization: `Bearer ${TOKEN}` }), + } as InternalAxiosRequestConfig; + const request = { _header: `POST /allowed HTTP/1.1\r\nAuthorization: Bearer ${TOKEN}\r\n` }; + const response = { + status: 502, + statusText: 'Bad Gateway', + data: 'bad gateway', + headers: {}, + config, + }; + return new AxiosError('Request failed', 'ERR_BAD_RESPONSE', config, request, response as never); +} + +test('an axios error is logged without its config, request or response', (t) => { + const serialized = serializeError(axiosErrorWithTheToken()); + + const text = JSON.stringify(serialized); + for (const secret of [TOKEN, 'password', 'query-secret']) { + t.false(text.includes(secret), secret); + } + t.deepEqual(serialized, { + type: 'AxiosError', + message: 'Request failed', + code: 'ERR_BAD_RESPONSE', + status: 502, + method: 'POST', + url: 'http://pdp.example.com:7766/allowed', + }); +}); + +test('any other error is logged as pino logs it', (t) => { + const serialized = serializeError(new TypeError('boom')) as Record; + + t.is(serialized.type, 'TypeError'); + t.is(serialized.message, 'boom'); +}); + +// The SDK's logger, writing its JSON lines to an array instead of stdout. +function capturingLogger(level = 'info') { + const lines: string[] = []; + const logger = LoggerFactory.createLogger( + { log: { level, label: 'test', json: false } } as IPermitConfig, + { write: (line: string) => lines.push(line) }, + ); + return { logger, lines, records: () => lines.map((line) => JSON.parse(line)) }; +} + +test('the SDK logger serializes an axios error without its config, request or response', (t) => { + const { logger, lines, records } = capturingLogger(); + + logger.error({ err: axiosErrorWithTheToken() }, 'failed'); + + for (const secret of [TOKEN, 'password', 'query-secret']) { + t.false(lines.join('').includes(secret), secret); + } + t.deepEqual(records()[0].err, { + type: 'AxiosError', + message: 'Request failed', + code: 'ERR_BAD_RESPONSE', + status: 502, + method: 'POST', + url: 'http://pdp.example.com:7766/allowed', + }); +}); + +// One case per redact path, so each path is pinned: the logged object, and where the +// censored value ends up in the record. +for (const [path, logged, read] of [ + ['token', { token: TOKEN }, (r: any) => r.token], + ['*.token', { client: { token: TOKEN } }, (r: any) => r.client.token], + [ + 'headers.Authorization', + { headers: { Authorization: `Bearer ${TOKEN}` } }, + (r: any) => r.headers.Authorization, + ], + [ + 'headers.authorization', + { headers: { authorization: `Bearer ${TOKEN}` } }, + (r: any) => r.headers.authorization, + ], + [ + '*.headers.Authorization', + { request: { headers: { Authorization: `Bearer ${TOKEN}` } } }, + (r: any) => r.request.headers.Authorization, + ], + [ + '*.headers.authorization', + { request: { headers: { authorization: `Bearer ${TOKEN}` } } }, + (r: any) => r.request.headers.authorization, + ], +] as Array<[string, Record, (record: any) => unknown]>) { + test(`the SDK logger redacts ${path}`, (t) => { + const { logger, lines, records } = capturingLogger(); + + logger.info(logged); + + t.false(lines.join('').includes(TOKEN)); + t.is(read(records()[0]), '[REDACTED]'); + }); +} + +test('any other error keeps no URL credentials in its input field', (t) => { + let invalid: unknown; + try { + new URL('http://user:password@host:bad/path?sig=query-secret'); + } catch (err) { + invalid = err; + } + t.is((invalid as { code?: string }).code, 'ERR_INVALID_URL'); + const { logger, lines, records } = capturingLogger(); + + logger.error({ err: invalid }, 'failed'); + + for (const secret of ['user:', 'password', 'query-secret']) { + t.false(lines.join('').includes(secret), secret); + } + t.is(records()[0].err.input, 'http://host:bad/path'); + t.is(records()[0].err.code, 'ERR_INVALID_URL'); +}); + +test('the input redaction never writes to the logged value', (t) => { + // Not error-like (no message), so pino hands back this same object; frozen, so a write + // would throw. + const value = Object.freeze({ code: 'X', input: 'http://user:password@host:7766/path' }); + const { logger, lines, records } = capturingLogger(); + + t.notThrows(() => logger.error({ err: value }, 'failed')); + + t.is(value.input, 'http://user:password@host:7766/path'); + t.false(lines.join('').includes('password')); + t.is(records()[0].err.input, 'http://host:7766/path'); +}); + +test('the HTTP debug lines show a URL without its credentials or query', async (t) => { + const { logger, lines, records } = capturingLogger('debug'); + const client = axios.create(); + AxiosLoggingInterceptor.setupInterceptor(client, logger); + client.defaults.adapter = async (config: InternalAxiosRequestConfig) => ({ + status: 200, + statusText: 'OK', + headers: {}, + config, + data: {}, + }); + + await client.get('http://user:password@pdp.example.com:7766/allowed?sig=query-secret'); + + for (const secret of ['user', 'password', 'query-secret']) { + t.false(lines.join('').includes(secret), secret); + } + t.deepEqual( + records().map((record) => record.msg), + [ + 'Sending HTTP request: GET http://pdp.example.com:7766/allowed', + 'Received HTTP response: GET http://pdp.example.com:7766/allowed, status: 200', + ], + ); +}); + +test('the HTTP debug lines show the URL a relative path goes to, an @ in its path kept', async (t) => { + const { logger, lines, records } = capturingLogger('debug'); + const client = axios.create({ baseURL: 'http://user:password@pdp.example.com:7766/' }); + AxiosLoggingInterceptor.setupInterceptor(client, logger); + client.defaults.adapter = async (config: InternalAxiosRequestConfig) => ({ + status: 200, + statusText: 'OK', + headers: {}, + config, + data: {}, + }); + + await client.get('/users/a@example.com/x', { params: { sig: 'query-secret' } }); + + for (const secret of ['user:', 'password', 'query-secret']) { + t.false(lines.join('').includes(secret), secret); + } + t.deepEqual( + records().map((record) => record.msg), + [ + 'Sending HTTP request: GET http://pdp.example.com:7766/users/a@example.com/x', + 'Received HTTP response: GET http://pdp.example.com:7766/users/a@example.com/x, status: 200', + ], + ); +}); + +function stubbedClient(logger: pino.Logger, config: CreateAxiosDefaults = {}): AxiosInstance { + const client = axios.create(config); + AxiosLoggingInterceptor.setupInterceptor(client, logger); + client.defaults.adapter = async (sent: InternalAxiosRequestConfig) => ({ + status: 200, + statusText: 'OK', + headers: {}, + config: sent, + data: {}, + }); + return client; +} + +test('the HTTP debug lines show config.url, redacted, when getUri throws', async (t) => { + const { logger, records } = capturingLogger('debug'); + const client = stubbedClient(logger); + client.getUri = () => { + throw new Error('getUri'); + }; + + await client.get('http://user:password@pdp.example.com:7766/allowed?sig=query-secret'); + + t.deepEqual( + records().map((record) => record.msg), + [ + 'Sending HTTP request: GET http://pdp.example.com:7766/allowed', + 'Received HTTP response: GET http://pdp.example.com:7766/allowed, status: 200', + ], + ); +}); + +test('the HTTP debug lines build their URL without the params', async (t) => { + const { logger, records } = capturingLogger('debug'); + let serialized = 0; + const client = stubbedClient(logger, { + params: { sig: 'query-secret' }, + paramsSerializer: { + serialize: () => { + serialized += 1; + return 'sig=query-secret'; + }, + }, + }); + + await client.get('http://pdp.example.com:7766/allowed'); + + // The stub adapter never builds the URL, so any call is the debug lines'. + t.is(serialized, 0); + t.is(records().length, 2); +}); + +test('a request with neither url nor baseURL logs an empty URL', async (t) => { + const { logger, records } = capturingLogger('debug'); + const client = stubbedClient(logger); + + await client.request({ method: 'get' }); + + t.deepEqual( + records().map((record) => record.msg), + ['Sending HTTP request: GET ', 'Received HTTP response: GET , status: 200'], + ); +}); + +for (const level of ['warn', 'debug']) { + test(`a url given as a URL object is sent and logged (level ${level})`, async (t) => { + const { logger, records } = capturingLogger(level); + const client = stubbedClient(logger); + + const response = await client.get( + new URL('http://user:password@pdp.example.com:7766/allowed') as unknown as string, + ); + + t.is(response.status, 200); + t.deepEqual( + records().map((record) => record.msg), + level === 'debug' + ? [ + 'Sending HTTP request: GET http://pdp.example.com:7766/allowed', + 'Received HTTP response: GET http://pdp.example.com:7766/allowed, status: 200', + ] + : [], + ); + }); +} + +test('the HTTP lines are not built when debug is off', async (t) => { + const { logger } = capturingLogger('info'); + const client = stubbedClient(logger); + let built = 0; + client.getUri = () => { + built += 1; + return 'http://pdp.example.com:7766/allowed'; + }; + + await client.get('http://pdp.example.com:7766/allowed'); + + t.is(built, 0); +}); + +for (const [name, url, expected] of [ + [ + 'a URL object', + new URL('http://user:password@pdp.example.com:7766/allowed?sig=query-secret'), + 'http://pdp.example.com:7766/allowed', + ], + ['a number', 42, '42'], + ['null', null, undefined], +] as const) { + test(`an axios error whose config.url is ${name} is still logged`, (t) => { + const error = axiosErrorWithTheToken(); + (error.config as { url?: unknown }).url = url; + + const serialized = serializeError(error) as Record; + + t.is(serialized.url, expected); + t.false(JSON.stringify(serialized).includes('password')); + }); +} diff --git a/src/tests/unit/redact-url.spec.ts b/src/tests/unit/redact-url.spec.ts new file mode 100644 index 00000000..48076eb4 --- /dev/null +++ b/src/tests/unit/redact-url.spec.ts @@ -0,0 +1,47 @@ +import test from 'ava'; + +import { redactUrl } from '../../utils/redact-url'; + +for (const [url, expected] of [ + ['http://localhost:7766', 'http://localhost:7766'], + ['https://api.example.com/v2/facts?page=2#top', 'https://api.example.com/v2/facts'], + ['http://user:password@pdp.example.com:7766/path?sig=secret', 'http://pdp.example.com:7766/path'], + // The parser lowercases the scheme and host. + ['HTTP://User:Password@Host:7766', 'http://host:7766'], + ['http://user:password@[::1]:7766/x', 'http://[::1]:7766/x'], + // A '?' or '#' in the password doesn't stop the user info being removed. + ['http://user:pa#ss@host:7766', 'http://host:7766'], + ['http://user:pa?ss@host:7766/path', 'http://host:7766/path'], + ['http://user:p@ss@host:7766', 'http://host:7766'], + // An '@' after the host belongs to the path or query, not to user info. + ['http://host:7766/users/a@example.com', 'http://host:7766/users/a@example.com'], + ['http://host:7766/path?email=a@example.com', 'http://host:7766/path'], + // An '@' in the query or fragment of a URL without a path isn't user info. + ['http://host:7766?a=b@c', 'http://host:7766'], + ['http://pdp?sig=a@b/c', 'http://pdp'], + ['http://host#x@y', 'http://host'], + // A '/' in the password: not a valid URL, so everything up to the last '@' goes. + ['http://user:pa/ss@host:7766', 'http://host:7766'], + // What Node's URL parser accepts: surrounding whitespace, and one slash after the scheme. + [' http://user:pass@pdp:7766\n', 'http://pdp:7766'], + ['http:/user:pass@pdp:7766', 'http://pdp:7766'], + // What the parser cleans up before it parses: a tab or newline inside the text, a '\\' + // read as '/'. axios still sends these with the user info, so it is removed. + ['ht\ttp://user:pass@pdp:7766', 'http://pdp:7766'], + ['http://pdp:7766/a\nb', 'http://pdp:7766/ab'], + ['http:\\\\user:pass@pdp:7766\\path', 'http://pdp:7766/path'], + // Not an http(s) URL to the parser, and a '/' before the '@': everything up to the last '@'. + ['http//user:pass@pdp:7766', 'pdp:7766'], + ['http//user:p@ss@pdp:7766', 'pdp:7766'], + ['htp:/x/user:pass@pdp:7766/path', 'htp:/pdp:7766/path'], + ['localhost:7766/a/user:pass@pdp', 'pdp'], + // Not a URL, but recognisable. + ['localhost:7766', 'localhost:7766'], + ['user:password@localhost:7766', 'localhost:7766'], + ['user:p@ss@localhost:7766/path?x=y', 'localhost:7766/path'], + ['', ''], +]) { + test(`redactUrl(${JSON.stringify(url)})`, (t) => { + t.is(redactUrl(url), expected); + }); +} diff --git a/src/utils/http-logger.ts b/src/utils/http-logger.ts index 8606cbfb..4b8acadb 100644 --- a/src/utils/http-logger.ts +++ b/src/utils/http-logger.ts @@ -1,13 +1,44 @@ import { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'; import pino from 'pino'; +import { redactUrl } from './redact-url'; + +// The URL a request goes to, `baseURL` included: a relative `url` on its own isn't a URL, so +// redactUrl would cut it at its last '@' (e.g. `/users/a@example.com/x`). Built without +// `params` (`null`: `undefined` falls back to the instance's), which the line doesn't show, +// so an app's `paramsSerializer` doesn't run for it. Whatever `getUri` returns, its type +// aside, is made a string: undefined for a request with neither `url` nor `baseURL`, and a +// `url` given as a `URL` object as it is. +function requestedUrl( + axiosInstance: AxiosInstance, + config: InternalAxiosRequestConfig | undefined, +): string { + if (config === undefined) { + return ''; + } + let uri: unknown; + try { + uri = axiosInstance.getUri({ ...config, params: null }); + } catch { + uri = config.url; + } + return uri == null ? '' : String(uri); +} + export class AxiosLoggingInterceptor { static setupInterceptor(axiosInstance: AxiosInstance, logger: pino.Logger): void { // Add a request interceptor axiosInstance.interceptors.request.use( function (request: InternalAxiosRequestConfig) { request.headers = request.headers || {}; // Ensure headers are not undefined - logger.debug(`Sending HTTP request: ${request?.method?.toUpperCase()} ${request?.url}`); + // Built only when it's logged, so a request at another level pays nothing for it. + if (logger.isLevelEnabled('debug')) { + logger.debug( + `Sending HTTP request: ${request?.method?.toUpperCase()} ${redactUrl( + requestedUrl(axiosInstance, request), + )}`, + ); + } return request; }, function (error: any) { @@ -19,11 +50,13 @@ export class AxiosLoggingInterceptor { // Add a response interceptor axiosInstance.interceptors.response.use( function (response: AxiosResponse) { - logger.debug( - `Received HTTP response: ${response?.config?.method?.toUpperCase()} ${ - response?.config?.url - }, status: ${response?.status}`, - ); + if (logger.isLevelEnabled('debug')) { + logger.debug( + `Received HTTP response: ${response?.config?.method?.toUpperCase()} ${redactUrl( + requestedUrl(axiosInstance, response?.config), + )}, status: ${response?.status}`, + ); + } return response; }, function (error) { diff --git a/src/utils/redact-url.ts b/src/utils/redact-url.ts new file mode 100644 index 00000000..873119c4 --- /dev/null +++ b/src/utils/redact-url.ts @@ -0,0 +1,24 @@ +// A URL fit for a log line or an error message, without the user, password, query or +// fragment, since any of them can carry a credential. An http(s) URL is rebuilt from what +// Node's URL parser reads (scheme, host, port, path); anything else, such as +// `localhost:7766`, loses everything up to its last '@', then its query and fragment. +export function redactUrl(url: string): string { + const trimmed = url.trim(); + try { + // An http(s) URL, the only kind axios requests, is rebuilt from what Node's URL parser + // (which axios uses) reads, after its own clean-up of the text: where requests go, without + // the user info, query or fragment. + const parsed = new URL(trimmed); + if (parsed.protocol === 'http:' || parsed.protocol === 'https:') { + const path = parsed.pathname === '/' ? '' : parsed.pathname; + return `${parsed.protocol}//${parsed.host}${path}`; + } + } catch { + // Not a URL the parser accepts: handled below. + } + // Anything else removes more rather than less: everything up to the last '@', then the + // query and fragment. A scheme in front is kept, so the value stays recognisable. + const scheme = /^[a-z][a-z0-9+.-]*:\/+/i.exec(trimmed)?.[0] ?? ''; + const rest = trimmed.slice(scheme.length); + return scheme + rest.slice(rest.lastIndexOf('@') + 1).split(/[?#]/, 1)[0]; +}