Skip to content

v0.10.0 — Working routes, partial updates and forms: fixes from the v0.9.0 audit

Latest

Choose a tag to compare

@polidog polidog released this 31 Oct 12:27
e073bff

Highlights

An audit of v0.9.0 (issues #55–#98) found that the README quick start did not run, route middleware never executed, and state was lost in many ordinary setups. This release fixes 46 issues, makes actions work without an explicit session_start(), adds useFormAction, and runs usephp.js in a real browser on CI.

⚠️ Upgrade notes

  • Run ./vendor/bin/usephp publish after updating. The server and usephp.js changed together, for example the new form markup, _usephp_snapshots[] and the restore signature.
  • Forms rendered before the deploy get a 400. Action signatures now cover the page path. Cached snapshot restores made by older pages also get a 400, and usephp.js discards that saved snapshot.

⚠️ Breaking changes

  • Action forms are bound to their page (#56 → #107). A form can only be posted to the path it was rendered for. Hand-written forms use signAction($instanceId, $json, $pagePath). HTML rendered on one route and inserted into another page gets a 400; use a defer for that. Forms inside a defer are bound to the defer name and accepted on any page, where that page's guard still runs.
  • Route middleware now runs (#56 → #101). A guard that used to be ignored now applies to GET and to action POSTs. Defer endpoints are not covered, so a protected defer has to check permissions itself.
  • Unresolvable components throw (#74, #111). An unregistered class as a route handler, or an unknown name given to createElement() / render(), throws LogicException instead of returning an empty result. A defer whose target is unregistered answers 500 instead of an empty 200. Names that arrive from a request keep their 400 / 404.
  • Event props (#75). onFocus, onBlur, onLoad and onError throw. Supported events are onClick, onChange, onInput and onSubmit.
  • Hook values (#93). Values carried by actions and snapshots must be null, scalars or arrays. Anything else throws when it is rendered.
  • Component keys (#71). A fixed key combined with a props key becomes row/k1, so those components no longer see their earlier state.
  • Nested defers (#59). An inner defer needs a registered endpoint.
  • srcdoc (#58). A non-empty value throws.
  • Generated markup (#77). Action buttons are <button form="…"> with a separate hidden form. CSS or scripts that relied on the old wrapping form need updating.
  • style / script children are raw (#85). Do not put user input in them. A closing tag in the content throws.
  • PSX (#84). Built-in tags compile to createElement(), so attribute order follows the source. Cached output is recompiled once.

Security fixes

  • Route guards were never run (#56 → #101, #107). Includes the bypass of posting a guarded page's signed form to an unguarded URL, and of using restore to get a form signed for another page.
  • Session data in shared caches (#59 → #100, #109 → #114). Nested defers were inlined under the parent's public Cache-Control. A public defer could also send Set-Cookie, a CSRF token or a session-bound signature. Those responses are now private, no-store with a logged warning.
  • javascript: block bypass (#57 → #99). Tabs and newlines inside a URL are removed before the check. Attribute and scheme checks ignore case.
  • srcdoc scripts ran (#58 → #99).
  • Malformed snapshots caused a 500 (#86 → #101). ?_snapshot[]=1 hit every route. Invalid input is now a verification failure, and a bad POST is a 400.

Added

  • useFormAction(['field' => $setter]) (#76 → #105). Copies submitted fields into useState. Which field goes to which hook is signed. The values are not signed, so treat them as untrusted input.
  • Element events (#75 → #105). onClick on any element, plus onChange, onInput (300 ms debounce) and onSubmit. Without JavaScript, only onClick on buttons and submit or image inputs and onSubmit work.
  • Arbitrary attributes on every H::* tag (#94 → #105). H::div(title: 'x', role: 'alert'), H::button(...['aria-label' => 'x']).
  • Route middleware API (#56 → #101). Takes (array $params, RequestContext $request). Return null or true to continue, false for a 403, or a string or Element to answer with it. runRouteMiddleware() is available for custom front controllers.

Fixes

  • Actions and partial updates (#104). No session_start() needed (#55). fc() with Session or Memory updates in place (#78, #96). Class components keep Snapshot state on Session and Persistent routes (#63). Redirects keep the query string (#64). useRouter()['params'] survives a partial update (#65).
  • Snapshots (#106). Session and Shared store one snapshot per instance, up to 64 (#67). A parent update keeps its children's state (#68). Isolated routes without JavaScript answer the POST directly, so state is kept (#69).
  • Client (#103, #105, #106). Partial updates morph the DOM, so input, focus and selection survive (#79). A double click sends one POST (part of #80). Parent and child components update in turn, and fixed-value clicks made meanwhile are dropped.
  • Hooks (#100, #116). useEffect cleanup no longer crashes the session (#60, #61). setState(null) keeps null (#66). A parent's hooks stay with the parent after a child renders or render() is nested (#70, #73, #112). Int keys work (#72).
  • Rendering (#99, #105). Nested children are flattened (#62). Invalid UTF-8 no longer empties text (#88). aria-hidden={false} and similar attributes are kept (#95). A button inside an app's form no longer nests forms (#77).
  • Router (#101). Literal route text is escaped (#89). HEAD falls back to GET (#90). Parameters are decoded (#91).
  • PSX (#102). {/* comment */}, {$x && <b/>}, <style> and <script> compile correctly, and invalid output is a compile error (#81, #82). Entities are decoded (#83). {0} and width="100" work under strict_types (#84). usephp compile exits 1 on a missing path (#97).

Docs

  • The README quick start now runs, and CI executes the README code (#98 → #108). ->name(), generate(), navigate and isActive were removed from the docs because they never existed.
  • State sharing across route parameters is documented, with how to set keys (#92).

Tests

  • 464 → 1211 PHPUnit tests, 6220 assertions. PHPStan and php-cs-fixer clean.
  • New 79 Playwright tests for usephp.js run on CI (#110 → #117).

Known limitations

  • #80: with several tabs, the last write wins, and there is no fn($prev) update.
  • #87: HTML size grows with the square of the row count when one state holds a whole list. Reported to exceed memory_limit=128M at 1000 rows.

Full Changelog: v0.9.0...v0.10.0