🪧 fix: Emit the Configured-Footer Answer With the HTML Shell (#15886)

* 🪧 fix: Emit the Configured-Footer Answer With the HTML Shell

The chat surface lays out against whether the deployment configured a footer
(customFooter, privacy policy, terms of service). The client answered that from
localStorage — whatever the last resolved `/api/config` recorded — so the answer
was a guess on a first-ever visit and could be stale for a deployment whose
configuration changed.

The backend already knows when it serves client/dist/index.html, so it says so
there: `injectConfiguredFooterBootstrap` writes `hasConfiguredFooter` into
`window.__LIBRECHAT_CONFIG__` ahead of the app's own scripts, once at boot from
the base app config, and the client reads it on its first render instead of
remembering the last answer. The `configured-footer` storage atom is gone.

Client and server share one predicate (`hasConfiguredFooter` in
librechat-data-provider) so the two cannot disagree, and the shell injection is
generalized (`injectBootstrapConfig`), with the query-devtools flag now going
through it.

* 🧷 fix: Keep Bootstrap Values Literal and State the Shell's Contract

`injectBootstrapConfig` passed its script as a replacement string, so a `$&`,
`$1` or `$'` inside an injected value was expanded by `String.replace` instead
of reaching the client. Both insertion points take a replacement function now,
and the spec reads its values back off the global through an explicit result
type rather than an unchecked dictionary.

The shell answers from the deployment's own configuration, which a per-tenant,
role or user override of `interface.privacyPolicy` or `termsOfService` can
disagree with — `/api/config` resolves the caller's and the client prefers it.
That contract was overstated in three comments ("a deployment-lifetime fact",
"the same one the config will give"); they now say what the answer is and what
resolves the rest. Two scenarios cover the disagreement end to end: a document
served without the answer, and a policy link only this caller's config carries.
This commit is contained in:
Marco Beretta 2026-09-14 03:53:54 +02:00 • committed by GitHub
parent 91e00bbe6e
commit 8e6e6f062d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 536 additions and 80 deletions

View file

@ -0,0 +1,75 @@
import { injectBootstrapConfig } from './bootstrap';
const SHELL = '<html><head></head><body></body></html>';
/** Every value these tests inject, as the client reads it back off the global. */
interface TestBootstrap {
first?: boolean;
second?: number;
note?: string;
flag?: boolean;
}
/** Runs the shell's injected scripts the way a browser would, so the assertions
* are about what the client receives rather than about the markup's spelling. */
const bootstrapOf = (html: string): TestBootstrap => {
const scripts = [...html.matchAll(/<script\b[^>]*>([\s\S]*?)<\/script>/g)].map(
(match) => match[1],
);
return new Function('window', `${scripts.join('\n')}\nreturn window.__LIBRECHAT_CONFIG__;`)(
{},
) as TestBootstrap;
};
describe('injectBootstrapConfig', () => {
it('merges answers so one injection does not drop another', () => {
const html = injectBootstrapConfig(
injectBootstrapConfig(SHELL, {
sentinel: 'data-librechat-first="true"',
values: { first: true },
}),
{ sentinel: 'data-librechat-second="true"', values: { second: 2 } },
);
expect(bootstrapOf(html)).toEqual({ first: true, second: 2 });
});
it('escapes markup in a value so it cannot close the script', () => {
const note = '</script><script>alert(1)</script>';
const html = injectBootstrapConfig(SHELL, {
sentinel: 'data-librechat-test="true"',
values: { note },
});
expect(html).not.toContain('<script>alert(1)</script>');
expect(html.match(/<\/script>/g)).toHaveLength(1);
expect(bootstrapOf(html)).toEqual({ note });
});
it('keeps `$` sequences in a value literal instead of expanding them', () => {
const note = "$& $` $' $1 $$";
expect(
bootstrapOf(
injectBootstrapConfig(SHELL, { sentinel: 'data-librechat-test="true"', values: { note } }),
).note,
).toBe(note);
expect(
bootstrapOf(
injectBootstrapConfig('<html><body class="x"></body></html>', {
sentinel: 'data-librechat-test="true"',
values: { note },
}),
).note,
).toBe(note);
});
it('lands ahead of the app in a document with no head at all', () => {
const html = injectBootstrapConfig('<html><body class="x"><script></script></body></html>', {
sentinel: 'data-librechat-test="true"',
values: { flag: true },
});
expect(html).toContain('<body class="x"><script data-librechat-test="true">');
});
});

View file

@ -0,0 +1,42 @@
/**
* The global the HTML shell hands to the client for answers the server already
* has when it serves the document. Merged rather than replaced, so several
* injections compose in any order.
*/
const BOOTSTRAP_GLOBAL = 'window.__LIBRECHAT_CONFIG__';
export interface BootstrapInjection {
/** Attribute marking this injection's script, so serving one shell through
* two injectors — or the same one twice — does not stack it. */
sentinel: string;
values: Record<string, boolean | number | string>;
}
/**
* Writes server-known values into the shell's `<head>` (or the top of `<body>`
* in a document without one), ahead of the app's own scripts so the first
* render reads them. The caller stamps the CSP nonce afterwards.
*/
export const injectBootstrapConfig = (
html: string,
{ sentinel, values }: BootstrapInjection,
): string => {
if (html.includes(sentinel)) {
return html;
}
/** A `<` in the payload could close the inline script; it never has to be raw. */
const payload = JSON.stringify(values).replace(/</g, '\\u003c');
const script = `<script ${sentinel}>${BOOTSTRAP_GLOBAL}=Object.assign({},${BOOTSTRAP_GLOBAL},${payload});</script>`;
/* Replacements are functions: a `$&`, `$1` or `$'` inside a value is data, and
a replacement string would have `String.replace` expand it instead. */
if (html.includes('</head>')) {
return html.replace('</head>', () => `${script}</head>`);
}
return html.replace(
/<body([^>]*)>/i,
(_match, attributes: string) => `<body${attributes}>${script}`,
);
};

View file

@ -1,7 +1,8 @@
import { injectBootstrapConfig } from './bootstrap';
export const QUERY_DEVTOOLS_HEADER = 'x-librechat-enable-query-devtools';
const QUERY_DEVTOOLS_SENTINEL = 'data-librechat-query-devtools="true"';
const QUERY_DEVTOOLS_BOOTSTRAP = `<script ${QUERY_DEVTOOLS_SENTINEL}>window.__LIBRECHAT_CONFIG__=Object.assign({},window.__LIBRECHAT_CONFIG__,{"enableQueryDevtools":true});</script>`;
export interface QueryDevtoolsRequest {
get(header: string): string | undefined;
@ -10,18 +11,6 @@ export interface QueryDevtoolsRequest {
export const shouldEnableQueryDevtools = (req: QueryDevtoolsRequest): boolean =>
req.get(QUERY_DEVTOOLS_HEADER) === '1';
const injectQueryDevtoolsBootstrap = (html: string): string => {
if (html.includes(QUERY_DEVTOOLS_SENTINEL)) {
return html;
}
if (html.includes('</head>')) {
return html.replace('</head>', `${QUERY_DEVTOOLS_BOOTSTRAP}</head>`);
}
return html.replace(/<body([^>]*)>/i, `<body$1>${QUERY_DEVTOOLS_BOOTSTRAP}`);
};
export const maybeInjectQueryDevtoolsBootstrap = (
html: string,
req: QueryDevtoolsRequest,
@ -30,5 +19,8 @@ export const maybeInjectQueryDevtoolsBootstrap = (
return html;
}
return injectQueryDevtoolsBootstrap(html);
return injectBootstrapConfig(html, {
sentinel: QUERY_DEVTOOLS_SENTINEL,
values: { enableQueryDevtools: true },
});
};

View file

@ -0,0 +1,66 @@
import { injectConfiguredFooterBootstrap } from './footer';
import { applyCspNonce } from '~/security/csp';
const SHELL =
'<!DOCTYPE html><html><head><title>LibreChat</title></head>' +
'<body><div id="root"></div><script type="module" src="/assets/index.js"></script></body></html>';
const flagOf = (html: string): boolean | undefined => {
const match = /"hasConfiguredFooter":(true|false)/.exec(html);
return match == null ? undefined : match[1] === 'true';
};
describe('injectConfiguredFooterBootstrap', () => {
it('answers yes for each kind of configured footer content', () => {
expect(flagOf(injectConfiguredFooterBootstrap(SHELL, { customFooter: 'Operator' }))).toBe(true);
expect(
flagOf(
injectConfiguredFooterBootstrap(SHELL, {
interfaceConfig: { privacyPolicy: { externalUrl: 'https://example.com/privacy' } },
}),
),
).toBe(true);
expect(
flagOf(
injectConfiguredFooterBootstrap(SHELL, {
interfaceConfig: { termsOfService: { externalUrl: 'https://example.com/terms' } },
}),
),
).toBe(true);
});
it('answers no for a deployment that configured nothing', () => {
expect(flagOf(injectConfiguredFooterBootstrap(SHELL, {}))).toBe(false);
expect(flagOf(injectConfiguredFooterBootstrap(SHELL))).toBe(false);
expect(
flagOf(
injectConfiguredFooterBootstrap(SHELL, {
customFooter: undefined,
interfaceConfig: { privacyPolicy: {}, termsOfService: {} },
}),
),
).toBe(false);
});
it('places the answer ahead of the app that reads it', () => {
const html = injectConfiguredFooterBootstrap(SHELL, { customFooter: 'Operator' });
expect(html).toContain('window.__LIBRECHAT_CONFIG__');
expect(html.indexOf('hasConfiguredFooter')).toBeLessThan(html.indexOf('/assets/index.js'));
});
it('takes the nonce a strict CSP requires to run it', () => {
const html = applyCspNonce(
injectConfiguredFooterBootstrap(SHELL, { customFooter: 'Operator' }),
'abc123',
);
expect(html).toContain('<script nonce="abc123" data-librechat-configured-footer="true">');
});
it('does not stack a second copy on a shell that already carries the answer', () => {
const once = injectConfiguredFooterBootstrap(SHELL, { customFooter: 'Operator' });
expect(injectConfiguredFooterBootstrap(once, { customFooter: 'Operator' })).toBe(once);
});
});

View file

@ -0,0 +1,45 @@
import { hasConfiguredFooter } from 'librechat-data-provider';
import type { TInterfaceConfig } from 'librechat-data-provider';
import { injectBootstrapConfig } from './bootstrap';
const CONFIGURED_FOOTER_SENTINEL = 'data-librechat-configured-footer="true"';
/** What the deployment configured, as the server holds it: the footer env var
* and the interface config the app loaded. */
export interface ConfiguredFooterSource {
customFooter?: string | null;
interfaceConfig?: Pick<TInterfaceConfig, 'privacyPolicy' | 'termsOfService'> | null;
}
/**
* Emits the deployment's footer answer with the HTML shell.
*
* A conversation's composer lays out against whether a footer bar sits beneath
* it, and `/api/config` answers that only after the composer has painted. The
* server already knows when it serves the document, so it says so and no client
* has to guess: a first-ever visit to a configured deployment paints the
* composer in its final position instead of correcting by the bar's height.
* The answer is the deployment's own configuration — `librechat.yaml` plus
* `CUSTOM_FOOTER` — because the document is served before there is a caller to
* resolve: a per-tenant, role or user config override of `privacyPolicy` or
* `termsOfService` is resolved by `/api/config`, and the client prefers that
* resolved answer over this one. Reading the effective config here would cost
* an override query on the document that carries the conversation's LCP, and
* still could not resolve role or user overrides pre-authentication.
*
* Always injected, including the negative answer, so an absent flag means "no
* server said" (the Vite dev server) rather than "no footer".
*/
export const injectConfiguredFooterBootstrap = (
html: string,
source?: ConfiguredFooterSource | null,
): string =>
injectBootstrapConfig(html, {
sentinel: CONFIGURED_FOOTER_SENTINEL,
values: {
hasConfiguredFooter: hasConfiguredFooter({
customFooter: source?.customFooter ?? undefined,
interface: source?.interfaceConfig ?? null,
}),
},
});

View file

@ -1 +1,3 @@
export * from './bootstrap';
export * from './devtools';
export * from './footer';