Repository navigation
Keep XML reports well-formed for control characters and combining marks - #6975
Open
Joseph-Cursio wants to merge 1 commit into
Open
Joseph-Cursio wants to merge 1 commit into
Joseph-Cursio wants to merge 1 commit into
Conversation
`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]>
Generated by 🚫 Danger |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6965.
checkstyle,junitandgitlabreports 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, andGitLabJUnitReporterescapes the rule identifier.Text without these characters is escaped byte for byte as before, so the canned reports in
ReporterTestsare unchanged.HTMLReporteralso callsescapedForXML(), 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 fullswift testpasses, apart fromSwiftVersionTests.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 --strictis clean.Full write-up (Claude Opus 5.5)
Why the old escaping let these through
is just as malformed). The old code copied them through.replacingOccurrences(of:with:)matches wholeCharacters, and<followed by U+0301 is a singleCharacterthat 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.GitLabJUnitReporterwroteviolation.ruleIdentifierunescaped in the single-quotedtestcasename and in the body, so acustom_ruleskey such asa<b,a&borit'sbroke the report.CheckstyleReporteralready escapes it, andJUnitReporterdoesn't write it.The change
Source/SwiftLintFramework/Extensions/String+XML.swift: one pass overunicodeScalars. 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.0Chars, so they are copied through. The file no longer needsimport Foundation.Source/SwiftLintFramework/Reporters/GitLabJUnitReporter.swift: escaperuleIdentifier.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 toReporterTestsbecause it reads no files.reasonKeepsReportWellFormedXMLParser: ESC, U+0001, U+FFFE,</&/"/'+ U+0301,]]>+ U+0301, and a control case with the five plain escapesfilePathKeepsReportWellFormed<+ U+0301ruleIdentifierKeepsReportWellFormeda<b,a&b,it's,a'+ U+0301 +bescapingReplacesCharactersXMLCannotHoldescapingHandlesMarkupBeforeCombiningMarks<+ U+0338 is escapedescapingLeavesOtherTextUnchanged&, decomposed and precomposed accents, emoji and a ZWJ sequenceThe 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
#expectmacro's expansion does not compile when a function-call argument contains\"followed by a combining mark.Verification
swift test --filter "XMLReporterWellFormedTests|ReporterTests": all pass.swift test(1,121 tests,SKIP_INTEGRATION_TESTS=true) on macOS 27.2, Xcode 27.2, Swift 6.4.2: everything passes exceptSwiftVersionTests.detectSwiftVersion, which is a toolchain-version mismatch unrelated to this change.swift run swiftlint lint --strict: no violations.Charproduction, with no mismatches;xmllintandXMLParser: every new report parses, and the old escaping fails 86 of the 216 XML reports;XMLParseron Linux, which uses libxml2's push parser, rejects the same old reports as macOS does.🤖 Generated with Claude Code