Skip to content

fix(security): remove is_safe html from Twig serial helpers - #18

Merged
HecFranco merged 2 commits into
mainfrom
security/remove-is-safe-html
Aug 5, 2026
Merged

fix(security): remove is_safe html from Twig serial helpers#18
HecFranco merged 2 commits into
mainfrom
security/remove-is-safe-html

Conversation

@HecFranco

@HecFranco HecFranco commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Remove Twig is_safe => ['html'] from serial_number / serial_number_mask
  • Document auto-escaping behavior in SECURITY.md
  • Unit tests assert getSafe() is null

Fixes #17

Test plan

  • Review docs/code diffs for accuracy
  • Confirm no unintended scope beyond this security remediation
  • CI green on this branch

Made with Cursor

Summary by CodeRabbit

  • Bug Fixes

    • Improved protection against cross-site scripting by ensuring serial numbers are automatically HTML-escaped in Twig templates.
    • Serial-number output is no longer treated as pre-approved HTML content, reducing the risk of unsafe markup being rendered.
  • Documentation

    • Updated security guidance to recommend keeping auto-escaping enabled, avoiding unsafe raw output, and applying context-appropriate escaping outside HTML.

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: eb14e3d3-9673-4dc1-a8a3-dae7bc86d027

📥 Commits

Reviewing files that changed from the base of the PR and between 5db858a and 1f346c8.

📒 Files selected for processing (2)
  • demo/symfony8/config/reference.php
  • tests/Unit/Twig/SerialNumberTwigExtensionTest.php

📝 Walkthrough

Walkthrough

The Twig serial-number function and filter no longer declare HTML-safe output. Tests verify the registrations, and security documentation describes auto-escaping and safe handling of unsanitized values. The Symfony 8 reference configuration enables strict typing.

Changes

Serial helper escaping

Layer / File(s) Summary
Twig HTML safety registration and verification
src/Twig/SerialNumberTwigExtension.php, tests/Unit/Twig/SerialNumberTwigExtensionTest.php, docs/SECURITY.md
The serial_number function and serial_number_mask filter no longer set is_safe => ['html']. Tests assert that both registrations return empty safety metadata from getSafe. Security guidance documents default auto-escaping and cautions against `

Demo strict typing

Layer / File(s) Summary
Symfony 8 reference configuration typing
demo/symfony8/config/reference.php
The reference configuration adds declare(strict_types=1);.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes satisfy issue #17 by removing HTML-safe registration, documenting auto-escaping, and adding unit-test coverage.
Out of Scope Changes check ✅ Passed All changes directly support issue #17 and contain no unrelated code or documentation updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the security fix: removing HTML-safe registration from Twig serial helpers.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch security/remove-is-safe-html

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
docs/SECURITY.md (1)

50-54: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Security Misconfiguration (CWE-16)

Make the auto-escaping precondition explicit.

Twig respects the configured auto-escaping setting; if it is disabled, removing is_safe does not escape these outputs. Reword the first sentence to describe this conditional behavior before the “keep auto-escaping enabled” guidance.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/SECURITY.md` around lines 50 - 54, Update the first sentence in the
serial_number and serial_number_mask guidance to state that their output is
auto-escaped only when Twig auto-escaping is enabled; preserve the existing
recommendation to keep auto-escaping enabled and avoid |raw for unsanitized
values.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/SECURITY.md`:
- Around line 50-54: Add an Unreleased security changelog note in
docs/CHANGELOG.md for the Twig serial_number and serial_number_mask XSS
guidance, using the existing 2.6/Unreleased Security section or the nearest
equivalent entry. Keep the note focused on the visible behavior that these Twig
outputs are not HTML-safe and remain auto-escaped by default, and do not add an
UPGRADING.md change unless the changelog entry implies an integration action.

---

Nitpick comments:
In `@docs/SECURITY.md`:
- Around line 50-54: Update the first sentence in the serial_number and
serial_number_mask guidance to state that their output is auto-escaped only when
Twig auto-escaping is enabled; preserve the existing recommendation to keep
auto-escaping enabled and avoid |raw for unsanitized values.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 23c8e83a-651c-4556-9647-88dc18f82167

📥 Commits

Reviewing files that changed from the base of the PR and between dc69557 and 5db858a.

📒 Files selected for processing (3)
  • docs/SECURITY.md
  • src/Twig/SerialNumberTwigExtension.php
  • tests/Unit/Twig/SerialNumberTwigExtensionTest.php
💤 Files with no reviewable changes (1)
  • src/Twig/SerialNumberTwigExtension.php

Comment thread docs/SECURITY.md
Comment on lines +50 to +54
Twig function `serial_number` and filter `serial_number_mask` do **not** mark output as HTML-safe (`is_safe` is not set). Twig **auto-escapes** their results in HTML templates by default.

- **Recommendation:** Do not pass unsanitized user input (forms, query strings, etc.) directly into `serial_number()` or `serial_number_mask()` without validating/escaping the result for HTML.
- If serials or context values may contain user content:
- Escape in the template (e.g. `{{ serial|serial_number_mask(4)|e }}` when you need escaping for that value), or
- Ensure values are sanitized before they reach the bundle.

The bundle does not HTML-escape; the application must use serials safely in HTML context.
- Prefer application-controlled values (e.g. system-generated invoice numbers).
- If serials or context values may contain user content, keep auto-escaping enabled; do not pipe through `|raw` unless the value is already sanitized for HTML.
- Outside HTML contexts (e.g. attributes, JS), escape appropriately for that context.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

test -f docs/CHANGELOG.md
test -f docs/UPGRADING.md

rg -n -i 'unreleased|serial_number|serial_number_mask|html-safe|auto.?escap' \
  docs/CHANGELOG.md docs/UPGRADING.md

Repository: nowo-tech/SerialNumberBundle

Length of output: 1067


🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

echo "== CHANGELOG around Unreleased/security-related entries =="
sed -n '1,120p' docs/CHANGELOG.md

echo
echo "== CHANGELOG serial_number mentions around end =="
sed -n '180,215p' docs/CHANGELOG.md

echo
echo "== UPGRADING Twig section =="
sed -n '80,115p' docs/UPGRADING.md

echo
echo "== SECURITY section context =="
sed -n '35,65p' docs/SECURITY.md

Repository: nowo-tech/SerialNumberBundle

Length of output: 11206


Add the Unreleased changelog entry for Twig XSS guidance.

docs/SECURITY.md documents visible XSS guidance for serial_number / serial_number_mask, but docs/CHANGELOG.md has no Unreleased entry covering this security behavior. Add it under docs/CHANGELOG.md section 2.6 (or the appropriate Unreleased Security entry) so this user-visible mitigation is recorded. No UPGRADING.md change is needed unless integrators must change code/config.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/SECURITY.md` around lines 50 - 54, Add an Unreleased security changelog
note in docs/CHANGELOG.md for the Twig serial_number and serial_number_mask XSS
guidance, using the existing 2.6/Unreleased Security section or the nearest
equivalent entry. Keep the note focused on the visible behavior that these Twig
outputs are not HTML-safe and remain auto-escaped by default, and do not add an
UPGRADING.md change unless the changelog entry implies an integration action.

Source: Path instructions

@HecFranco HecFranco changed the title security: remove is_safe html from Twig serial helpers fix(security): remove is_safe html from Twig serial helpers Aug 5, 2026
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
⚠️ Action not completed

Pull request is closed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@HecFranco
HecFranco merged commit e45688b into main Aug 5, 2026
25 of 27 checks passed
@HecFranco
HecFranco deleted the security/remove-is-safe-html branch August 5, 2026 10:42
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.

security: remove is_safe html from Twig serial helpers

1 participant