Skip to content

Commit

Permalink
feat: remove xss (#252)
Browse files Browse the repository at this point in the history
closes #251
  • Loading branch information
Kikobeats authored Dec 30, 2019
1 parent ddd373b commit 87670c1
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 118 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,6 @@ Type: `String`

The HTML markup for extracting the content.

##### escape

Type: `Boolean`<br>
Default: `true`

It sanetizes the ouptut to prevent xss attacks.

#### rules

Type: `Array`
Expand Down
4 changes: 2 additions & 2 deletions packages/metascraper-iframe/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('metascraper-iframe', () => {
commonProviders.forEach(url => {
it(url, async () => {
const metascraper = createMetascraper([createMetascraperIframe()])
const meta = await metascraper({ url, escape: false })
const meta = await metascraper({ url })
should(meta.iframe).be.not.null()
})
})
Expand All @@ -54,7 +54,7 @@ describe('metascraper-iframe', () => {
const url = 'https://view.genial.ly/5dc53cfa759d2a0f4c7db5f4'
const rules = [createMetascraperIframe()]
const metascraper = createMetascraper(rules)
const meta = await metascraper({ url, html, escape: false })
const meta = await metascraper({ url, html })
should(meta.iframe).be.not.null()
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exports['song 1'] = {
"author": "Beauty Brain",
"description": "Thanks for 5.000 likes on https://www.facebook.com/BeautyBrainMusic :D &lt;3 &lt;3 &lt;3",
"description": "Thanks for 5.000 likes on https://www.facebook.com/BeautyBrainMusic :D <3 <3 <3",
"date": "2014-01-27T16:19:55.000Z",
"image": "https://i1.sndcdn.com/artworks-000069142357-nwttc6-t500x500.jpg",
"lang": "en",
Expand Down
8 changes: 0 additions & 8 deletions packages/metascraper/__snapshots__/xss.js.snap-shot

This file was deleted.

4 changes: 1 addition & 3 deletions packages/metascraper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@
"cheerio": "~1.0.0-rc.3",
"cheerio-advanced-selectors": "~2.0.1",
"lodash": "~4.17.15",
"map-values-deep": "~1.0.2",
"whoops": "~4.1.0",
"xss": "~1.0.6"
"whoops": "~4.1.0"
},
"devDependencies": {
"clear-module": "latest",
Expand Down
24 changes: 10 additions & 14 deletions packages/metascraper/src/get-data.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

const { isString, map, fromPairs } = require('lodash')
const mapValuesDeep = require('map-values-deep')
const { map, fromPairs } = require('lodash')
const { has } = require('@metascraper/helpers')
const xss = require('xss')

const truthyTest = () => true

Expand All @@ -24,20 +22,18 @@ const getValue = async ({ htmlDom, url, rules, meta, ...props }) => {
return value
}

const escapeValue = (value, { escape }) => {
if (!has(value)) return null
if (!escape) return value
return mapValuesDeep(value, value => (isString(value) ? xss(value) : value))
}
const normalizeValue = value => (has(value) ? value : null)

const getData = async ({ rules, htmlDom, url, escape, ...props }) => {
const getData = async ({ rules, htmlDom, url, ...props }) => {
const data = await Promise.all(
map(rules, async ([propName, innerRules]) => {
const value = escapeValue(
await getValue({ htmlDom, url, rules: innerRules, ...props }),
{ escape }
)
return [propName, value]
const value = await getValue({
htmlDom,
url,
rules: innerRules,
...props
})
return [propName, normalizeValue(value)]
})
)

Expand Down
9 changes: 1 addition & 8 deletions packages/metascraper/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ const MetascraperError = whoops('MetascraperError')

module.exports = rules => {
const loadedRules = loadRules(rules)
return async ({
url,
html,
rules: inlineRules,
escape = true,
...props
} = {}) => {
return async ({ url, html, rules: inlineRules, ...props } = {}) => {
if (!isUrl(url)) {
throw new MetascraperError({
message: 'Need to provide a valid URL.',
Expand All @@ -27,7 +21,6 @@ module.exports = rules => {

return getData({
url,
escape,
htmlDom: loadHTML(html),
rules: mergeRules(inlineRules, loadedRules),
...props
Expand Down
30 changes: 0 additions & 30 deletions packages/metascraper/test/unit/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,6 @@ it('url is required', async () => {
}
})

it('escape is enabled by default', async () => {
const html = `
<!doctype html>
<html xmlns:og="http://ogp.me/ns#" lang="en">
<head>
<meta charset="utf8">
<title>metascraper</title>
<meta property="og:description" content="The HR startups go to war.">
<meta property="og:image" content="image">
<meta property="og:title" content="<script src='http://127.0.0.1:8080/malware.js'></script>">
<meta property="og:type" content="article">
<meta property="og:url" content="http://127.0.0.1:8080">
</head>
<body>
</body>
</html>
`
const metascraper = createMetascraper([titleRules])
const metadata = await metascraper({
html,
url: 'http://127.0.0.1:8080'
})

should(metadata.title).be.equal(
'&lt;script src=‘http://127.0.0.1:8080/malware.js’&gt;&lt;/script&gt;'
)
})

it('load extra rules', async () => {
const url = 'https://microlink.io'

Expand Down
45 changes: 0 additions & 45 deletions packages/metascraper/test/unit/xss.js

This file was deleted.

0 comments on commit 87670c1

Please sign in to comment.