Skip to content

Context-specific encoding helpers, CsrfHelper split - #157

Merged
young-steveo merged 3 commits into
mainfrom
context-encoding-helpers
Apr 12, 2026
Merged

Context-specific encoding helpers, CsrfHelper split#157
young-steveo merged 3 commits into
mainfrom
context-encoding-helpers

Conversation

@young-steveo

Copy link
Copy Markdown
Collaborator

Summary

  • Split CsrfHelper from HtmlHelper — CSRF methods (field(), token()) moved to a dedicated CsrfHelper that owns the ActiveSession dependency. Registered as Csrf alias. {{ csrf }} directive updated to Csrf::field().
  • HtmlHelper is now dependency-free with four OWASP-aligned encoding methods:
    • Html::url($href) — URL scheme validation (rejects javascript:, data:, etc.)
    • Html::js($value) — JavaScript string encoding (whitelist, non-alphanumeric → \uHHHH)
    • Html::attr($value) — strict HTML attribute encoding (non-alphanumeric → &#xHH;)
    • Html::css($value) — CSS hex encoding (non-alphanumeric → \HEX )
  • Html helper always available — no longer gated behind ActiveSession check in bootstrap
  • Documentation updated — Shodo README with encoding context guide, Htmx README (Csrf::token()), COMPENDIUM with built-in helper list
  • Starter app needs a follow-up to update Html::csrf()Csrf::field() and Html::csrfToken()Csrf::token()

Test plan

  • composer check passes (cs-fix, phpstan level 9, phpunit — 2774 tests, 5546 assertions)
  • New CsrfHelper tests (field, token, match)
  • 28 new HtmlHelper tests covering url/js/attr/css encoding edge cases
  • Updated compiler and formatter tests for Csrf::field() directive
  • Smoke test starter app (will need Csrf alias update first)

Generated with Claude Code

young-steveo and others added 3 commits April 11, 2026 21:36
HtmlHelper is now dependency-free with context-specific encoding:
- Html::url() — scheme validation (rejects javascript:, data:, etc.)
- Html::js() — JS string encoding (\uHHHH whitelist)
- Html::attr() — strict HTML attribute encoding (&#xHH;)
- Html::css() — CSS hex encoding (\HEX)
Plus existing nonce() and classIf().

CsrfHelper owns the ActiveSession dependency with field() and
token(). Registered as Csrf alias. {{ csrf }} directive updated
to Csrf::field().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Shodo README: encoding context guide with Html::url/js/attr/css
examples, updated helper table (Html is now dependency-free, Csrf
owns ActiveSession), {{ csrf }} maps to Csrf::field().
Htmx README: Html::csrfToken() → Csrf::token().
COMPENDIUM: built-in helpers listed in Shodo description.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Starter app Csrf references are the only remaining item — separate
repo, will update when this PR merges.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@young-steveo
young-steveo merged commit 1ee4840 into main Apr 12, 2026
1 check passed
@young-steveo
young-steveo deleted the context-encoding-helpers branch April 12, 2026 01:43
young-steveo added a commit to arcanum-org/arcanum that referenced this pull request Apr 12, 2026
Framework split CsrfHelper from HtmlHelper (arcanum-org/framework#157).
CSRF methods now live on the Csrf alias.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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