Skip to content

v0.9.0 — Security hardening: signed actions, session-bound snapshots, validated markup

Choose a tag to compare

@polidog polidog released this 31 Oct 05:39
· 78 commits to main since this release
e98c268

Highlights

A security-hardening release. A server-side audit (issues #31, #37–#43, #49, #52, #53) and a client-script audit (#35) found that the browser could tamper with hook state, plant snapshots into other users' sessions, make the server execute arbitrary PHP functions through event props, and break out of tags via attribute names. All of them are fixed, the trust boundaries are documented, and the test suite now actually runs to completion.

⚠️ Breaking changes

  • Actions are signed (#47). Every rendered action form carries _usephp_action_sig, an HMAC over the instance id, the exact action JSON and the session id. doHandleAction() rejects any non-restore action without a valid signature. The key is the snapshot secret when setSnapshotSecret() was called, otherwise a random per-session key. Apps with no secret and no PHP session now get 400 on every action until they call setSnapshotSecret(). Custom form rendering and hand-written tests must call UsePHP::signAction($instanceId, $json).
  • Event props accept only Action or \Closure (#46). createElement() no longer runs is_callable() on onClick / onChange / …; strings, arrays, invokable objects and ints throw InvalidArgumentException. The H::* $on* parameters are narrowed to Action|\Closure|null.
  • Tag and attribute names are validated (#50). Tag names must match [A-Za-z][A-Za-z0-9-]*, attribute names [A-Za-z_:][A-Za-z0-9:_.-]*, and any string prop named on* (onclick, onerror, onLoad as a string) is refused. Violations throw RuntimeException. data-*, aria-*, SVG names, xlink:href, viewBox, custom elements all pass unchanged.
  • Failed partial submits are not replayed (#35). A non-2xx or thrown partial POST no longer falls back to form.submit(). A cancelable usephp:submit-error event is dispatched and the component gets data-usephp-error.
  • setDeferPrefix() users must pass the same prefix to renderClientScript($src, $deferPrefix) (#35), which now emits window.usePHP.deferPrefix.
  • session_regenerate_id() invalidates previously rendered action signatures and stored Session/Shared snapshots, same as the CSRF token.

Security fixes

  • Client-writable hook state / storageType / instance id (#37, #41, #42 → #47). Action::fromArray() validates every field's shape; malformed JSON is 400 Invalid action data instead of an uncaught TypeError. Hook index is bounded to [0, 255], _usephp_component must contain #, be ≤ 255 bytes and carry no control characters, and the snapshot memo must name the posted instance.
  • Open redirect via REQUEST_URI (#38 → #44). The post-action 303 Location is built by normalizeRedirectPath(): exactly one leading /, no \, no query string, fallback to / on control characters. usephp.js only follows same-origin redirects.
  • Session snapshot injection (#39 → #48). Session/Shared snapshots are HMAC-bound to session_id() via SnapshotSerializer::withContext(). A ?_snapshot= on a Session route is ignored and logged. The empty context is byte-identical to the old payload, so Persistent and plain Snapshot round trips are unchanged.
  • Session/Shared snapshots were never restored (#52, #53 → #54). handleSnapshotRestoration() returned early without ?_snapshot=, so Shared only restored by accident and Session state was write-only. Both now restore from $_SESSION on every GET through the session-bound serializer.
  • onClick string callable executed (#40 → #46) and tag/attribute name injection (#31 → #50), see breaking changes above.
  • Client script (#35). data-usephp-defer-url is only hydrated when same-origin, under the defer prefix, and text/html. Fragments containing a usePHP form, CSRF token or snapshot are never written to localStorage even with Defer::$localCache.

Docs

  • README (en/ja): new "What usePHP does not protect (trust boundaries)" section (#45), plus subsections for signed actions, the deferred fetch guard, non-replayed partial submits, tag/attribute validation, and the session binding of snapshots.
  • docs/: UsePHP decomposition design note (#36).

Tests

  • The PRG redirect exit in doHandleAction() used to kill the PHPUnit process silently with exit code 0, so roughly 80 tests never ran and CI stayed green (#49 → #51). UsePHP::withExitHandler() / withHeaderEmitter() are the test seams; the suite now completes.
  • 357 → 464 tests, 1122 assertions, all green. PHPStan and php-cs-fixer clean.

Full Changelog: v0.8.0...v0.9.0