1
+ // --- stubs ---
2
+
3
+ class Data {
4
+ init < S> ( _ elements: S ) { }
5
+ }
6
+
7
+ struct URL {
8
+ init ? ( string: String ) { }
9
+ }
10
+
11
+ extension String {
12
+ struct Encoding : Hashable {
13
+ let rawValue : UInt
14
+ static let utf8 = String . Encoding ( rawValue: 1 )
15
+ }
16
+
17
+ init ( contentsOf: URL ) {
18
+ let data = " "
19
+ self . init ( data)
20
+ }
21
+ }
22
+
23
+ class AEXMLElement { }
24
+
25
+ struct AEXMLOptions {
26
+ var parserSettings = ParserSettings ( )
27
+
28
+ struct ParserSettings {
29
+ public var shouldResolveExternalEntities = false
30
+ }
31
+ }
32
+
33
+ class AEXMLDocument {
34
+ init ( root: AEXMLElement ? = nil , options: AEXMLOptions ) { }
35
+ init ( xml: Data , options: AEXMLOptions = AEXMLOptions ( ) ) { }
36
+ init ( xml: String , encoding: String . Encoding , options: AEXMLOptions ) { }
37
+ func loadXML( _: Data ) { }
38
+ }
39
+
40
+ class AEXMLParser {
41
+ init ( document: AEXMLDocument , data: Data ) { }
42
+ }
43
+
44
+ // --- tests ---
45
+
46
+ func testString( ) {
47
+ var options = AEXMLOptions ( )
48
+ options. parserSettings. shouldResolveExternalEntities = true
49
+
50
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
51
+ let _ = AEXMLDocument ( xml: remoteString, encoding: String . Encoding. utf8, options: options) // $ hasXXE=50
52
+ }
53
+
54
+ func testStringSafeImplicit( ) {
55
+ var options = AEXMLOptions ( )
56
+
57
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
58
+ let _ = AEXMLDocument ( xml: remoteString, encoding: String . Encoding. utf8, options: options) // NO XXE
59
+ }
60
+
61
+ func testStringSafeExplicit( ) {
62
+ var options = AEXMLOptions ( )
63
+ options. parserSettings. shouldResolveExternalEntities = false
64
+
65
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
66
+ let _ = AEXMLDocument ( xml: remoteString, encoding: String . Encoding. utf8, options: options) // NO XXE
67
+ }
68
+
69
+ func testData( ) {
70
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
71
+ let remoteData = Data ( remoteString)
72
+ var options = AEXMLOptions ( )
73
+ options. parserSettings. shouldResolveExternalEntities = true
74
+ let _ = AEXMLDocument ( xml: remoteData, options: options) // $ hasXXE=70
75
+ }
76
+
77
+ func testDataSafeImplicit( ) {
78
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
79
+ let remoteData = Data ( remoteString)
80
+ var options = AEXMLOptions ( )
81
+ let _ = AEXMLDocument ( xml: remoteData, options: options) // NO XXE
82
+ }
83
+
84
+ func testDataSafeExplicit( ) {
85
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
86
+ let remoteData = Data ( remoteString)
87
+ var options = AEXMLOptions ( )
88
+ options. parserSettings. shouldResolveExternalEntities = false
89
+ let _ = AEXMLDocument ( xml: remoteData, options: options) // NO XXE
90
+ }
91
+
92
+ func testDataLoadXml( ) {
93
+ var options = AEXMLOptions ( )
94
+ options. parserSettings. shouldResolveExternalEntities = true
95
+ let doc = AEXMLDocument ( root: nil , options: options)
96
+
97
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
98
+ let remoteData = Data ( remoteString)
99
+ doc. loadXML ( remoteData) // $ hasXXE=97
100
+ }
101
+
102
+ func testDataLoadXmlSafeImplicit( ) {
103
+ var options = AEXMLOptions ( )
104
+ let doc = AEXMLDocument ( root: nil , options: options)
105
+
106
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
107
+ let remoteData = Data ( remoteString)
108
+ doc. loadXML ( remoteData) // NO XXE
109
+ }
110
+
111
+ func testDataLoadXmlSafeExplicit( ) {
112
+ var options = AEXMLOptions ( )
113
+ options. parserSettings. shouldResolveExternalEntities = false
114
+ let doc = AEXMLDocument ( root: nil , options: options)
115
+
116
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
117
+ let remoteData = Data ( remoteString)
118
+ doc. loadXML ( remoteData) // NO XXE
119
+ }
120
+
121
+ func testParser( ) {
122
+ var options = AEXMLOptions ( )
123
+ options. parserSettings. shouldResolveExternalEntities = true
124
+ let doc = AEXMLDocument ( root: nil , options: options)
125
+
126
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
127
+ let remoteData = Data ( remoteString)
128
+ let _ = AEXMLParser ( document: doc, data: remoteData) // $ hasXXE=126
129
+ }
130
+
131
+ func testParserSafeImplicit( ) {
132
+ var options = AEXMLOptions ( )
133
+ let doc = AEXMLDocument ( root: nil , options: options)
134
+
135
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
136
+ let remoteData = Data ( remoteString)
137
+ let _ = AEXMLParser ( document: doc, data: remoteData) // NO XXE
138
+ }
139
+
140
+ func testParserSafeExplicit( ) {
141
+ var options = AEXMLOptions ( )
142
+ options. parserSettings. shouldResolveExternalEntities = false
143
+ let doc = AEXMLDocument ( root: nil , options: options)
144
+
145
+ let remoteString = String ( contentsOf: URL ( string: " http://example.com/ " ) !)
146
+ let remoteData = Data ( remoteString)
147
+ let _ = AEXMLParser ( document: doc, data: remoteData) // NO XXE
148
+ }
0 commit comments