Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
fix(frontend): tag apollo titles with a Symbol marker so prod detects them
Sergey Kozyrenko1903e8fc0f6e
diff --git a/frontend/src/components/shared/document-title.test.tsx b/frontend/src/components/shared/document-title.test.tsxindex 4d1675dc..a9acef29 100644--- a/frontend/src/components/shared/document-title.test.tsx+++ b/frontend/src/components/shared/document-title.test.tsx@@ -2,6 +2,8 @@ import { render, waitFor } from '@testing-library/react';import { createMemoryRouter, Outlet, RouterProvider } from 'react-router-dom';import { describe, expect, it } from 'vitest';+import { apolloTitle } from '@/lib/route-titles/apollo-title';+import { DocumentTitle } from './document-title';const renderAt = (initialPath: string, routes: Parameters<typeof createMemoryRouter>[0]) => {@@ -100,13 +102,16 @@ describe('DocumentTitle', () => {await waitFor(() => expect(document.title).toBe('API tokens — PentAGI'));});- it('renders a title from a PascalCase-named component', async () => {- // PascalCase-named function = component (apolloTitle returns one).- function CustomTitle({ params }: { params: Record<string, string | undefined> }) {- // React 19 only hoists <title> when the child is a single string,- // so compute the string ahead of the JSX.- return <title>{`Custom #${params.id} — PentAGI`}</title>;- }+ it('renders a title component produced by apolloTitle()', async () => {+ // End-to-end check that the public factory wires the marker correctly+ // and `DocumentTitle` recognizes it. A stubbed `useQuery` returns+ // canned data so the test is hermetic.+ const CustomTitle = apolloTitle<{ name: string }, Record<string, never>>({+ select: (data, { id }) => `Custom #${id} ${data?.name ?? ''}`.trim(),+ // Minimal stub — only `data` is read downstream.+ useQuery: (() => ({ data: { name: 'thing' } })) as never,+ variables: () => ({}),+ });renderAt('/items/42', [{@@ -121,16 +126,13 @@ describe('DocumentTitle', () => {},]);- await waitFor(() => expect(document.title).toBe('Custom #42 — PentAGI'));+ await waitFor(() => expect(document.title).toBe('Custom #42 thing — PentAGI'));});- it('treats a lowercase-named function as a plain resolver, not a component', async () => {- // A named function starting with a lowercase letter is detected as a- // resolver: DocumentTitle calls it with params and wraps the returned- // string with the standard "X — PentAGI" template.- function resolveTitle(params: Record<string, string | undefined>) {- return `Item ${params.id}`;- }+ it('treats an unmarked function as a plain resolver, not a component', async () => {+ // Without the marker, DocumentTitle calls the function with params and+ // wraps the returned string with the standard "X — PentAGI" template.+ const resolveTitle = (params: Record<string, string | undefined>) => `Item ${params.id}`;renderAt('/items/7', [{diff --git a/frontend/src/components/shared/document-title.tsx b/frontend/src/components/shared/document-title.tsxindex d0f837df..5b4cd327 100644--- a/frontend/src/components/shared/document-title.tsx+++ b/frontend/src/components/shared/document-title.tsx@@ -2,6 +2,7 @@ import type { ComponentType } from 'react';import { useMatches } from 'react-router-dom';+import { isApolloTitle } from '@/lib/route-titles/apollo-title';import { renderTitle, type RouteParams } from '@/lib/route-titles/render-title';type TitleComponent = ComponentType<{ params: RouteParams }>;@@ -18,17 +19,6 @@ const hasTitle = (handle: unknown): handle is { title: TitleResolver } => {return typeof value === 'string' || typeof value === 'function';};-// Distinguishes a React component from a plain resolver by the PascalCase-// convention — components must start with an uppercase letter (Apollo title-// factories return named `ApolloTitle`, regular resolvers are anonymous-// arrow functions). This mirrors how React itself differentiates them in-// JSX, so we don't need an extra marker on the function.-const isTitleComponent = (fn: (params: RouteParams) => unknown): fn is TitleComponent => {- const first = fn.name.charAt(0);-- return first === first.toUpperCase() && first !== first.toLowerCase();-};-/*** Document `<title>` driver. Walks `useMatches()` for the deepest route* exposing `handle.title` and renders accordingly:@@ -61,7 +51,7 @@ export function DocumentTitle() {return renderTitle(value);}- if (isTitleComponent(value)) {+ if (isApolloTitle(value)) {const TitleComp = value;return <TitleComp params={match.params} />;diff --git a/frontend/src/lib/route-titles/apollo-title.test.tsx b/frontend/src/lib/route-titles/apollo-title.test.tsxnew file mode 100644index 00000000..91a21296--- /dev/null+++ b/frontend/src/lib/route-titles/apollo-title.test.tsx@@ -0,0 +1,47 @@+import { describe, expect, it } from 'vitest';++import { apolloTitle, isApolloTitle } from './apollo-title';++describe('apolloTitle()', () => {+ // The single most important invariant of the module: the factory must+ // produce a value that `isApolloTitle()` recognizes. If this ever breaks+ // (e.g. someone replaces `Object.assign` with a spread that drops Symbol+ // keys, or swaps `Symbol.for` for a fresh `Symbol`), `DocumentTitle`+ // silently misclassifies reactive titles as resolvers and the next+ // production build crashes on routes like /flows/:id.+ it('returns a component recognized by isApolloTitle', () => {+ const Title = apolloTitle({+ select: () => 'x',+ useQuery: (() => ({ data: null })) as never,+ variables: () => null,+ });++ expect(isApolloTitle(Title)).toBe(true);+ });++ it('isApolloTitle rejects plain functions and non-functions', () => {+ expect(isApolloTitle(() => 'x')).toBe(false);+ expect(isApolloTitle(function named() {})).toBe(false);+ expect(isApolloTitle('static title')).toBe(false);+ expect(isApolloTitle(null)).toBe(false);+ expect(isApolloTitle(undefined)).toBe(false);+ expect(isApolloTitle({})).toBe(false);+ });++ it('survives function name stripping (simulates Terser minification)', () => {+ const Title = apolloTitle({+ select: () => 'x',+ useQuery: (() => ({ data: null })) as never,+ variables: () => null,+ });++ // Redefine `.name` to '' to mimic what Terser does to anonymous-in-+ // source function expressions. The marker is property-based, so the+ // guard must still pass — this is the regression that caused the+ // /flows/:id crash on production.+ Object.defineProperty(Title, 'name', { value: '' });++ expect(Title.name).toBe('');+ expect(isApolloTitle(Title)).toBe(true);+ });+});diff --git a/frontend/src/lib/route-titles/apollo-title.tsx b/frontend/src/lib/route-titles/apollo-title.tsxindex f33ad343..35af1c17 100644--- a/frontend/src/lib/route-titles/apollo-title.tsx+++ b/frontend/src/lib/route-titles/apollo-title.tsx@@ -29,17 +29,39 @@ interface ApolloTitleOpts<TData, TVars extends Record<string, unknown>> {variables: (params: RouteParams) => null | TVars;}+// Terser drops the `function ApolloTitle()` name in production builds, so+// `DocumentTitle` cannot rely on `fn.name` to distinguish a reactive title+// component from a plain `(params) => string` resolver. This explicit marker+// survives minification because Vite preserves property names by default,+// and Terser leaves Symbol-keyed properties alone even under aggressive+// `mangle.properties`. `Symbol.for` is used so an accidental double-bundle+// (worker, SSR boundary) still resolves to the same key.+const APOLLO_TITLE_MARKER = Symbol.for('pentagi.apolloTitle');++export type ApolloTitleComponent = ComponentType<{ params: RouteParams }> & {+ readonly [APOLLO_TITLE_MARKER]: true;+};++/**+ * Type guard for components produced by {@link apolloTitle}. `DocumentTitle`+ * uses this to decide whether to render `<TitleComp params={...} />` or call+ * the value as a `(params) => string` resolver. Keep the marker private to+ * this module — callers should only depend on this guard.+ */+export const isApolloTitle = (value: unknown): value is ApolloTitleComponent =>+ typeof value === 'function' && (value as { [APOLLO_TITLE_MARKER]?: unknown })[APOLLO_TITLE_MARKER] === true;+/*** Factory for Apollo-cache-driven `<title>` components used by route handles.*- * Returns a named `ApolloTitle` component so `DocumentTitle` can detect it as- * a component (PascalCase convention) versus a plain `(params) => string`- * resolver.+ * Tags the returned component with an internal marker so `DocumentTitle`+ * (via {@link isApolloTitle}) can identify it as a component and render via+ * JSX rather than treating it as a `(params) => string` resolver.*/export function apolloTitle<TData, TVars extends Record<string, unknown>>(opts: ApolloTitleOpts<TData, TVars>,-): ComponentType<{ params: RouteParams }> {- return function ApolloTitle({ params }: { params: RouteParams }) {+): ApolloTitleComponent {+ function ApolloTitle({ params }: { params: RouteParams }) {const vars = opts.variables(params);const { data } = opts.useQuery(vars === null@@ -48,5 +70,7 @@ export function apolloTitle<TData, TVars extends Record<string, unknown>>();return renderTitle(opts.select(data, params));- };+ }++ return Object.assign(ApolloTitle, { [APOLLO_TITLE_MARKER]: true as const });}diff --git a/frontend/src/lib/route-titles/index.ts b/frontend/src/lib/route-titles/index.tsindex 3cd8eb77..eeeb9ee5 100644--- a/frontend/src/lib/route-titles/index.ts+++ b/frontend/src/lib/route-titles/index.ts@@ -20,9 +20,11 @@ export interface RouteTitleHandle {* - `string` — fully static, known at build time.* - `(params) => string` — derived synchronously from URL params.* - `ComponentType<{ params }>` — reactive (e.g. subscribes to Apollo- * cache for resource-driven titles). Must be PascalCase-named so the- * runtime detector in `DocumentTitle` distinguishes it from a plain- * resolver function.+ * cache for resource-driven titles). Must be produced by `apolloTitle()`+ * so the marker it attaches lets `DocumentTitle` distinguish a component+ * from a `(params) => string` resolver at runtime. A hand-rolled component+ * function will be misdetected as a resolver and called with raw params —+ * always route reactive titles through `apolloTitle()`.*/export type TitleResolver = ((params: RouteParams) => string) | ComponentType<{ params: RouteParams }> | string;