Skip to content

Keep XML reports well-formed for control characters and combining marks - #6975

Open
Joseph-Cursio wants to merge 1 commit into
realm:mainfrom
Joseph-Cursio:xml-reports-well-formed
Open

Joseph-Cursio wants to merge 1 commit into
realm:mainfrom
Joseph-Cursio:xml-reports-well-formed

Conversation

@Joseph-Cursio

Copy link
Copy Markdown

Fixes #6965.

checkstyle, junit and gitlab reports are now well-formed XML when a reason, file path or rule identifier contains a C0 control character, U+FFFE or U+FFFF, or <, >, &, " or ' followed by a combining mark. A typical way in is an ESC pasted into a // TODO: comment. escapedForXML() now walks Unicode scalars and writes U+FFFD for each character XML 1.0 cannot hold, and GitLabJUnitReporter escapes the rule identifier.

Text without these characters is escaped byte for byte as before, so the canned reports in ReporterTests are unchanged. HTMLReporter also calls escapedForXML(), so its output changes the same way for these inputs. Against the old code, 5 of the 6 new tests fail. With the fix the full swift test passes, apart from SwiftVersionTests.detectSwiftVersion, which fails the same way without it because it expects 6.4.0 and the local toolchain is 6.4.2. swift run swiftlint lint --strict is clean.

Full write-up (Claude Opus 5.5)

Why the old escaping let these through

  • Control characters. XML 1.0 does not allow U+0001–U+0008, U+000B, U+000C, U+000E–U+001F, U+FFFE or U+FFFF anywhere in a document, not even as a character reference (&#27; is just as malformed). The old code copied them through.
  • Markup + combining mark. replacingOccurrences(of:with:) matches whole Characters, and < followed by U+0301 is a single Character that is not <, so the < was written raw. The same goes for >: a reason containing ]]> + U+0301 put a raw ]]> into the GitLab <failure> body, which XML forbids in content.
  • GitLab rule identifier. GitLabJUnitReporter wrote violation.ruleIdentifier unescaped in the single-quoted testcase name and in the body, so a custom_rules key such as a<b, a&b or it's broke the report. CheckstyleReporter already escapes it, and JUnitReporter doesn't write it.

The change

  • Source/SwiftLintFramework/Extensions/String+XML.swift: one pass over unicodeScalars. It writes the same five entity replacements as before, keeps tab, line feed and carriage return, writes U+FFFD for the other C0 controls and for U+FFFE and U+FFFF, and copies every other scalar. C1 controls, DEL and the supplementary-plane noncharacters are legal XML 1.0 Chars, so they are copied through. The file no longer needs import Foundation.
  • Source/SwiftLintFramework/Reporters/GitLabJUnitReporter.swift: escape ruleIdentifier.

U+FFFD keeps the report readable and shows that something was there. Dropping the character would also produce well-formed XML.

Tests

Tests/FrameworkTests/XMLReporterWellFormedTests.swift (Swift Testing). It sits in FrameworkTests rather than next to ReporterTests because it reads no files.

Test What it covers On the old code
reasonKeepsReportWellFormed 9 reasons, each rendered with all three XML reporters and parsed with XMLParser: ESC, U+0001, U+FFFE, < / & / " / ' + U+0301, ]]> + U+0301, and a control case with the five plain escapes 8 cases fail, 18 issues; the control passes
filePathKeepsReportWellFormed a file name containing U+0001 and < + U+0301 fails, all 3 reporters
ruleIdentifierKeepsReportWellFormed a<b, a&b, it's, a' + U+0301 + b all 4 fail (GitLab)
escapingReplacesCharactersXMLCannotHold which scalars become U+FFFD; tab, LF, CR, CRLF, U+007F, U+0085, U+FFFD and U+1FFFE are kept fails
escapingHandlesMarkupBeforeCombiningMarks each of the five markup characters + U+0301; U+226E is copied while < + U+0338 is escaped fails
escapingLeavesOtherTextUnchanged ordinary text, &amp;, decomposed and precomposed accents, emoji and a ZWJ sequence passes (regression guard)

The direct tests compare Unicode scalars rather than using String's ==. == treats canonically equivalent text as equal, so it could not catch a change that normalizes the output.

One test keeps its literals in local constants. On Swift 6.4.2, the #expect macro's expansion does not compile when a function-call argument contains \" followed by a combining mark.

Verification

  • swift test --filter "XMLReporterWellFormedTests|ReporterTests": all pass.
  • Old source with the new tests: 5 of the 6 tests fail, with 29 issues.
  • Full swift test (1,121 tests, SKIP_INTEGRATION_TESTS=true) on macOS 27.2, Xcode 27.2, Swift 6.4.2: everything passes except SwiftVersionTests.detectSwiftVersion, which is a toolchain-version mismatch unrelated to this change.
  • swift run swiftlint lint --strict: no violations.
  • Before opening the PR, an independent review checked:
    • every scalar from U+0000 to U+10FFFF against XML 1.0's Char production, with no mismatches;
    • each reporter template with 24 hostile inputs through xmllint and XMLParser: every new report parses, and the old escaping fails 86 of the 216 XML reports;
    • that corelibs XMLParser on Linux, which uses libxml2's push parser, rejects the same old reports as macOS does.

🤖 Generated with Claude Code

`escapedForXML()` now walks Unicode scalars, so `<`, `>`, `&`, `"` or `'`
before a combining mark is escaped, and writes U+FFFD for each character
XML 1.0 cannot hold. `GitLabJUnitReporter` now escapes the rule
identifier. Text without these characters is escaped exactly as before;
`HTMLReporter` output changes the same way for these inputs. Fixes realm#6965.

Details:

- XML 1.0 forbids U+0001-U+0008, U+000B, U+000C, U+000E-U+001F, U+FFFE and
  U+FFFF even as character references, so they are replaced, not escaped.
  Tab, line feed and carriage return are kept.
- `replacingOccurrences(of:with:)` matched whole `Character`s, and `<`
  followed by U+0301 is one `Character` that is not `<`, so the `<` was
  written raw.
- A `custom_rules` key such as `a<b` or `it's` broke the `gitlab` report,
  which wrote the identifier unescaped in a single-quoted attribute and in
  the body.
- New `XMLReporterWellFormedTests` in FrameworkTests render each case with
  the checkstyle, JUnit and GitLab reporters and parse the report with
  `XMLParser`, plus direct tests of `escapedForXML()` that compare Unicode
  scalars. Against the old code 5 of the 6 tests fail with 29 issues; the
  canned reports in `ReporterTests` are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@SwiftLintBot

Copy link
Copy Markdown
20 Messages
📖 Building this branch resulted in a binary size of 28772.54 KiB vs 28772.41 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.68 s vs 0.69 s on main (1% faster).
📖 Linting Alamofire with this PR took 0.98 s vs 0.97 s on main (1% slower).
📖 Linting Brave with this PR took 5.52 s vs 5.47 s on main (0% slower).
📖 Linting Brigade with this PR took 18.13 s vs 18.18 s on main (0% faster).
📖 Linting DuckDuckGo with this PR took 35.96 s vs 35.76 s on main (0% slower).
📖 Linting Firefox with this PR took 12.43 s vs 12.37 s on main (0% slower).
📖 Linting Kickstarter with this PR took 7.31 s vs 7.32 s on main (0% faster).
📖 Linting Moya with this PR took 0.38 s vs 0.38 s on main (0% slower).
📖 Linting NetNewsWire with this PR took 2.82 s vs 2.83 s on main (0% faster).
📖 Linting Nimble with this PR took 0.57 s vs 0.56 s on main (1% slower).
📖 Linting PocketCasts with this PR took 7.6 s vs 7.59 s on main (0% slower).
📖 Linting Quick with this PR took 0.36 s vs 0.37 s on main (2% faster).
📖 Linting Realm with this PR took 2.97 s vs 2.97 s on main (0% slower).
📖 Linting Sourcery with this PR took 1.55 s vs 1.53 s on main (1% slower).
📖 Linting Swift with this PR took 4.68 s vs 4.68 s on main (0% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 0.18 s vs 0.18 s on main (0% slower).
📖 Linting VLC with this PR took 1.49 s vs 1.48 s on main (0% slower).
📖 Linting Wire with this PR took 17.07 s vs 17.06 s on main (0% slower).
📖 Linting WordPress with this PR took 10.92 s vs 10.93 s on main (0% faster).

Generated by 🚫 Danger

This branch has not been deployed

No deployments
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.

Checkstyle, JUnit and GitLab reports can be malformed XML: control characters and < before a combining mark get through

2 participants