Skip to content

Harden the response headers, the session cookie and what templates write into scripts - #330

Open
r0ny123 wants to merge 14 commits into
ref/integrate-all-20261025from
fix/web-security-hardening
Open

r0ny123 wants to merge 14 commits into
ref/integrate-all-20261025from
fix/web-security-hardening

Conversation

@r0ny123

@r0ny123 r0ny123 commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Based on ref/integrate-all-20261025, the build of all open PRs (#316's comment has the order). Its scripts, templates and tests are the ones this changes, so the diff here is this change alone. One of a set of PRs from a review of the front end against the modern-web-guidance security and privacy guides; the others cover forms, keyboard access, semantics, colour, layout, tooltips, performance and account deletion (#328).

What was wrong.

  • The only CSP was frame-ancestors 'self'. An injection that got past autoescaping (Server-rendered values are injected into <script> blocks via |safe #85 was one) could run inline script, load an <object>, set a <base> or post a form to another origin.
  • No Permissions-Policy, Cross-Origin-Opener-Policy or Strict-Transport-Security.
  • The session cookie was plain session. A sibling subdomain, or anyone in the middle of a plain-HTTP request before sign-in, could plant a validly signed session of their own account.
  • admin_server.html wrote the registration token into a script as "{{ ... }}", and three integer ids went unescaped into scripts. The Malpedia link put the family name into a URL path unencoded. A cross-compare cell carried a backend id inside a JS string inside onclick. None of these is exploitable today, but each is the pattern AGENTS.md's escaping rules forbid.
  • /admin/server printed the backend server token into a text field, which AGENTS.md says is never rendered. The form needed it because an empty field cleared the token.
  • /settings printed the user's API token into the page, and the pages that show a token could be restored from the back/forward cache after logout.

Change.

  • e49ad3b. The CSP adds object-src 'none'; base-uri 'none'; form-action 'self'; connect-src 'self'; font-src 'self'; img-src 'self' data: blob:; worker-src 'self'; media-src 'none'; manifest-src 'self', still with setdefault, so a deployment's own header wins.
  • bdece4f. Scripts:
    • Each response draws a nonce (csp_nonce()). Every <script> in the templates carries it, as does the one flask-dropzone's config() writes. The policy is script-src 'nonce-…' 'strict-dynamic' 'report-sample'; script-src-attr 'unsafe-inline'.
    • later(), the CFG viewer loader and the Dropzone loader add scripts by src, which 'strict-dynamic' admits. live_search.js's adopt() gives the scripts it re-creates the page's nonce.
    • The inline handler attributes still run until they are listeners; an injected <script> does not.
    • No rendered HTML is reused across responses: the result cache and memos hold data, gzip works per response, and pages carry no ETag.
  • 1c8a7cc. Playwright evaluates string wait_for_function predicates with eval, which the policy refuses. 37 such predicates became arrow functions, and AGENTS.md says so.
  • c796815. testFunctionTableRows.py compared two renders byte for byte, which now differ in the nonce. It masks the nonce as it already masked the CSRF token.
  • b2d2055. Every response gets Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=(), usb=(); the clipboard stays allowed.
    • Cross-Origin-Opener-Policy: same-origin-allow-popups and Strict-Transport-Security go only on secure requests: Chromium logs an error for COOP on every plain-HTTP page off loopback.
    • The new STRICT_TRANSPORT_SECURITY setting defaults to what docker-mcrit's TLS site already adds in NGINX, max-age=31536000; includeSubDomains. A browser acts on the first such header only, which would be ours. None sends none. The README has an HTTPS section.
  • 8870c8d and 4fc20fc. The session cookie is __Host-session when it is Secure, host-only and at path /, and session otherwise. A SESSION_COOKIE_NAME in instance/config.py still wins.
  • df3c8be and 8fd7131. The token and ids go through |tojson and the Malpedia link through |urlencode.
    • A cross-compare cell carries its target in data-href, followed by a listener bound while the page parses, so an early click still works.
    • testScriptEscaping.py now fails on any interpolation in a script body other than |tojson or url_for().
  • 4522a99. The token field starts empty, and a line under it says whether a token is set. An empty field keeps the token, a typed one replaces it, and a new Remove Token button clears it.
  • 68ef945. The views that show a token are sent with Cache-Control: no-store, which keeps them out of the HTTP cache and out of Chromium's back/forward cache, so Back after logout cannot show a token.
    • A no_store decorator in compression.py sits next to uncompressed, so every other page keeps its caching.
    • A test holds that the two sets of views are the same.
    • Clear-Site-Data is not needed for this.
  • 82cd67e. The same secret-key fix as Follow-ups from the batch E review: read-only database, backup command, key file without hard links, upper-case job ids #322: a worker without hard links could read the key file while another was still writing it, and start with a different key. The key is now staged and renamed into place under a lock on the folder.
  • 5011a7e. /settings no longer contains the API token.
    • The page shows an empty field and a Show token button. The button POSTs to the new admin.show_apitoken with the CSRF token and gets back {"apitoken": …} for the signed-in user only. It then fills the field and enables the copy button. A failed request, including a session that has ended, shows an error and no token.
    • show_apitoken is @visitor_required, POST-only, @no_store and @uncompressed. /settings is gzipped and cacheable again: 9.4 KB on the wire instead of 42.8 KB. On mobile, the uncompressed page took an extra round trip and scored 99 on the build of all PRs.
    • After Regenerate, the settings page fetches the new token by itself. The script clears the token when the user leaves the page, so the back/forward cache never holds it.
    • An early click on Show token is replayed once the scripts are in, as for the page's other buttons.
    • /admin/server still shows the registration token and stays no-store and uncompressed.

Since review was requested.

Tests.

  • New: testContentSecurityPolicyBrowser.py, testScriptNonces.py, testSecurityHeaders.py, testSessionCookieBrowser.py, testServerToken.py, testNoStoreSecrets.py, testShowApitoken.py and testShowApitokenBrowser.py.
    • testContentSecurityPolicyBrowser.py walks every page as visitor, contributor, admin and signed out, plus the upload drop, the on-demand Dropzone, live search, the CFG graph and its worker, function comparison, modals, tabs, sorting, copy buttons, Show token, column settings and the jobs page's polling. It records zero securitypolicyviolation events and no page error.
    • testScriptNonces.py fails on a <script> without the nonce.
    • testShowApitoken.py (16 tests, CSRF on) checks that no role's /settings has the token, and that the page is gzipped and not no-store. It checks that the route returns the caller's own token and never another user's, and that it refuses a missing or wrong CSRF token, a GET, a signed-out caller and a pending account. Printing the token in settings.html again fails the token scan.
    • testShowApitokenBrowser.py (6 tests, Chromium) clicks Show token and reads the copied token back from the clipboard. It also covers an early click, Regenerate, a server error, an ended session and a return through the back/forward cache. Its early-click test holds the script by a pattern that allows ?v= (975aeef).
  • Extended: testFramingHeaders.py, testAppConfig.py, testScriptEscaping.py, testCrossMatrixLinks.py, testCompression.py and testApiTokenRotation.py.
  • Each fails on the base. The non-browser suite passes apart from the two renders c796815 fixes (3502 passed). The browser modules pass (519, plus one whose timing 8fd7131 fixed, which then passed 3 runs of 3), and ruff is clean. On 5011a7e, the token, compression, no-store, rotation, script-scoping and CSP-walk modules pass (91).

Lighthouse. Accessibility, Best Practices and SEO are 100 on the sampled pages, signed in and out. Desktop Performance is 100. An interleaved run with and without the header showed no cost from the policy. On 5011a7e /settings scores 100 in all four on desktop. On mobile it scored 100, 99 and 100; the 99 was blocking time on a shared machine.

For the other PRs of the set. Any new <script> needs nonce="{{ csp_nonce() }}", or it does not run and testScriptNonces.py fails. A browser test that builds its own app needs secure on its cookie dicts. A new view that returns a token needs @no_store and @uncompressed.

The Content-Security-Policy said only who may frame a page. Markup that
got past autoescaping could still load a plugin, add a <base> that
re-points every relative link, post a form to another site, or fetch
and send data anywhere.

The policy now also carries object-src 'none', base-uri 'none',
form-action 'self', connect-src, font-src, worker-src and manifest-src
'self', img-src 'self' data: blob: and media-src 'none'. Everything
MCRITweb loads is its own, so nothing changes for a page: every page
was walked in Chromium as each role and signed out, with the upload,
the overlay's on-demand Dropzone, live search, the CFG worker and the
function comparison driven, without a violation. It is still set with
setdefault, so a response that names its own policy keeps it.
Playwright evaluates a wait_for_function predicate given as a bare
expression with eval, once per poll. Under a Content-Security-Policy
that refuses eval, the first poll that is needed fails the wait with an
EvalError, though the page itself is fine. A predicate written as a
function is compiled once, through the debugging protocol, which the
policy does not govern.

The 37 expression predicates in the browser tests become arrow
functions with the same body, ahead of the script policy that refuses
eval on every page.
The Content-Security-Policy said nothing about scripts, so a <script>
that slipped past autoescaping ran like one of ours. It now carries
script-src 'nonce-...' 'strict-dynamic' 'report-sample', with a nonce
drawn per response by csp_nonce() and written on every script element
the templates have, and on the one flask-dropzone's config() writes.
'strict-dynamic' extends that to what those scripts add themselves:
base.html's later() queue, the CFG viewer's libraries and its worker,
Dropzone fetched on the first drag. live_search.js gives the scripts
of the rows it swaps in this page's nonce, since an inline script is
admitted by its nonce alone; Chromium runs it without, the spec does
not. script-src-attr 'unsafe-inline' keeps the onclick= and other
handler attributes running until they are listeners.

No rendered page is reused: the result cache holds reports and images,
the memos hold data, gzip compresses each response as it goes, and
pages carry no validators, so every response renders its own nonce.

testScriptNonces.py fails on a template's script without the nonce and
on any rendered page's script without that response's nonce, and pins
the policy's shape. testContentSecurityPolicyBrowser.py walks every
page as visitor, contributor, admin and signed out, and drives the
drop and the overlay's Dropzone, live search, the CFG worker, the
function comparison, the cross compare tabs and swatches, the dialogs,
sorting, the copy buttons, the column settings and a running job's
polling, failing on any violation, page error or worker fallback.
AGENTS.md says how to write a script, and a browser test's wait,
under the policy.
No response said which browser features a page may ask for, and none
told a browser that had come over HTTPS to stay there or kept other
sites from holding a handle on MCRITweb's window.

Every response now carries Permissions-Policy: camera=(), microphone=(),
geolocation=(), payment=(), usb=(), none of which a page uses; the
clipboard the copy buttons write to stays allowed. A request that came
over HTTPS, directly or through a proxy TRUSTED_PROXY_COUNT trusts,
also gets Strict-Transport-Security and Cross-Origin-Opener-Policy:
same-origin-allow-popups. Over plain HTTP neither is sent: a browser
ignores HSTS there, and Chromium logs an error for COOP on every
plain-HTTP page off the loopback address, which docker-mcrit's default
site is.

The HSTS value is STRICT_TRANSPORT_SECURITY, defaulting to the one
docker-mcrit's TLS site already adds in NGINX. Browsers act on the
first such header in a response, which is the app's, so any other
default would change what that deployment says. None sends none.
Under the plain name "session", a sibling subdomain, or anyone in the
middle of a plain-HTTP request to the host before the user signs in,
could set a session cookie of their own - validly signed, for their
own account - that the browser then sent in place of ours, or one
that logged the user out. A browser takes a __Host- cookie only from
the host itself, over HTTPS, for the whole site.

create_app names the cookie once the config has loaded: __Host-session
when it is Secure, has no Domain and its path is /, which is the
default, and "session" otherwise, so plain-HTTP deployments with
SESSION_COOKIE_SECURE = False and cookies shared across subdomains keep
working. A name set in instance/config.py wins. Everyone logs in once
more, as the session generation change in this release asks anyway.

The shared test fixture keeps "session": the browser tests hand
Chromium the cookie without a Secure flag, which it refuses under a
__Host- name. testAppConfig.py covers the name and the attributes the
prefix needs, and testSessionCookieBrowser.py signs in through the form
of a deployment-built app and stays signed in.
Three places wrote a value with nothing but autoescaping, which is
HTML escaping. The admin server page put the registration token, which
the admin chooses and only the registration form's HTML pattern
limits, into a quoted JS string: a trailing backslash escaped the
closing quote and broke the script. The family rows put a contributor's
family name into the path of the Malpedia link, where a ? or a # began
a query or fragment of its own. And a cross compare cell wrote its
sample's job id into a JS string in an onclick attribute, which the
HTML parser decodes before the handler is compiled; job ids are
ObjectIds, so that one was inert.

The token goes through |tojson, as do the three integer ids written
into the CFG pages' scripts, the family name through |urlencode, and
the cell's link into a data-href with the same relative target, which
a listener the page binds while it parses follows, as early as the
onclick did. testScriptEscaping.py now fails on any interpolation in
a script body that is not |tojson, url_for() apart, and renders the
three cases.
testThemeBrowser.py builds its own app, which names its session cookie
__Host-session as a deployment's now does, and handed Chromium the
cookie without a Secure flag. Chromium refuses a __Host- cookie
without one, so every test in the module failed to set up. The cookie
now carries the flag the app's own Set-Cookie does, and the theme walk
runs on the deployment's cookie name.
The Change Backend Server form printed the stored server token as the
value of a text field, so that a submit would post it back unchanged:
the token sat in clear in the page source, the browser's cache and on
the admin's screen, where AGENTS.md says it is never rendered. An empty
field cleared it.

The field now starts empty and a line under it says whether a token is
set. change_server keeps the stored token when the field comes back
empty, replaces it with a typed one, and clears it only for the new
Remove Token button, shown when there is a token to remove. The page
still shows the registration token, so it stays uncompressed; the
docstring and README sentence that listed the backend token there now
list only that one.
The test that clicks a cross compare cell while jQuery is held back
loaded the page waiting for its load event, which the held script can
delay for good when the queue starts first. It timed out once in a
full run. It now waits for the document to be parsed, which is all a
listener bound while the page parses needs.
The settings page prints the account's API token and the server page the
registration token. Without no-store a browser kept them in its back/forward
cache, so Back after logout restored the page, token included, without asking
the server. The two views now carry a no_store decorator next to uncompressed;
every other page keeps its caching.
The rendered-pages test compares two renders of a page byte for byte. Each
response now carries its own CSP nonce, so the two differed in every script
tag. The comparison masks the nonce next to the CSRF token it already masks.
Without hard links the key was written into a file created with O_EXCL,
empty until filled. A worker that opened that file in the gap could
replace it and find the creator's key still written into the file it
had displaced; a third worker that had opened the displaced file before
the replacement then read the loser's key and used it, while everyone
else used the winner's. One round in about 1600 ended that way.

The staged key, complete and private from mkstemp, is now renamed into
place under a lock on the instance folder, looking at the key file again
once the lock is held. No key file is opened for writing any more, and
none is ever visible unfinished.

The test that pinned the old create-then-lock order is replaced by two
that pin the new one.
The settings page printed the account's API token, so the whole page was
sent uncompressed (a secret that is the same in every response is what a
BREACH attack reads out of a compressed one) and with no-store, for a
value most visits never look at.

The page now has a "Show token" button. static/show_apitoken.js, queued
with later(), POSTs with the CSRF token to the new admin.show_apitoken,
which answers the session's own token as JSON, uncompressed and
no-store, and refuses a GET, a missing or wrong CSRF token, a signed-out
caller and a pending account. The token lands where it was shown before,
next to its copy button, and a polite status says whether it worked.
Regenerating the token leaves a one-time mark in the session, so the page
it leads back to fetches the new token at once, as it used to show it.
The script takes the token off the page on pagehide, since the page may
now be kept in the back/forward cache. base.html holds an early press on
the button, as it does for data-post buttons.

/settings has no secret left and is compressed and cacheable again. The
registry of secret views, the token-scan test and the no-store test now
name admin.show_apitoken instead, and printing the token on the settings
page again fails the scan.
The test held the request for show_apitoken.js by its bare path. With #331's
static versioning the URL ends in ?v=<digest>, the route never matched and the
test failed on the build of all PRs. The pattern now allows a query.
r0ny123 added a commit that referenced this pull request Oct 26, 2026
The status message tests copy the API token on the settings page. With #330
the page no longer prints the token: its copy button stays hidden until Show
token is pressed, so the tests timed out on the build of all PRs. They press it
first where the page has it.
r0ny123 added a commit that referenced this pull request Oct 26, 2026
The test that the 64-character token wraps at 320 and 390px looked for it in
the page's source. With #330 the page fetches the token when Show token is
pressed, so it was not there on the build of all PRs. The test presses the
button where the page has it and then measures the page with the token shown.
r0ny123 added a commit that referenced this pull request Oct 26, 2026
…ading and #333's slider wording

The tag that queues upload_form.js from base.html carries the page's
nonce, the overlay's title is the h2 #336 made it, and the slider's
handler upload_form.js defines for the overlay sets its aria-valuetext
as the template's does. #333's tests of the overlay's button and of its
family, version and base address fields read the overlay as the first
drag fetches it.
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.

1 participant