From 44647680d9976e7da84a76c5ac86de28ff613347 Mon Sep 17 00:00:00 2001 From: "Jason T. Greene" Date: Sat, 5 Sep 2026 00:48:21 -0500 Subject: [PATCH] Keep the caller's bearer when routing Graph requests through the proxy Since 6564c4a7, rewriting a graph.microsoft.com URL onto CLIMICROSOFT365_GRAPH_BASE_URL also deleted the authorization header. The external Graph proxy authenticates its caller with exactly that header, so every built-in command failed with the proxy's 401 while `m365 request --url /...` kept working because it never passes through the rewrite. The strip assumed an intermediary that attached the proxy credential on the way out. In the deployment this fork serves there is none: CLIMICROSOFT365_ACCESS_TOKEN is the caller's own credential toward the proxy (a governed placeholder substituted in transit, or the proxy's static bearer). This process never holds a Graph token, and the value it holds goes nowhere but the proxy. Keep it on the rewritten request; the spec that pinned the strip now asserts the header survives alongside the rewritten URL. Co-Authored-By: Claude Opus 5 --- src/request.spec.ts | 10 ++++++++-- src/request.ts | 15 +++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) 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 {