Repository navigation
Fix dspace-customers#1083 R22: SSR static pages no longer trust the Host header; SSR cache keyed by origin and language - #1669
Merged
Conversation
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.
4 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
References
dspace-customers issue 1083, finding R22.
No upstream fix.
HtmlContentServiceis CLARIN-only (from ufal#103 and ufal#97, ported in #1241); ufalclarin-v7still builds the URL from the Host header. DSpace/dspace-angular keys the SSR cache byreq.urlonmainanddspace-7_x..dspace-9_x. Searched: history ofhtml-content.service.tsandserver.tson ufalclarin-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
HostHTTP Header", 7.x) for the vanilla Host uses (redirects, robots.txt, metadata); it comes with #1664.Description
During SSR,
HtmlContentServicebuilt the URL ofstatic-files/*.htmlfromreq.protocoland the requestHostheader and rendered the response unsanitized (dsSafeHtml=bypassSecurityTrustHtml). The SSR page caches were keyed by path only. With the bot and anonymous caches on (as inconfig/config.yml), a request to/static/<page>with a forgedHostput the attacker's HTML,<script>included, into the page that the next visitors got from the cache. The same key served adsLanguage=csrender to visitors without the cookie, and an authenticated request with a bot user agent got the anonymous cached page./static/..%2Findexloaded the app's ownindex.htmlinto the page, because Express decodes%2Fbefore serving files.Fix:
ui.baseUrl(built fromui.ssl/ui.host/ui.port, the address Node listens on, e.g.http://dspace-angular:4000indocker/docker-compose.yml), not from the requestHost. Browser requests are unchanged.[protocol, Host, dsLanguage cookie, path+query]instead ofpath+query./or\after URL-decoding (query ignored) loads nothing and renders the existing 404.Behaviour changes: SSR requests static pages from the configured
ui.baseUrlorigin. 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:buildRuntimeUrlusesnew URL(appConfig.ui.baseUrl).origininstead ofreq.protocol+Host(unusedREQUESTinjection removed);getHmtlContentByPathAndLocalerejects names that are not a single file name once decoded.ssr-cache-key.util.ts: newgetSsrCacheKey, replacinggetCacheKeyfromserver.ts(moved so Karma can test it).server.ts: usesgetSsrCacheKey; the/reloadexclusion checksreq.url(the key no longer starts with the path); the bot-cache read requires an anonymous request.Tests:
HtmlContentServiceshould fetch static content from the configured UI origin, not the request Host, during SSR: forgedHoston the server platform; the fetch must go to theui.baseUrlorigin. Old code:Requests received are: GET https://attacker.example/repository/static-files/about.html.HtmlContentServiceshould not request a file name that leaves static-files once decoded:..%2Findexmust not be requested. Old code: one request.getSsrCacheKeyshould separate renders for different language cookiesandshould separate renders for different request origins: old key fails both (Expected '/static/about' not to equal '/static/about').cacheCheckand the caches live inserver.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):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/evilcurl -s -H 'Host: localhost:9999' http://localhost:4000/repository/static/about | grep -c MARKER: before 1, after 0.Hostheader, right after: before 1 (from the cache), after 0.curl -s -H 'Cookie: dsLanguage=cs' http://localhost:4000/repository/static/cite, then without the cookie: before both<html lang="cs", after the second islang="en".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:
html-content.service.spec.ts7/7,ssr-cache-key.util.spec.ts2/2; withstatic-page.component.spec.tsandclarin-license-agreement-page.component.spec.ts28/28. Each new test failed first on the old code.yarn run build:prodpasses (26 warnings, same count asdtq-dev);eslinton the changed files: no new problems; madge: no circular dependency.dtq-devvs 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 gotCACHE HITfrom the bot cache;/static/..%2Findexreturned the nestedindex.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.baseUrlorigin. Withui.ssl: trueand a certificate not valid forui.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:
HostHTTP Header DSpace/dspace-angular#5288 via Upgrade vanilla base 7.6.5 → 7.6.7 #1664. Until then the cache key keeps such renders per Host.Accept-Languagein the key: the server render language is thedsLanguagecookie or the default, and SSR sends that language first (q=1) in the RESTAccept-Language.X-Forwarded-Hostin e-mail links (separate finding).