diff --git a/src/request.spec.ts b/src/request.spec.ts index 5290a534cd4..b9823d33a34 100644 --- a/src/request.spec.ts +++ b/src/request.spec.ts @@ -625,12 +625,17 @@ describe('Request', () => { assert((auth.ensureAccessToken as sinon.SinonStub).calledWith('https://graph.microsoft.com')); }); - it('sends no authorization header when routing through the external Graph base URL', async () => { + it('keeps the authorization header when routing through the external Graph base URL', async () => { + // The external token is the caller's credential toward the proxy (an + // OpenShell placeholder or the inter-VM bearer), so the rewritten request + // must still carry it: the proxy authenticates the caller with it. process.env.CLIMICROSOFT365_ACCESS_TOKEN = 'inter-vm-bearer'; process.env.CLIMICROSOFT365_GRAPH_BASE_URL = 'http://127.0.0.1:18080'; auth.connection.cloudType = CloudType.Public; + let actualUrl = ''; let actualHeaders: any; sinon.stub(_request as any, 'req').callsFake((requestOptions: any) => { + actualUrl = requestOptions.url as string; actualHeaders = requestOptions.headers; return { data: {} }; }); @@ -640,7 +645,8 @@ describe('Request', () => { headers: {} }); - assert.strictEqual(actualHeaders.authorization, undefined); + assert.strictEqual(actualUrl, 'http://127.0.0.1:18080/v1.0/me/messages?$top=10'); + assert.strictEqual(actualHeaders.authorization, 'Bearer ABC'); }); it('routes a headerless Graph request through the external Graph base URL', async () => { diff --git a/src/request.ts b/src/request.ts index 8c850c7fa6c..4b78552a4bc 100644 --- a/src/request.ts +++ b/src/request.ts @@ -260,12 +260,15 @@ class Request { options.url = `${proxyUrl.origin}${requestUrl.pathname}${requestUrl.search}${requestUrl.hash}`; - // The proxy owns the caller's credential; whatever token this process - // holds must never ride toward it. Strip the header at the same moment - // the destination changes, so no request can carry both. - if (options.headers) { - delete options.headers.authorization; - } + // The authorization header stays on the rewritten request. Under the + // governed deployment CLIMICROSOFT365_ACCESS_TOKEN is not a Graph token + // but the caller's credential toward the proxy itself (an OpenShell + // provider placeholder that the sandbox supervisor substitutes on the + // inspected hop, or the inter-VM bearer the proxy authenticates). The + // proxy holds the real Graph credential and only ever sees this + // process's value as its own front-door bearer, so stripping it here + // produced 401s from the proxy for every built-in command while + // `m365 request --url /...` kept working. } private updateRequestForCloudType(options: AxiosRequestConfig, cloudType: CloudType): void {