Skip to content

fix(ui-server): post-#1891 access policy and WS upgrade hardening - #1898

Merged
jerome-benoit merged 3 commits into
mainfrom
feat/ui-gateway-access-policy
Jun 14, 2026
Merged

jerome-benoit merged 3 commits into
mainfrom
feat/ui-gateway-access-policy

Conversation

@jerome-benoit

@jerome-benoit jerome-benoit commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #1891 consolidating post-merge review findings: a high-severity DoS in the WebSocket upgrade rejection path, an allowedHosts schema gap, and several hardening items in the forwarded-header parser and configuration validation.

Changes

Security

  • WebSocket upgrade handler attaches the socket error listener at the top of the handler instead of after the rejection writes. A client TCP-reset between an upgrade event and a rejection write previously fired 'error' on the Duplex socket with no listener, which Node escalates to an unhandled exception — a process-level DoS reachable by any unauthenticated client.

Configuration validation

  • accessPolicy.allowedHosts entries containing a port now fail schema validation at config load. Runtime matching was already port-agnostic, so runtime behavior is unchanged.
  • UIServerListenOptions splits object-shape validation from the accessPolicy rejection so non-object values surface 'must be a non-array object' instead of the misleading 'accessPolicy must be configured under uiServer' message.

Forwarded-header parser

  • Forwarded parameter values: RFC 7230 quoted-pair sequences inside double-quoted values are now unescaped.
  • X-Forwarded-Proto / X-Forwarded-Host: comma-only inputs (e.g. ,,,) are denied with new invalid-forwarded-protocol / invalid-forwarded-host reasons, mirroring the existing invalid-forwarded-client pattern. Multi-value inputs keep the ambiguous-* reasons.
  • nonEmpty / hasForwardedHeaders align on a whitespace-aware empty check. Whitespace-only forwarded values (e.g. X-Forwarded-Proto: ' ') are treated as absent.
  • getForwardedClientAddress propagates forwarded.kind === 'error' before the trustedProxy gate, matching getForwardedProtocol and getForwardedHost. No current behavior change; closes a latent asymmetry.

Host normalization

  • normalizeHost strips a trailing dot from the host part after the colon split. Host: localhost.:80 now resolves to localhost and matches allowedHosts: ['localhost'] instead of being denied as host-not-allowed.

Documentation

  • UIServerAccessCache JSDoc documents identity-based cache invalidation: mutating accessPolicy.trustedProxies in place will not refresh the normalized set; reload flows must construct a new configuration.

Defaults that change observable behavior

  • accessPolicy.allowedHosts entries containing a port fail config validation at load time. Strip the port suffix from existing entries; runtime matching is unchanged.

Validation

pnpm typecheck, pnpm lint, pnpm format, pnpm build, pnpm test — 0 errors, 0 warnings, 0 fail, 6 skipped.

@hyperspace-pr-bot

Copy link
Copy Markdown
Contributor

Summary

The following content is AI-generated and provides a summary of the pull request:


fix(ui-server): Reject ports in allowedHosts, fix host normalization edge cases

Bug Fix

🐛 Follow-up to #1891 addressing post-merge review findings in the UI server access policy. Three correctness issues are resolved: port-bearing allowedHosts entries are now rejected at config load, FQDN hosts with a port suffix normalize correctly, and whitespace-only forwarded header values are treated as absent.

Changes

  • src/charging-station/ui-server/UIServerAccessPolicy.ts:

    • nonEmpty now uses .trim() so whitespace-only strings are treated as absent.
    • hasForwardedHeaders delegates to nonEmpty instead of a bare !== '' check, ensuring whitespace-only forwarded values (e.g. X-Forwarded-Proto: ' ') no longer flip forwardedHeadersPresent to true and cause a spurious ambiguous-forwarded-protocol denial.
  • src/charging-station/ui-server/UIServerNet.ts:

    • normalizeHost now strips the trailing FQDN dot after the colon-split, so Host: localhost.:80 correctly resolves to localhost instead of being rejected as malformed.
  • src/utils/ConfigurationSchema.ts:

    • allowedHosts schema entries with a port suffix (e.g. gateway.example.com:8080, 127.0.0.1:8080, [::1]:8080) are now rejected at config load with the message "must be a host literal without port". Runtime matching was already port-agnostic, so existing behavior is preserved — operators just need to drop the port from their config.
  • tests/charging-station/ui-server/UIServerAccessPolicy.test.ts: Added test asserting that a whitespace-only x-forwarded-proto header is treated as absent and the request is allowed.

  • tests/charging-station/ui-server/UIServerNet.test.ts: Added assertion that normalizeHost('localhost.:80') returns 'localhost'.

  • tests/utils/ConfigurationSchema.test.ts: Added test rejecting port-bearing allowedHosts entries; removed [::1]:8080 from the "valid entries" acceptance test.


  • 🔄 Regenerate and Update Summary
  • ✏️ Insert as PR Description (deletes this comment)
  • 🗑️ Delete comment
PR Bot Information

Version: 1.22.9

@hyperspace-pr-bot hyperspace-pr-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The PR is well-structured and the three changes (whitespace-aware nonEmpty, trailing-dot fix for localhost.:80, and port-rejection in allowedHosts) are each correct and well-tested. The main concern is in ConfigurationSchema.ts: the port-detection logic in the new allowedHosts refine function duplicates knowledge that already lives in normalizeHost/isValidPort, using two independent regex patterns instead of deriving port-absence from the normalization result itself. This creates a fragile maintenance surface where edge cases (e.g. leading-zero ports, future port-format changes) could silently diverge between the schema validator and the runtime matcher.

PR Bot Information

Version: 1.22.9

Comment thread src/utils/ConfigurationSchema.ts Resolved
Comment thread src/utils/ConfigurationSchema.ts Resolved
…edges

- ConfigurationSchema: reject allowedHosts entries that carry a port
  (e.g., 'gateway.example.com:8080', '[::1]:8080'). Runtime host
  matching strips ports, so a port-bearing entry was silently
  equivalent to its host-only form. Operators are now forced to
  spell out host-only entries at config load time, matching how
  matching actually works.
- UIServerNet: normalizeHost strips a trailing dot after the colon
  split rather than from the whole input, so a Host header like
  'localhost.:80' now resolves to 'localhost' instead of leaving
  the trailing dot intact and failing the allowlist match.
- UIServerAccessPolicy: nonEmpty treats whitespace-only forwarded
  values as absent, and hasForwardedHeaders consumes nonEmpty so
  the two helpers agree. A whitespace-only X-Forwarded-Proto no
  longer flips forwardedHeadersPresent to true and triggers an
  Ambiguous* denial.

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
@jerome-benoit
jerome-benoit force-pushed the feat/ui-gateway-access-policy branch from 67512ea to 89a305c Compare June 14, 2026 19:21
…ion writes

The onSocketError listener was previously attached only after the
prologue and bad-header guards had already written their rejection
responses, and was removed before the auth-failure rejection write.
A client TCP-reset between an upgrade event and the destroy callback
could fire 'error' on the Duplex socket with no listener, which Node
escalates to an unhandled exception and crashes the process.

The listener is now attached at the top of the upgrade handler and
only removed once webSocketServer.handleUpgrade takes ownership of
the socket. All three rejection paths (bad upgrade headers, prologue
failure, auth failure) keep the listener until socket.destroy(),
neutralising the DoS vector.

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
…ded parsing

- Schema: split UIServerListenOptions validation so non-object values
  surface 'must be a non-array object' instead of the misleading
  'accessPolicy must be configured under uiServer' message.
- Forwarded parser: unescape RFC 7230 quoted-pair sequences inside
  double-quoted Forwarded parameter values.
- getForwardedClientAddress: propagate forwarded.kind === 'error' before
  the trustedProxy gate, matching getForwardedProtocol and
  getForwardedHost (no behavioral change today; closes a latent
  asymmetry if the upstream untrusted-peer gate is reordered).
- UIServerAccessCache: document identity-based cache invalidation for
  trustedProxies; reload flows must construct a new configuration.
- getForwardedProtocol / getForwardedHost: distinguish length === 0
  (new InvalidForwardedProtocol / InvalidForwardedHost reasons) from
  length > 1 (existing Ambiguous reasons), matching the existing
  InvalidForwardedClient / AmbiguousForwardedClient pattern.

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
@jerome-benoit jerome-benoit changed the title fix(ui-server): reject ports in allowedHosts, fix host normalization edges fix(ui-server): post-#1891 access policy and WS upgrade hardening Jun 14, 2026
@jerome-benoit
jerome-benoit merged commit bf4bc1b into main Jun 14, 2026
44 checks passed
@jerome-benoit
jerome-benoit deleted the feat/ui-gateway-access-policy branch October 29, 2026 23:47
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