Skip to content

Fix dspace-customers#1083 R22: SSR static pages no longer trust the Host header; SSR cache keyed by origin and language - #1669

Merged
milanmajchrak merged 4 commits into
dtq-devfrom
fix/r22-ssr-static-content-origin
Oct 28, 2026
Merged

milanmajchrak merged 4 commits into
dtq-devfrom
fix/r22-ssr-static-content-origin

Conversation

@vidiecan

Copy link
Copy Markdown

References

dspace-customers issue 1083, finding R22.

No upstream fix. HtmlContentService is CLARIN-only (from ufal#103 and ufal#97, ported in #1241); ufal clarin-v7 still builds the URL from the Host header. DSpace/dspace-angular keys the SSR cache by req.url on main and dspace-7_x..dspace-9_x. Searched: history of html-content.service.ts and server.ts on ufal clarin-v7; ufal PRs for "html-content", "Host header", "static files SSR"; DSpace issues/PRs for "getCacheKey", "cache language".
Related, not part of this PR: DSpace#5288 ("Remove reliance on Host HTTP Header", 7.x) for the vanilla Host uses (redirects, robots.txt, metadata); it comes with #1664.

Description

During SSR, HtmlContentService built the URL of static-files/*.html from req.protocol and the request Host header and rendered the response unsanitized (dsSafeHtml = bypassSecurityTrustHtml). The SSR page caches were keyed by path only. With the bot and anonymous caches on (as in config/config.yml), a request to /static/<page> with a forged Host put the attacker's HTML, <script> included, into the page that the next visitors got from the cache. The same key served a dsLanguage=cs render to visitors without the cookie, and an authenticated request with a bot user agent got the anonymous cached page. /static/..%2Findex loaded the app's own index.html into the page, because Express decodes %2F before serving files.

Fix:

  • SSR loads static pages from the origin of ui.baseUrl (built from ui.ssl/ui.host/ui.port, the address Node listens on, e.g. http://dspace-angular:4000 in docker/docker-compose.yml), not from the request Host. Browser requests are unchanged.
  • SSR cache key: [protocol, Host, dsLanguage cookie, path+query] instead of path+query.
  • Neither cache is read for authenticated requests (the bot cache was).
  • A static page name that contains / or \ after URL-decoding (query ignored) loads nothing and renders the existing 404.

Behaviour changes: SSR requests static pages from the configured ui.baseUrl origin. A path gets separate cache entries per origin and language cookie. Authenticated bots are rendered instead of served from the cache. No new config.

Instructions for Reviewers

Changes:

  • html-content.service.ts: buildRuntimeUrl uses new URL(appConfig.ui.baseUrl).origin instead of req.protocol + Host (unused REQUEST injection removed); getHmtlContentByPathAndLocale rejects names that are not a single file name once decoded.
  • ssr-cache-key.util.ts: new getSsrCacheKey, replacing getCacheKey from server.ts (moved so Karma can test it).
  • server.ts: uses getSsrCacheKey; the /reload exclusion checks req.url (the key no longer starts with the path); the bot-cache read requires an anonymous request.

Tests:

  • HtmlContentService should fetch static content from the configured UI origin, not the request Host, during SSR: forged Host on the server platform; the fetch must go to the ui.baseUrl origin. Old code: Requests received are: GET https://attacker.example/repository/static-files/about.html.
  • HtmlContentService should not request a file name that leaves static-files once decoded: ..%2Findex must not be requested. Old code: one request.
  • getSsrCacheKey should separate renders for different language cookies and should separate renders for different request origins: old key fails both (Expected '/static/about' not to equal '/static/about').
  • The authenticated bot-cache bypass has no unit test: cacheCheck and the caches live in server.ts, which Karma cannot load. Checked end to end (below).

How to test (production build, ui.nameSpace: /repository; curl counts as a bot, so the bot cache answers repeats):

  1. Attacker origin: mkdir -p /tmp/evil/repository/static-files && echo '<p>MARKER</p><script>window.r22=1</script>' > /tmp/evil/repository/static-files/about.html && python3 -m http.server 9999 -d /tmp/evil
  2. curl -s -H 'Host: localhost:9999' http://localhost:4000/repository/static/about | grep -c MARKER: before 1, after 0.
  3. The same without the Host header, right after: before 1 (from the cache), after 0.
  4. curl -s -H 'Cookie: dsLanguage=cs' http://localhost:4000/repository/static/cite, then without the cookie: before both <html lang="cs", after the second is lang="en".
  5. curl -s -o /dev/null -w '%{http_code}' http://localhost:4000/repository/static/..%2Findex: before 200 (app shell nested in the page), after 404.

Local run:

  • Specs: html-content.service.spec.ts 7/7, ssr-cache-key.util.spec.ts 2/2; with static-page.component.spec.ts and clarin-license-agreement-page.component.spec.ts 28/28. Each new test failed first on the old code.
  • yarn run build:prod passes (26 warnings, same count as dtq-dev); eslint on the changed files: no new problems; madge: no circular dependency.
  • Built server with a minimal REST stub instead of a backend, browser user agent, dtq-dev vs this branch. dtq-dev: the forged-Host page and its <script> were served, then served from the cache to a normal request; the cs render was served without the cookie; an authenticated Googlebot request got CACHE HIT from the bot cache; /static/..%2Findex returned the nested index.html. This branch: none of these, and the attacker origin got no request. /reload/... is still not cached.

Assumption: Node can reach its own ui.baseUrl origin. With ui.ssl: true and a certificate not valid for ui.host, the SSR self-request fails TLS and the static page renders as not found on the server (the browser still loads it).

Not in scope:

jm added 4 commits October 27, 2026 14:40
…eader

During SSR, HtmlContentService built the static-file URL from the request
protocol and Host header. A request with a spoofed Host made the server fetch
/static-files/*.html from an attacker origin and render it unsanitized
(dsSafeHtml); the SSR page caches, keyed by path only, could then serve that
render to other visitors.

Use the origin of the configured ui.baseUrl (the address the Node server
listens on) instead and drop the now unused REQUEST injection.

dspace-customers#1083, finding R22.
getCacheKey returned req.url, but an anonymous SSR render also depends on the
request origin (protocol and Host header, e.g. citation_pdf_url via
getCurrentOrigin) and on the dsLanguage cookie. A request with a forged Host or
another language filled the bot and anonymous caches for everyone requesting
that path.

Move the key to getSsrCacheKey so it can be unit tested and include protocol,
Host and the language cookie. The /reload check now reads req.url, because the
key no longer starts with the path.

dspace-customers#1083, finding R22.
cacheCheck read the bot cache for any bot user agent before checking for an
authentication cookie, so an authenticated request with a bot user agent got
the anonymous cached page instead of its own render. Read either cache only
for anonymous requests; saveToCache already stores anonymous renders only.

dspace-customers#1083, finding R22.
The static page file name comes from the URL. Express decodes the path before
serving files, so /static/..%2Findex loaded the app's own index.html into the
page. Return no content for a name that contains a path separator after
decoding (the query part is ignored), which renders the existing 404.

dspace-customers#1083, finding R22.
@milanmajchrak
milanmajchrak merged commit 60f6ccb into dtq-dev Oct 28, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants