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(logs/azure): redact sensitive header when DEBUG is set #1218

Merged
merged 19 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 10 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
21 changes: 20 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,27 @@
}

export function debug(action: string, ...args: any[]) {
const sensitiveHeaders = ["authorization", "api-key"];

Check failure on line 1142 in src/core.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"authorization",·"api-key"` with `'authorization',·'api-key'`
if (typeof process !== 'undefined' && process?.env?.['DEBUG'] === 'true') {
console.log(`OpenAI:DEBUG:${action}`, ...args);
for (const arg of args) {
if (!arg) {
break;
}
// Check for sensitive headers in request body "headers"
if (arg["headers"]) {

Check failure on line 1149 in src/core.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"headers"` with `'headers'`
for (const header in arg["headers"]){

Check failure on line 1150 in src/core.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"headers"])` with `'headers'])·`
if (sensitiveHeaders.includes(header.toLowerCase())){

Check failure on line 1151 in src/core.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
arg["headers"][header] = "REDACTED";

Check failure on line 1152 in src/core.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"headers"][header]·=·"REDACTED"` with `'headers'][header]·=·'REDACTED'`
}
}
}
// Check for sensitive headers in headers object
for (const header in arg){

Check failure on line 1157 in src/core.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
if (sensitiveHeaders.includes(header.toLowerCase())){

Check failure on line 1158 in src/core.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
arg[header] = "REDACTED";

Check failure on line 1159 in src/core.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"REDACTED"` with `'REDACTED'`
}
}
}
}
}

Expand Down
48 changes: 48 additions & 0 deletions tests/qs/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { debug } from 'openai/core';
import { combine, merge, is_buffer, assign_single_source } from 'openai/internal/qs/utils';

describe('merge()', function () {
Expand Down Expand Up @@ -167,3 +168,50 @@
// t.equal(is_buffer(buffer), true, 'real Buffer instance is a buffer');
expect(is_buffer(buffer)).toEqual(true);
});

test("debug()", function(){

Check failure on line 172 in tests/qs/utils.test.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"debug()",·function()` with `'debug()',·function·()·`
const originalDebugValue = process.env["DEBUG"];

Check failure on line 173 in tests/qs/utils.test.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"DEBUG"` with `'DEBUG'`

const spy = jest.spyOn(console, "log");

// Debug enabled
process.env["DEBUG"]= "true";

// Test request body includes headers object with Authorization
const headersTest = {
headers: {
Authorization: "fakeAuthorization"
}
}
debug("request", headersTest);
expect(spy).toHaveBeenCalledWith("OpenAI:DEBUG:request", {
headers: {
Authorization: "REDACTED"
}
});

// Test request body includes headers object with api-ley
const apiKeyTest = {
headers: {
"api-key": "fakeKey"
}
}
debug("request", apiKeyTest);
expect(spy).toHaveBeenCalledWith("OpenAI:DEBUG:request", {
headers: {
"api-key": "REDACTED"
}
});

// Test headers object with authorization header
const authorizationTest = {
authorization: "fakeValue"
}
debug("request", authorizationTest);
expect(spy).toHaveBeenCalledWith("OpenAI:DEBUG:request", {
authorization: "REDACTED"
});

process.env["DEBUG"] = originalDebugValue;

})
Loading