Files
BetterDesk/web-nodejs/lib/safeJsonScript.js
T
UNITRONIX 0429c4080d fix(security): close the 2026-11-13 audit findings (C1-C3, H1-H10, M4-M6/M8/M11)
Full report and per-finding status in docs/security/AUDIT_2026-11-13.md.

Critical
- POST /api/bd/register now requires an Ed25519 proof of possession for a
  first registration and for any public_key/uuid rotation, rejects banned and
  soft-deleted ids before writing, and is rate limited per IP. Knowing a device
  id is no longer enough to flood inventory, resurrect a removed device or
  swap its key.
- Signal RegisterPk with old_id is applied only when the supplied key or the
  machine UUID matches the stored peer, so an unauthenticated caller can no
  longer move and delete someone else's row. Stock RustDesk change-ID (no pk,
  matching UUID) still works.
- The Go server runs as betterdesk from /usr/local/bin/betterdesk-server and
  /opt/rustdesk is no longer group-writable at the directory level, so the
  console user cannot replace the binary. DSNs and the bootstrap admin password
  are gone from systemd units.

High
- /metrics and /api/health/detail require the metrics allowlist or an API key;
  public /api/health returns only {status, version}.
- Panel login consults the shared account-lockout tracker (429 + Retry-After);
  delegated 2FA fails closed; pending-2FA state is written after session
  regeneration; GET /logout no longer destroys the session.
- HTTP->HTTPS redirect no longer trusts the Host header or absolute request
  targets.
- Desktop login no longer pre-renders the account list; a rate-limited
  /login/lookup returns at most 8 prefix matches.
- Panel section overrides are clamped to the role ceiling, so a per-user policy
  cannot grant a permission the role lacks.
- /api/devices/:id/* is covered by one scope guard (tags, rename, change-id,
  restore, access-policy).
- applyUpdate verifies an optional Ed25519-signed manifest and can be made
  fail-closed with UPDATE_REQUIRE_SIGNED_MANIFEST=true.
- The privileged broker no longer manages DATABASE_URL, SESSION_SECRET,
  DEFAULT_ADMIN_PASSWORD, UPDATE_GITHUB_TOKEN, API_KEY_PATH,
  ORG_PEER_VAULT_KEY, SMTP_PASS or API_TOKEN_EXPIRY_DAYS, and reports a
  protocol version so a stale broker is detectable.
- Peer status/online/metrics/linked/policy endpoints require device.view and
  enforce organisation scope.

Medium / low
- Backup restore enforces a decompressed-size and entry-count budget.
- limit/offset clamped on the console and in the Go audit handlers; public
  audit writes are rate limited.
- Data embedded in inline <script> blocks goes through safeJsonScript; the
  pairing page escapes the suggested host.
- Go toolchain and builder images bumped to go1.26.11 (GO-2026-6859); the
  console requires at least that version to rebuild the server.
- Workflow Actions pinned to commit SHAs; Dependabot tracks github-actions.
- Restart tokens compared with crypto.timingSafeEqual.

Tooling
- scripts/security/pentest-local.sh / .ps1 local security gate.
- scripts/check-security-regressions.sh CI gate (unit credentials, inline
  script escaping), wired into Secret Scan.
- Two long-standing red tests fixed (platform loopback address, missing
  panelAccessPolicyService mock).

Verified on the test host: /metrics 401, /api/health/detail 401,
/api/peers/online 401, desktop login ships an empty user list, unproven
register returns 403, Host spoofing redirects to the configured public host,
and the broker reports version 2 with a secret-free manifest.

Thanks: INSOLVE (Honorary); Marco Jakobs (@jacotec); MyNameisStitch (@MyNameisStitch); Redspin (@playerumpknow); muopc (@muopc)
2026-11-13 21:25:10 +01:00

53 lines
1.6 KiB
JavaScript

'use strict';
/**
* Safe JSON embedding for inline `<script>` blocks.
*
* SECURITY (audit M11, CodeQL 367/368, 2026-11-13): `JSON.stringify()` output is
* safe inside a JSON document but NOT inside HTML. A string containing
* `</script>` terminates the script block early, and `<` / `>` can break out of
* attribute or comment contexts in older parsers. Bootstrapping data (branding,
* translations, user objects) is written with `<%- %>`, so the escaping has to
* happen here.
*
* Usage in EJS:
* <script nonce="<%= cspNonce %>">
* window.BetterDesk = { branding: <%- safeJsonScript(branding) %> };
* </script>
*/
const HTML_UNSAFE = /[<>&\u2028\u2029]/g;
const REPLACEMENTS = {
'<': '\\u003c',
'>': '\\u003e',
'&': '\\u0026',
'\u2028': '\\u2028',
'\u2029': '\\u2029',
};
/**
* Serialize a value for inline embedding in a script element.
*
* @param {unknown} value
* @returns {string} JSON text that cannot terminate or escape the script block.
*/
function safeJsonScript(value) {
const json = JSON.stringify(value === undefined ? null : value);
if (typeof json !== 'string') return 'null';
return json.replace(HTML_UNSAFE, (ch) => REPLACEMENTS[ch] || ch);
}
/**
* Convenience wrapper with a fallback for null/undefined, used for optional
* view locals so a missing variable does not print "undefined".
*
* @param {unknown} value
* @param {unknown} [fallback]
*/
function safeJsonScriptOr(value, fallback) {
return safeJsonScript(value === undefined || value === null ? fallback : value);
}
module.exports = { safeJsonScript, safeJsonScriptOr };