index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. 'use strict'
  2. var xtend = require('xtend')
  3. var defaults = require('./github.json')
  4. module.exports = wrapper
  5. var own = {}.hasOwnProperty
  6. var allData = 'data*'
  7. var nodeSchema = {
  8. root: {children: all},
  9. doctype: handleDoctype,
  10. comment: handleComment,
  11. element: {
  12. tagName: handleTagName,
  13. properties: handleProperties,
  14. children: all
  15. },
  16. text: {value: handleValue},
  17. '*': {
  18. data: allow,
  19. position: allow
  20. }
  21. }
  22. // Sanitize `node`, according to `schema`.
  23. function wrapper(node, schema) {
  24. var ctx = {type: 'root', children: []}
  25. var replace
  26. if (!node || typeof node !== 'object' || !node.type) {
  27. return ctx
  28. }
  29. replace = one(xtend(defaults, schema || {}), node, [])
  30. if (!replace) {
  31. return ctx
  32. }
  33. if ('length' in replace) {
  34. if (replace.length === 1) {
  35. return replace[0]
  36. }
  37. ctx.children = replace
  38. return ctx
  39. }
  40. return replace
  41. }
  42. // Sanitize `node`.
  43. function one(schema, node, stack) {
  44. var type = node && node.type
  45. var replacement = {type: node.type}
  46. var replace = true
  47. var definition
  48. var allowed
  49. var result
  50. var key
  51. if (!own.call(nodeSchema, type)) {
  52. replace = false
  53. } else {
  54. definition = nodeSchema[type]
  55. if (typeof definition === 'function') {
  56. definition = definition(schema, node)
  57. }
  58. if (!definition) {
  59. replace = false
  60. } else {
  61. allowed = xtend(definition, nodeSchema['*'])
  62. for (key in allowed) {
  63. result = allowed[key](schema, node[key], node, stack)
  64. if (result === false) {
  65. replace = false
  66. // Set the non-safe value.
  67. replacement[key] = node[key]
  68. } else if (result !== null && result !== undefined) {
  69. replacement[key] = result
  70. }
  71. }
  72. }
  73. }
  74. if (!replace) {
  75. if (
  76. !replacement.children ||
  77. replacement.children.length === 0 ||
  78. schema.strip.indexOf(replacement.tagName) !== -1
  79. ) {
  80. return null
  81. }
  82. return replacement.children
  83. }
  84. return replacement
  85. }
  86. // Sanitize `children`.
  87. function all(schema, children, node, stack) {
  88. var nodes = children || []
  89. var length = nodes.length || 0
  90. var results = []
  91. var index = -1
  92. var result
  93. stack = stack.concat(node.tagName)
  94. while (++index < length) {
  95. result = one(schema, nodes[index], stack)
  96. if (result) {
  97. if ('length' in result) {
  98. results = results.concat(result)
  99. } else {
  100. results.push(result)
  101. }
  102. }
  103. }
  104. return results
  105. }
  106. // Sanitize `properties`.
  107. function handleProperties(schema, properties, node, stack) {
  108. var name = handleTagName(schema, node.tagName, node, stack)
  109. var attrs = schema.attributes
  110. var reqs = schema.required || /* istanbul ignore next */ {}
  111. var props = properties || {}
  112. var result = {}
  113. var allowed
  114. var required
  115. var definition
  116. var prop
  117. var value
  118. allowed = xtend(
  119. toPropertyValueMap(attrs['*']),
  120. toPropertyValueMap(own.call(attrs, name) ? attrs[name] : [])
  121. )
  122. for (prop in props) {
  123. value = props[prop]
  124. if (own.call(allowed, prop)) {
  125. definition = allowed[prop]
  126. } else if (data(prop) && own.call(allowed, allData)) {
  127. definition = allowed[allData]
  128. } else {
  129. continue
  130. }
  131. if (value && typeof value === 'object' && 'length' in value) {
  132. value = handlePropertyValues(schema, value, prop, definition)
  133. } else {
  134. value = handlePropertyValue(schema, value, prop, definition)
  135. }
  136. if (value !== null && value !== undefined) {
  137. result[prop] = value
  138. }
  139. }
  140. required = own.call(reqs, name) ? reqs[name] : {}
  141. for (prop in required) {
  142. if (!own.call(result, prop)) {
  143. result[prop] = required[prop]
  144. }
  145. }
  146. return result
  147. }
  148. // Sanitize a property value which is a list.
  149. function handlePropertyValues(schema, values, prop, definition) {
  150. var length = values.length
  151. var result = []
  152. var index = -1
  153. var value
  154. while (++index < length) {
  155. value = handlePropertyValue(schema, values[index], prop, definition)
  156. if (value !== null && value !== undefined) {
  157. result.push(value)
  158. }
  159. }
  160. return result
  161. }
  162. // Sanitize a property value.
  163. function handlePropertyValue(schema, value, prop, definition) {
  164. if (
  165. typeof value !== 'boolean' &&
  166. typeof value !== 'number' &&
  167. typeof value !== 'string'
  168. ) {
  169. return null
  170. }
  171. if (!handleProtocol(schema, value, prop)) {
  172. return null
  173. }
  174. if (definition.length !== 0 && definition.indexOf(value) === -1) {
  175. return null
  176. }
  177. if (schema.clobber.indexOf(prop) !== -1) {
  178. value = schema.clobberPrefix + value
  179. }
  180. return value
  181. }
  182. // Check whether `value` is a safe URL.
  183. function handleProtocol(schema, value, prop) {
  184. var protocols = schema.protocols
  185. var protocol
  186. var first
  187. var colon
  188. var length
  189. var index
  190. protocols = own.call(protocols, prop) ? protocols[prop].concat() : []
  191. if (protocols.length === 0) {
  192. return true
  193. }
  194. value = String(value)
  195. first = value.charAt(0)
  196. if (first === '#' || first === '/') {
  197. return true
  198. }
  199. colon = value.indexOf(':')
  200. if (colon === -1) {
  201. return true
  202. }
  203. length = protocols.length
  204. index = -1
  205. while (++index < length) {
  206. protocol = protocols[index]
  207. if (
  208. colon === protocol.length &&
  209. value.slice(0, protocol.length) === protocol
  210. ) {
  211. return true
  212. }
  213. }
  214. index = value.indexOf('?')
  215. if (index !== -1 && colon > index) {
  216. return true
  217. }
  218. index = value.indexOf('#')
  219. if (index !== -1 && colon > index) {
  220. return true
  221. }
  222. return false
  223. }
  224. // Always return a valid HTML5 doctype.
  225. function handleDoctypeName() {
  226. return 'html'
  227. }
  228. // Sanitize `tagName`.
  229. function handleTagName(schema, tagName, node, stack) {
  230. var name = typeof tagName === 'string' ? tagName : null
  231. var ancestors = schema.ancestors
  232. var length
  233. var index
  234. if (!name || name === '*' || schema.tagNames.indexOf(name) === -1) {
  235. return false
  236. }
  237. ancestors = own.call(ancestors, name) ? ancestors[name] : []
  238. // Some nodes can break out of their context if they don’t have a certain
  239. // ancestor.
  240. if (ancestors.length !== 0) {
  241. length = ancestors.length + 1
  242. index = -1
  243. while (++index < length) {
  244. if (!ancestors[index]) {
  245. return false
  246. }
  247. if (stack.indexOf(ancestors[index]) !== -1) {
  248. break
  249. }
  250. }
  251. }
  252. return name
  253. }
  254. function handleDoctype(schema) {
  255. return schema.allowDoctypes ? {name: handleDoctypeName} : null
  256. }
  257. function handleComment(schema) {
  258. return schema.allowComments ? {value: handleValue} : null
  259. }
  260. // Sanitize `value`.
  261. function handleValue(schema, value) {
  262. return typeof value === 'string' ? value : ''
  263. }
  264. // Create a map from a list of props or a list of properties and values.
  265. function toPropertyValueMap(values) {
  266. var result = {}
  267. var length = values.length
  268. var index = -1
  269. var value
  270. while (++index < length) {
  271. value = values[index]
  272. if (value && typeof value === 'object' && 'length' in value) {
  273. result[value[0]] = value.slice(1)
  274. } else {
  275. result[value] = []
  276. }
  277. }
  278. return result
  279. }
  280. // Allow `value`.
  281. function allow(schema, value) {
  282. return value
  283. }
  284. // Check if `prop` is a data property.
  285. function data(prop) {
  286. return prop.length > 4 && prop.slice(0, 4).toLowerCase() === 'data'
  287. }