Skip to content

Commit 8a34baf

Browse files
committed
fix(http): keep the fetch collector out of production bundles
providePangularHttp() gated its early fetch() watcher on a runtime read of globalThis.ngDevMode, which a bundler cannot fold, so the fetch collector and its list of devtools paths (/__devframes/) stayed in every production build and the security e2e flagged overlay code in the bundle. Gate it on a bare ngDevMode, which production builds replace with false, so the whole branch and the collector are dropped.
1 parent 8ef013b commit 8a34baf

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎packages/devtools/src/__tests__/fetch-collector.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { requestOf, watchNativeFetch } from '../fetch-collector.ts';
88
import { notePendingClientRequest } from '../http-record.ts';
99
import { httpRegistry, sanitizeCalls } from '../http-rules.ts';
1010
import { readHeaders, withCapturedHeaders } from '../http-headers.ts';
11-
import { withPangular } from '../http.ts';
11+
import { providePangularHttp, withPangular } from '../http.ts';
1212
import { angularAtLeast, initAngularTestBed, zonelessProviders } from './angular-version.ts';
1313

1414
const g = globalThis as { fetch?: typeof fetch; __PANGULAR_HTTP__?: unknown };
@@ -170,6 +170,31 @@ describe('watchNativeFetch', () => {
170170
describe.runIf(angularAtLeast('19.0'))('native fetch with HttpClient', () => {
171171
afterEach(() => TestBed.resetTestingModule());
172172

173+
it('wraps fetch() from bootstrap only in dev mode', () => {
174+
const dev = globalThis as { ngDevMode?: unknown };
175+
const saved = dev.ngDevMode;
176+
const stub = stubFetch(async () => new Response(''));
177+
initAngularTestBed();
178+
try {
179+
dev.ngDevMode = false;
180+
TestBed.configureTestingModule({
181+
providers: [...zonelessProviders(), providePangularHttp()],
182+
});
183+
TestBed.inject(HttpClient, null);
184+
expect(g.fetch).toBe(stub);
185+
TestBed.resetTestingModule();
186+
dev.ngDevMode = true;
187+
TestBed.configureTestingModule({
188+
providers: [...zonelessProviders(), providePangularHttp()],
189+
});
190+
TestBed.inject(HttpClient, null);
191+
expect(g.fetch).not.toBe(stub);
192+
expect(httpRegistry().nativeFetch).toBe(stub);
193+
} finally {
194+
dev.ngDevMode = saved;
195+
}
196+
});
197+
173198
it('records an HttpClient request on withFetch() once, as an HttpClient call', async () => {
174199
(globalThis as { ngDevMode?: unknown }).ngDevMode = true;
175200
const stub = stubFetch(

‎packages/devtools/src/http.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ import { redirectOf } from './http-redirect.ts';
6666
import { claimTransferEntry, mockContentType, parseBody } from './http-internals.ts';
6767
export { decodePayload, type PayloadEntry, type PayloadSummary } from './http-payload.ts';
6868

69+
declare const ngDevMode: unknown;
70+
6971
const MAX_WARNINGS = 50;
7072
const PREVIEW_CHARS = 2000;
7173

@@ -446,8 +448,11 @@ export function providePangularHttp(): EnvironmentProviders {
446448

447449
/** Records the page's own `fetch()` calls from bootstrap on, before the overlay connects. */
448450
function watchNativeFetchEarly() {
451+
// A bare `ngDevMode`, which a production build replaces with false, so the
452+
// fetch collector and the devtools paths it knows are dropped from the app.
453+
if (typeof ngDevMode === 'undefined' || !ngDevMode) return;
449454
const platform = inject(PLATFORM_ID, { optional: true });
450-
if (!devMode() || !platform || isPlatformServer(platform)) return;
455+
if (!platform || isPlatformServer(platform)) return;
451456
watchNativeFetch();
452457
}
453458

0 commit comments

Comments
 (0)