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: Prevent Auth Logging by Default #565

Merged
merged 9 commits into from
Aug 11, 2023
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
> An HTTP request client that provides an `axios` like interface over top of `node-fetch`.

## Install

```sh
$ npm install gaxios
```
Expand All @@ -16,12 +17,13 @@ $ npm install gaxios
```js
const {request} = require('gaxios');
const res = await request({
url: 'https://www.googleapis.com/discovery/v1/apis/'
url: 'https://www.googleapis.com/discovery/v1/apis/',
});
```

## Setting Defaults
Gaxios supports setting default properties both on the default instance, and on additional instances. This is often useful when making many requests to the same domain with the same base settings. For example:

Gaxios supports setting default properties both on the default instance, and on additional instances. This is often useful when making many requests to the same domain with the same base settings. For example:

```js
const gaxios = require('gaxios');
Expand Down Expand Up @@ -86,7 +88,7 @@ over other authentication methods, i.e., application default credentials.
return qs.stringify(params);
},

// The timeout for the HTTP request. Defaults to 0.
// The timeout for the HTTP request in milliseconds. Defaults to 0.
timeout: 1000,

// Optional method to override making the actual HTTP request. Useful
Expand Down Expand Up @@ -152,8 +154,18 @@ over other authentication methods, i.e., application default credentials.
// Cancelling a request requires the `abort-controller` library.
// See https://github.com/bitinn/node-fetch#request-cancellation-with-abortsignal
signal?: AbortSignal

/**
* An experimental, customizable error redactor.
*
* Set `false` to disable.
*
* @experimental
*/
errorRedactor?: typeof defaultErrorRedactor | false;
}
```

## License

[Apache-2.0](https://github.com/googleapis/gaxios/blob/master/LICENSE)
146 changes: 137 additions & 9 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,50 @@ export class GaxiosError<T = any> extends Error {
* 'ECONNRESET'
*/
code?: string;
response?: GaxiosResponse<T>;
config: GaxiosOptions;
/**
* An HTTP Status code.
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/Response/status Response: status property}
*
* @example
* 500
*/
status: number;
status?: number;

constructor(
message: string,
options: GaxiosOptions,
response: GaxiosResponse<T>,
public config: GaxiosOptions,
public response?: GaxiosResponse<T>,
public error?: Error | NodeJS.ErrnoException
) {
super(message);
this.response = response;
this.config = options;
this.response.data = translateData(options.responseType, response.data);

if (this.response) {
this.response.data = translateData(config.responseType, response?.data);
this.status = this.response.status;
}

if (error && 'code' in error && error.code) {
this.code = error.code;
}
this.status = response.status;

if (config.errorRedactor) {
const errorRedactor = config.errorRedactor<T>;

// shallow-copy config for redaction as we do not want
// future requests to have redacted information
this.config = {...config};
if (this.response) {
// copy response's config, as it may be recursively redacted
this.response = {...this.response, config: {...this.response.config}};
}

const results = errorRedactor({config, response});
this.config = {...config, ...results.config};

if (this.response) {
this.response = {...this.response, ...results.response, config};
}
}
}
}

Expand Down Expand Up @@ -138,7 +158,31 @@ export interface GaxiosOptions {
// Configure client to use mTLS:
cert?: string;
key?: string;
/**
* An experimental error redactor.
*
* @experimental
*/
errorRedactor?: typeof defaultErrorRedactor | false;
}
/**
* A partial object of `GaxiosResponse` with only redactable keys
*
* @experimental
*/
export type RedactableGaxiosOptions = Pick<
GaxiosOptions,
'body' | 'data' | 'headers' | 'url'
>;
/**
* A partial object of `GaxiosResponse` with only redactable keys
*
* @experimental
*/
export type RedactableGaxiosResponse<T = any> = Pick<
GaxiosResponse<T>,
'config' | 'data' | 'headers'
>;

/**
* Configuration for the Gaxios `request` method.
Expand Down Expand Up @@ -243,3 +287,87 @@ function translateData(responseType: string | undefined, data: any) {
return data;
}
}

/**
* An experimental error redactor.
*
* @param config Config to potentially redact properties of
* @param response Config to potentially redact properties of
*
* @experimental
*/
export function defaultErrorRedactor<T = any>(data: {
config?: RedactableGaxiosOptions;
response?: RedactableGaxiosResponse<T>;
}) {
const REDACT =
'<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.';

function redactHeaders(headers?: Headers) {
if (!headers) return;

for (const key of Object.keys(headers)) {
// any casing of `Authentication`
if (/^authentication$/.test(key)) {
headers[key] = REDACT;
}
}
}

function redactString(obj: GaxiosOptions, key: keyof GaxiosOptions) {
if (
typeof obj === 'object' &&
obj !== null &&
typeof obj[key] === 'string'
) {
const text = obj[key];

if (/grant_type=/.test(text) || /assertion=/.test(text)) {
obj[key] = REDACT;
danielbankhead marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

function redactObject<T extends GaxiosOptions['data']>(obj: T) {
if (typeof obj === 'object' && obj !== null) {
if ('grant_type' in obj) {
obj['grant_type'] = REDACT;
}

if ('assertion' in obj) {
obj['assertion'] = REDACT;
}
}
}

if (data.config) {
redactHeaders(data.config.headers);

redactString(data.config, 'data');
redactObject(data.config.data);

redactString(data.config, 'body');
redactObject(data.config.body);

try {
const url = new URL(data.config.url || '');
if (url.searchParams.has('token')) {
url.searchParams.set('token', REDACT);
}

data.config.url = url.toString();
} catch {
// ignore error
}
}

if (data.response) {
defaultErrorRedactor({config: data.response.config});
redactHeaders(data.response.headers);

redactString(data.response, 'data');
redactObject(data.response.data);
}

return data;
}
20 changes: 16 additions & 4 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
GaxiosPromise,
GaxiosResponse,
Headers,
defaultErrorRedactor,
} from './common';
import {getRetryConfig} from './retry';
import {Stream} from 'stream';
Expand Down Expand Up @@ -156,14 +157,15 @@ export class Gaxios {
} else {
translatedResponse = await this._defaultAdapter(opts);
}

if (!opts.validateStatus!(translatedResponse.status)) {
if (opts.responseType === 'stream') {
let response = '';
await new Promise(resolve => {
(translatedResponse.data as Stream).on('data', chunk => {
(translatedResponse?.data as Stream).on('data', chunk => {
response += chunk;
});
(translatedResponse.data as Stream).on('end', resolve);
(translatedResponse?.data as Stream).on('end', resolve);
});
translatedResponse.data = response as T;
}
Expand All @@ -175,8 +177,11 @@ export class Gaxios {
}
return translatedResponse;
} catch (e) {
const err = e as GaxiosError;
err.config = opts;
const err =
e instanceof GaxiosError
? e
: new GaxiosError((e as Error).message, opts, undefined, e as Error);

const {shouldRetry, config} = await getRetryConfig(err);
if (shouldRetry && config) {
err.config.retryConfig!.currentRetryAttempt =
Expand Down Expand Up @@ -324,6 +329,13 @@ export class Gaxios {
}
}

if (
typeof opts.errorRedactor !== 'function' &&
opts.errorRedactor !== false
) {
opts.errorRedactor = defaultErrorRedactor;
}

return opts;
}

Expand Down
2 changes: 1 addition & 1 deletion src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function shouldRetryRequest(err: GaxiosError) {

// node-fetch raises an AbortError if signaled:
// https://github.com/bitinn/node-fetch#request-cancellation-with-abortsignal
if (err.name === 'AbortError') {
if (err.name === 'AbortError' || err.error?.name === 'AbortError') {
return false;
}

Expand Down
Loading