Skip to content

JCU/Fix dspace-customers 1083 R22: SSR cache keyed by origin and language, no static file outside static-files - #1757

Open
jr-rk wants to merge 1 commit into
customer/jcufrom
jcu/fe-ssr-host-header-cache-key
Open

jr-rk wants to merge 1 commit into
customer/jcufrom
jcu/fe-ssr-host-header-cache-key

Conversation

@jr-rk

@jr-rk jr-rk commented Nov 26, 2026 •

Copy link
Copy Markdown

References

Port of dtq-dev-93 PR 1718 (commit fd7cd1c), the v9 port of dtq-dev PR 1669. dspace-customers issue 1083, finding R22.
In comparison with the source, the customized diff is based on jcu rewriting html-content.service.ts in #1478 so that it loads static pages only in the browser and never reads the Host header, so the Host header part does not apply. Only the same isPlainFileName guard from the source is added to jcu's getHtmlContentByPathAndLocale, with a new spec, because jcu had none for this service. The SSR cache stats endpoint now lists the new keys; it only lists them, so nothing else changes.

Description

The SSR page caches were keyed by req.url only, and the bot cache was read before the login check. Now the key holds origin, path and language, the bot cache serves only anonymous requests, and a static page name like ..%2Findex is refused.

Instructions for Reviewers

  • server.ts and new ssr-cache-key.util.ts: the same as the source. Only the import block next to the jcu ssr-cache-stats import conflicted.
  • html-content.service.ts: jcu has its own browser-only version (Add deployed-version info: VERSION_D at /static/VERSION_D (#813) #1478) that never reads the Host header, so the Host header part does not apply. Only the isPlainFileName guard is added to getHtmlContentByPathAndLocale.
  • New html-content.service.spec.ts with one test for the guard (..%2Findex makes no request); jcu had no spec for this service.
  • The SSR cache stats endpoint (JCU: opt-in loopback endpoint listing the SSR cache contents #1657, JCU: expose SSR cache hits, misses and hit rate #1681) now lists the new keys (JSON arrays with origin, language cookie and path). It only lists keys, so nothing else changes.
  • The title differs from the source because the Host header part does not apply.
  • Tests: ssr-cache-key.util.spec.ts and html-content.service.spec.ts (3 specs pass locally on Node 20). ESLint is clean on the changed files, npm run build:prod passes. Not tested on a running SSR server.

Checklist

  • My PR is created against the branch it ports to
  • My PR is small in size
  • My PR follows the code conventions and passes ESLint
  • My PR passes the relevant specs and includes new or updated specs
  • My PR includes details on how to test it
  • No new dependencies, no new configuration

…uage, no static file outside static-files

Port of dtq-dev-93 PR 1718 (commit fd7cd1c), the v9 port of dtq-dev
PR 1669.
- server.ts and ssr-cache-key.util(.spec).ts as in 1718; only the import
  block next to the jcu ssr-cache-stats import conflicted.
- jcu has its own browser-only html-content.service that never reads the
  Host header, so the origin part of 1718 does not apply. Only the
  isPlainFileName guard is added to getHtmlContentByPathAndLocale, with a
  new spec for it.
@jr-rk jr-rk self-assigned this Nov 26, 2026
@jr-rk
jr-rk requested a lite review from Copilot November 26, 2026 10:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical cache-key issue remains: requests with different Accept-Language values can receive the wrong localized SSR HTML when no valid language cookie is present.

1 open issue
What changed in this PR

This pull request hardens SSR page caching by separating cached renders by origin and language, preventing authenticated users from receiving bot-cache entries, and rejecting encoded path traversal in static-file requests.

Changes:

  • Added origin-, language-, and URL-aware SSR cache keys.
  • Restricted bot-cache reads to anonymous requests.
  • Added static filename validation and focused unit tests.
File Description
src/​app/​shared/​ssr-cache-key.util.ts Defines composite SSR cache keys using request origin, language-cookie context, and URL.
src/​app/​shared/​ssr-cache-key.util.spec.ts Tests SSR cache-key separation by language and request origin.
src/​app/​shared/​html-content.service.ts Rejects decoded filenames containing path separators before fetching static content.
src/​app/​shared/​html-content.service.spec.ts Verifies encoded traversal-style filenames do not trigger requests.
server.ts Uses the new SSR cache keys and prevents authenticated requests from reading bot-cache entries.

Review effort: Lite

* @param req current request
*/
export function getSsrCacheKey(req: Request): string {
return JSON.stringify([req.protocol, req.get('host'), req.cookies?.[LANG_COOKIE], req.url]);
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