mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
* 🪧 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.
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
/**
|
|
* 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}`,
|
|
);
|
|
};
|