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.
75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
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">');
|
|
});
|
|
});
|