Skip to content
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
9 changes: 6 additions & 3 deletions containers/api-proxy/providers/copilot.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ const { URL } = require('url');
// it means the sidecar received a dummy key (not a real BYOK credential) and should
// fall back to COPILOT_GITHUB_TOKEN as the sole auth source.
const COPILOT_PLACEHOLDER_TOKEN = 'ghu_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
const COPILOT_DUMMY_BYOK_KEY = 'dummy-byok-key-for-offline-mode';

/**
* Strip any accidental "Bearer " prefix from a raw credential value and trim
* Strip any accidental "Bearer " or "token " prefix from a raw credential
* value and trim
* surrounding whitespace. Returns undefined when the result is empty so that
* callers can use `|| undefined` fall-through cleanly.
*
Expand All @@ -45,7 +47,7 @@ const COPILOT_PLACEHOLDER_TOKEN = 'ghu_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
* @returns {string|undefined}
*/
function stripBearerPrefix(value) {
return ((value || '').replace(/^\s*Bearer\s+/i, '').trim()) || undefined;
return ((value || '').replace(/^\s*(?:Bearer|token)\s+/i, '').trim()) || undefined;
}
Comment on lines 49 to 51

/**
Expand All @@ -60,7 +62,7 @@ function stripBearerPrefix(value) {
*/
function resolveApiKey(env) {
const key = stripBearerPrefix(env.COPILOT_API_KEY);
return key === COPILOT_PLACEHOLDER_TOKEN ? undefined : key;
return key === COPILOT_PLACEHOLDER_TOKEN || key === COPILOT_DUMMY_BYOK_KEY ? undefined : key;
}
Comment on lines 63 to 66

/**
Expand Down Expand Up @@ -367,5 +369,6 @@ module.exports = {
deriveGitHubApiTarget,
deriveGitHubApiBasePath,
COPILOT_PLACEHOLDER_TOKEN,
COPILOT_DUMMY_BYOK_KEY,
},
};
24 changes: 23 additions & 1 deletion containers/api-proxy/server.auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

const { shouldStripHeader } = require('./proxy-utils');
const {
_testing: { resolveCopilotAuthToken, resolveApiKey, stripBearerPrefix, COPILOT_PLACEHOLDER_TOKEN },
_testing: {
resolveCopilotAuthToken,
resolveApiKey,
stripBearerPrefix,
COPILOT_PLACEHOLDER_TOKEN,
COPILOT_DUMMY_BYOK_KEY,
},
createCopilotAdapter,
} = require('./providers/copilot');
const { sanitizeNullToolCallTypes } = require('./body-transform');
Expand Down Expand Up @@ -55,6 +61,11 @@ describe('stripBearerPrefix', () => {
expect(stripBearerPrefix('BEARER sk-or-v1-abc')).toBe('sk-or-v1-abc');
});

it('strips "token " prefix case-insensitively', () => {
expect(stripBearerPrefix('token sk-or-v1-abc')).toBe('sk-or-v1-abc');
expect(stripBearerPrefix('TOKEN sk-or-v1-abc')).toBe('sk-or-v1-abc');
});

it('strips leading whitespace before "Bearer "', () => {
expect(stripBearerPrefix(' Bearer sk-or-v1-abc')).toBe('sk-or-v1-abc');
});
Expand Down Expand Up @@ -155,6 +166,13 @@ describe('resolveCopilotAuthToken', () => {
COPILOT_API_KEY: COPILOT_PLACEHOLDER_TOKEN,
})).toBe('gho_real_token');
});

it('uses COPILOT_GITHUB_TOKEN when COPILOT_API_KEY is the offline BYOK dummy key', () => {
expect(resolveCopilotAuthToken({
COPILOT_GITHUB_TOKEN: 'gho_real_token',
COPILOT_API_KEY: COPILOT_DUMMY_BYOK_KEY,
})).toBe('gho_real_token');
});
});

describe('resolveApiKey', () => {
Expand All @@ -166,6 +184,10 @@ describe('resolveApiKey', () => {
expect(resolveApiKey({ COPILOT_API_KEY: COPILOT_PLACEHOLDER_TOKEN })).toBeUndefined();
});

it('returns undefined when COPILOT_API_KEY is the offline BYOK dummy key', () => {
expect(resolveApiKey({ COPILOT_API_KEY: COPILOT_DUMMY_BYOK_KEY })).toBeUndefined();
});

it('returns undefined when COPILOT_API_KEY is not set', () => {
expect(resolveApiKey({})).toBeUndefined();
});
Expand Down
Loading