Skip to content

[XMLParser] Fix reentrancy issue around currentParser #5061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 31 additions & 58 deletions Sources/FoundationXML/XMLParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,7 @@ extension XMLParser : @unchecked Sendable { }

open class XMLParser : NSObject {
private var _handler: _CFXMLInterfaceSAXHandler
#if !os(WASI)
internal var _stream: InputStream?
#endif
internal var _data: Data?

internal var _chunkSize = Int(4096 * 32) // a suitably large number for a decent chunk size
Expand All @@ -414,9 +412,6 @@ open class XMLParser : NSObject {

// initializes the parser with the specified URL.
public convenience init?(contentsOf url: URL) {
#if os(WASI)
return nil
#else
setupXMLParsing()
if url.isFileURL {
if let stream = InputStream(url: url) {
Expand All @@ -434,7 +429,6 @@ open class XMLParser : NSObject {
return nil
}
}
#endif
}

// create the parser from data
Expand All @@ -450,15 +444,13 @@ open class XMLParser : NSObject {
_CFXMLInterfaceDestroyContext(_parserContext)
}

#if !os(WASI)
//create a parser that incrementally pulls data from the specified stream and parses it.
public init(stream: InputStream) {
setupXMLParsing()
_stream = stream
_handler = _CFXMLInterfaceCreateSAXHandler()
_parserContext = nil
}
#endif

open weak var delegate: XMLParserDelegate?

Expand All @@ -469,33 +461,35 @@ open class XMLParser : NSObject {
open var externalEntityResolvingPolicy: ExternalEntityResolvingPolicy = .never

open var allowedExternalEntityURLs: Set<URL>?

#if os(WASI)
private static var _currentParser: XMLParser?
#endif

#if os(WASI)
/// The current parser associated with the current thread. (assuming no multi-threading)
/// FIXME: Unify the implementation with the other platforms once we unlock `threadDictionary`
/// or migrate to `FoundationEssentials._ThreadLocal`.
private static nonisolated(unsafe) var _currentParser: XMLParser? = nil
#else
/// The current parser associated with the current thread.
private static var _currentParser: XMLParser? {
get {
return Thread.current.threadDictionary["__CurrentNSXMLParser"] as? XMLParser
}
set {
Thread.current.threadDictionary["__CurrentNSXMLParser"] = newValue
}
}
#endif

/// The current parser associated with the current thread.
internal static func currentParser() -> XMLParser? {
#if os(WASI)
return _currentParser
#else
if let current = Thread.current.threadDictionary["__CurrentNSXMLParser"] {
return current as? XMLParser
} else {
return nil
}
#endif
}

internal static func setCurrentParser(_ parser: XMLParser?) {
#if os(WASI)

/// Execute the given closure with the current parser set to the given parser.
internal static func withCurrentParser<R>(_ parser: XMLParser, _ body: () -> R) -> R {
let oldParser = _currentParser
_currentParser = parser
#else
if let p = parser {
Thread.current.threadDictionary["__CurrentNSXMLParser"] = p
} else {
Thread.current.threadDictionary.removeObject(forKey: "__CurrentNSXMLParser")
}
#endif
defer { _currentParser = oldParser }
return body()
}

internal func _handleParseResult(_ parseResult: Int32) -> Bool {
Expand Down Expand Up @@ -569,7 +563,6 @@ open class XMLParser : NSObject {
return result
}

#if !os(WASI)
internal func parseFrom(_ stream : InputStream) -> Bool {
var result = true

Expand Down Expand Up @@ -598,37 +591,17 @@ open class XMLParser : NSObject {

return result
}
#else
internal func parse(from data: Data) -> Bool {
var result = true
var chunkStart = 0
var chunkEnd = min(_chunkSize, data.count)
while result && chunkStart < chunkEnd {
let chunk = data[chunkStart..<chunkEnd]
result = parseData(chunk)
chunkStart = chunkEnd
chunkEnd = min(chunkEnd + _chunkSize, data.count)
}
return result
}
#endif

// called to start the event-driven parse. Returns YES in the event of a successful parse, and NO in case of error.
open func parse() -> Bool {
#if os(WASI)
return _data.map { parse(from: $0) } ?? false
#else
XMLParser.setCurrentParser(self)
defer { XMLParser.setCurrentParser(nil) }

if _stream != nil {
return parseFrom(_stream!)
} else if _data != nil {
return parseData(_data!, lastChunkOfData: true)
return Self.withCurrentParser(self) {
if _stream != nil {
return parseFrom(_stream!)
} else if _data != nil {
return parseData(_data!, lastChunkOfData: true)
}
return false
}

return false
#endif
}

// called by the delegate to stop the parse. The delegate will get an error message sent to it.
Expand Down
43 changes: 42 additions & 1 deletion Tests/Foundation/TestXMLParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,46 @@ class TestXMLParser : XCTestCase {
ElementNameChecker("noPrefix").check()
ElementNameChecker("myPrefix:myLocalName").check()
}


func testExternalEntity() throws {
class Delegate: XMLParserDelegateEventStream {
override func parserDidStartDocument(_ parser: XMLParser) {
// Start a child parser, updating `currentParser` to the child parser
// to ensure that `currentParser` won't be reset to `nil`, which would
// ignore any external entity related configuration.
let childParser = XMLParser(data: "<child />".data(using: .utf8)!)
XCTAssertTrue(childParser.parse())
super.parserDidStartDocument(parser)
}
}
try withTemporaryDirectory { dir, _ in
let greetingPath = dir.appendingPathComponent("greeting.xml")
try Data("<hello />".utf8).write(to: greetingPath)
let xml = """
<?xml version="1.0" standalone="no"?>
<!DOCTYPE doc [
<!ENTITY greeting SYSTEM "\(greetingPath.absoluteString)">
]>
<doc>&greeting;</doc>
"""

let parser = XMLParser(data: xml.data(using: .utf8)!)
// Explicitly disable external entity resolving
parser.externalEntityResolvingPolicy = .never
let delegate = Delegate()
parser.delegate = delegate
// The parse result changes depending on the libxml2 version
// because of the following libxml2 commit (shipped in libxml2 2.9.10):
// https://gitlab.gnome.org/GNOME/libxml2/-/commit/eddfbc38fa7e84ccd480eab3738e40d1b2c83979
// So we don't check the parse result here.
_ = parser.parse()
XCTAssertEqual(delegate.events, [
.startDocument,
.didStartElement("doc", nil, nil, [:]),
// Should not have parsed the external entity
.didEndElement("doc", nil, nil),
.endDocument,
])
}
}
}