|
72 | 72 | (skippedEntity [name])
|
73 | 73 | ))))
|
74 | 74 |
|
75 |
| -(defn startparse-sax [s ch] |
76 |
| - (.. SAXParserFactory (newInstance) (newSAXParser) (parse s ch))) |
| 75 | +(defn sax-parser |
| 76 | + "Create a new SAXParser" |
| 77 | + {:added "1.11"} |
| 78 | + ^SAXParser [] |
| 79 | + (.newSAXParser (SAXParserFactory/newInstance))) |
| 80 | + |
| 81 | +(defn disable-external-entities |
| 82 | + "Modifies a SAXParser to disable external entity resolution to prevent XXE attacks" |
| 83 | + {:added "1.11"} |
| 84 | + ^SAXParser [^SAXParser parser] |
| 85 | + (let [reader (.getXMLReader parser)] |
| 86 | + ;; as per https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html |
| 87 | + (.setFeature reader "http://apache.org/xml/features/nonvalidating/load-external-dtd" false) |
| 88 | + (.setFeature reader "http://xml.org/sax/features/external-general-entities", false) |
| 89 | + (.setFeature reader "http://xml.org/sax/features/external-parameter-entities" false) |
| 90 | + parser)) |
| 91 | + |
| 92 | +(defn startparse-sax |
| 93 | + "A startparse function suitable for use with clojure.xml/parse. |
| 94 | + Note that this function is open to XXE entity attacks, see startparse-sax-safe." |
| 95 | + {:added "1.0"} |
| 96 | + [s ch] |
| 97 | + (.parse (sax-parser) s ch)) |
| 98 | + |
| 99 | +(defn startparse-sax-safe |
| 100 | + "A startparse function suitable for use with clojure.xml/parse. |
| 101 | + External entity resolution is disabled to prevent XXE entity attacks." |
| 102 | + {:added "1.11"} |
| 103 | + [s ch] |
| 104 | + (.parse (disable-external-entities (sax-parser)) s ch)) |
77 | 105 |
|
78 | 106 | (defn parse
|
79 | 107 | "Parses and loads the source s, which can be a File, InputStream or
|
80 | 108 | String naming a URI. Returns a tree of the xml/element struct-map,
|
81 | 109 | which has the keys :tag, :attrs, and :content. and accessor fns tag,
|
82 | 110 | attrs, and content. Other parsers can be supplied by passing
|
83 | 111 | startparse, a fn taking a source and a ContentHandler and returning
|
84 |
| - a parser" |
| 112 | + a parser. |
| 113 | +
|
| 114 | + Prior to 1.11, used startparse-sax by default. As of 1.11, uses |
| 115 | + startparse-sax-safe, which disables XXE (XML External Entity) |
| 116 | + processing. Pass startparse-sax to revert to prior behavior." |
85 | 117 | {:added "1.0"}
|
86 |
| - ([s] (parse s startparse-sax)) |
| 118 | + ([s] (parse s startparse-sax-safe)) |
87 | 119 | ([s startparse]
|
88 | 120 | (binding [*stack* nil
|
89 | 121 | *current* (struct element)
|
|
0 commit comments