mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 17:29:23 +00:00
Fix bug stripping HTML with angle bracket characters in literals. Fixes #5339.
This commit is contained in:
1 parent
808403b005
commit
d44b3b9213
4 files changed
+63
-5
No files matched your search
@@ -144,9 +144,7 @@ private enum StripHTML {
|
||||
// MARK: Main entry (called from `String.strippingHTML`)
|
||||
|
||||
/// Strip HTML tags from a UTF-8 byte buffer, writing the plain-text
|
||||
/// result into `output`. Produces byte-for-byte the same output as the
|
||||
/// previous `striphtml.c`, so the expected-output fixtures in
|
||||
/// `StripHTMLTests.swift` stay valid.
|
||||
/// result into `output`.
|
||||
///
|
||||
/// Behavior:
|
||||
/// - Tags (`<…>`) are removed; their inner markup is discarded.
|
||||
@@ -167,6 +165,9 @@ private enum StripHTML {
|
||||
/// the style pair). While either is true, a `<` does **not** bump
|
||||
/// `tagLevel`, so the `>` at the end of the closing tag is what brings
|
||||
/// `tagLevel` back to zero. That's the one subtle bit of the algorithm.
|
||||
/// - `inQuote` / `quoteByte`: true while inside a quoted attribute value
|
||||
/// within a tag. While true, `<` and `>` are literal content, so a `>`
|
||||
/// inside a media query like `"(width >= 700px)"` doesn't close the tag.
|
||||
/// - `lastCharacterWasSpace`: prevents doubled spaces. Starts `true` so
|
||||
/// leading whitespace is skipped without a separate branch.
|
||||
/// - `charactersAdded`: counts output characters for the `maxCharacters`
|
||||
@@ -191,6 +192,8 @@ private enum StripHTML {
|
||||
var inStyle = false
|
||||
var lastCharacterWasSpace = true
|
||||
var charactersAdded = 0
|
||||
var inQuote = false
|
||||
var quoteByte: UInt8 = 0
|
||||
|
||||
while inputIndex < inputCount && outputIndex < outputCapacity {
|
||||
|
||||
@@ -202,6 +205,12 @@ private enum StripHTML {
|
||||
|
||||
if byte == UInt8(ascii: "<") {
|
||||
|
||||
// A '<' inside a quoted attribute value is literal content.
|
||||
if inQuote {
|
||||
inputIndex += 1
|
||||
continue
|
||||
}
|
||||
|
||||
if !inScript && !inStyle {
|
||||
tagLevel += 1
|
||||
}
|
||||
@@ -242,6 +251,12 @@ private enum StripHTML {
|
||||
}
|
||||
|
||||
if byte == UInt8(ascii: ">") {
|
||||
// A '>' inside a quoted attribute value is literal content — it
|
||||
// does not close the tag.
|
||||
if inQuote {
|
||||
inputIndex += 1
|
||||
continue
|
||||
}
|
||||
if !inScript && !inStyle && tagLevel > 0 {
|
||||
tagLevel -= 1
|
||||
}
|
||||
@@ -249,6 +264,22 @@ private enum StripHTML {
|
||||
continue
|
||||
}
|
||||
|
||||
// Track quoted attribute values inside a tag so that '<' and '>'
|
||||
// appearing inside them — e.g. a media query like "(width >= 700px)" —
|
||||
// are treated as literal characters, not as tag delimiters.
|
||||
if tagLevel > 0 && !inScript && !inStyle && (byte == UInt8(ascii: "\"") || byte == UInt8(ascii: "'")) {
|
||||
if inQuote {
|
||||
if byte == quoteByte {
|
||||
inQuote = false
|
||||
}
|
||||
} else {
|
||||
inQuote = true
|
||||
quoteByte = byte
|
||||
}
|
||||
inputIndex += 1
|
||||
continue
|
||||
}
|
||||
|
||||
if tagLevel > 0 || inScript || inStyle {
|
||||
inputIndex += 1
|
||||
continue
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
<figure><picture>
|
||||
<source media="(resolution >= 2x) and (width >= 700px)" srcset="https://unsung.aresluna.org/_media/no-one-knows-who-patient-zero-was/1.2096w.avif" type="image/avif">
|
||||
<source media="(resolution >= 3x) or (width >= 700px)" srcset="https://unsung.aresluna.org/_media/no-one-knows-who-patient-zero-was/1.1600w.avif" type="image/avif">
|
||||
<source srcset="https://unsung.aresluna.org/_media/no-one-knows-who-patient-zero-was/1.1088w.avif" type="image/avif">
|
||||
<source srcset="https://unsung.aresluna.org/_media/no-one-knows-who-patient-zero-was/1.1088w.jpg" type="image/jpeg">
|
||||
<img src="https://unsung.aresluna.org/_media/no-one-knows-who-patient-zero-was/1.1088w.avif" alt="" width="800" height="450">
|
||||
</picture></figure>
|
||||
<blockquote><p>If you stepped into the dwarven capital of Ironforge on September 13, 2005, you would find only bones. Lots of bones. The city, along with every other major population center in World of Warcraft, had been ravaged by a plague that slaughtered players by the thousands, their bleached bones covering every street.</p></blockquote>
|
||||
<p>This is the beginning of the retelling of one of the most infamous bugs in videogame history, <a href="https://www.pcgamer.com/how-blizzard-coped-with-world-of-warcrafts-blood-plague-and-other-early-disasters/" target="_blank">written by Steven Messner in 2019</a>. It’s a surprisingly thrilling read. </p>
|
||||
<p>The TL; DR of the whole issue is that during a specific special event in World of Warcraft featured a big bad boss who actually stole blood off of players to replenish his own health. The fun narrative idea was that players were meant to infect <em>themselves</em> with a virus called Corrupted Blood, to trick the supervillain into getting infected, too.</p>
|
||||
<p>Things worked well except… the virus escaped containment.</p>
|
||||
<blockquote><p>“The corrupted blood was an effect and the designers forgot to clear it off your pet, so if your pet got despawned while it was in the encounter, it would save your pet with corrupted blood on it. The next time you summoned your pet there was no code to go «Oh you’re not in the raid, we should get rid of the corrupted blood.»” </p></blockquote>
|
||||
<p>If this reminds you of something, yeah, <a href="https://unsung.aresluna.org/durial321-is-a-banned-runescape-player-and-a-bug-abuser/">RuneScape had a similar incident a few months later in 2006</a>. Here, it was also similarly tricky for the developers to figure out how to restore order:</p>
|
||||
<blockquote><p>“Our choices were either to go through every pet in every server in every country in the entire world and check if it had corrupted blood and get rid of it, or get really hacky code in where every time you summoned a pet it would check and see if it had corrupted blood on it and get rid of it.” […] Despite numerous hotfixes, it was nearly a month until Blizzard fixed the problem completely by making it impossible for pets to contract the disease. </p></blockquote>
|
||||
<p>The disaster had a few interesting codas. The first one was that World of WarCraft and other games eventually started occasionally introducing an epidemic – now 100% intentional – as special events in their games.</p>
|
||||
<p>The second one? The accidental in-game event helped researchers understand actual real-life epidemics. As <a href="https://en.wikipedia.org/wiki/Corrupted_Blood_incident#As_a_research_model" target="_blank">summarized on Wikipedia</a>:</p>
|
||||
<blockquote><p>Of particular interest to researchers in the use of MMORPGs for epidemiology is that character responses to a virtual pandemic are the result of individual player reactions, adding “a level of authenticity that doesn’t exist in other simulations”. Disease researchers typically study disease spread and control through the use of three general models, all of which make significant assumptions about human behavior. As behavior is difficult to predict, the effectiveness of these models is limited.</p></blockquote><p><a href="https://unsung.aresluna.org/tags/bug-deep-dives/">#bug deep dives</a> <a href="https://unsung.aresluna.org/tags/bugs/">#bugs</a> <a href="https://unsung.aresluna.org/tags/games/">#games</a></p>
|
||||
@@ -0,0 +1 @@
|
||||
If you stepped into the dwarven capital of Ironforge on September 13, 2005, you would find only bones. Lots of bones. The city, along with every other major population center in World of Warcraft, had been ravaged by a plague that slaughtered players by the thousands, their bleached bones covering every street. This is the beginning of the retelling of one of the most infamous bugs in videogame history, written by Steven Messner in 2019. It’s a surprisingly thrilling read. The TL; DR of the whole issue is that during a specific special event in World of Warcraft featured a big bad boss who actually stole blood off of players to replenish his own health. The fun narrative idea was that players were meant to infect themselves with a virus called Corrupted Blood, to trick the supervillain into getting infected, too. Things worked well except… the virus escaped containment. “The corrupted blood was an effect and the designers forgot to clear it off your pet, so if your pet got despawned while it was in the encounter, it would save your pet with corrupted blood on it. The next time you summoned your pet there was no code to go «Oh you’re not in the raid, we should get rid of the corrupted blood.»” If this reminds you of something, yeah, RuneScape had a similar incident a few months later in 2006. Here, it was also similarly tricky for the developers to figure out how to restore order: “Our choices were either to go through every pet in every server in every country in the entire world and check if it had corrupted blood and get rid of it, or get really hacky code in where every time you summoned a pet it would check and see if it had corrupted blood on it and get rid of it.” […] Despite numerous hotfixes, it was nearly a month until Blizzard fixed the problem completely by making it impossible for pets to contract the disease. The disaster had a few interesting codas. The first one was that World of WarCraft and other games eventually started occasionally introducing an epidemic – now 100% intentional – as special events in their games. The second one? The accidental in-game event helped researchers understand actual real-life epidemics. As summarized on Wikipedia: Of particular interest to researchers in the use of MMORPGs for epidemiology is that character responses to a virtual pandemic are the result of individual player reactions, adding “a level of authenticity that doesn’t exist in other simulations”. Disease researchers typically study disease spread and control through the use of three general models, all of which make significant assumptions about human behavior. As behavior is difficult to predict, the effectiveness of these models is limited. #bug deep dives #bugs #games
|
||||
@@ -91,7 +91,16 @@ struct StripHTMLTests {
|
||||
#expect("<a href=\"#\"><span>link</span></a>".strippingHTML() == "link")
|
||||
}
|
||||
|
||||
@Test(arguments: ["daringfireball", "apple", "inessential", "scripting"])
|
||||
@Test func greaterThanInsideQuotedAttribute() {
|
||||
// Issue #5339: a '>' inside a quoted attribute value (a CSS media query)
|
||||
// must not be treated as the end of the tag.
|
||||
let html = "<source media=\"(resolution >= 2x) and (width >= 700px)\" type=\"image/avif\"><p>Hello</p>"
|
||||
#expect(html.strippingHTML() == "Hello")
|
||||
#expect("<a title=\"a > b\">link</a>".strippingHTML() == "link")
|
||||
#expect("<div data-x='1 > 0'>text</div>".strippingHTML() == "text")
|
||||
}
|
||||
|
||||
@Test(arguments: ["daringfireball", "apple", "inessential", "scripting", "unsung"])
|
||||
func withRealWorldHTML(testFile: String) throws {
|
||||
let url = try #require(Bundle.module.url(forResource: testFile, withExtension: "html", subdirectory: "Resources"))
|
||||
let html = try String(contentsOf: url, encoding: .utf8)
|
||||
@@ -103,7 +112,7 @@ struct StripHTMLTests {
|
||||
#expect(!result.contains("//"), "Should fully remove script content")
|
||||
}
|
||||
|
||||
@Test(arguments: ["apple", "daringfireball", "inessential", "scripting"])
|
||||
@Test(arguments: ["apple", "daringfireball", "inessential", "scripting", "unsung"])
|
||||
func matchesExpectedOutput(testFile: String) throws {
|
||||
let htmlURL = try #require(Bundle.module.url(forResource: testFile, withExtension: "html", subdirectory: "Resources"))
|
||||
let txtURL = try #require(Bundle.module.url(forResource: testFile, withExtension: "txt", subdirectory: "Resources"))
|
||||
|
||||
Reference in new issue
Block a user