Skip to content

Commit

Permalink
fix(audio): don't consider iframe referencing itself (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 18, 2023
1 parent 4409000 commit 134ec60
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
41 changes: 12 additions & 29 deletions packages/metascraper-audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,18 @@ module.exports = ({ getIframe = _getIframe } = {}) => {
return {
audio: audioRules.concat(
async ({ htmlDom: $, url }) => {
const iframe = $('iframe')
if (iframe.length === 0) return

const srcs = []

iframe.each(function () {
const src = $(this).attr('src')
const normalizedUrl = normalizeUrl(url, src)
if (
typeof normalizedUrl === 'string' &&
normalizedUrl.startsWith('http') &&
srcs.indexOf(normalizedUrl) === -1
) {
srcs.push(normalizedUrl)
}
})

return srcs.length > 0
? pReflect(
Promise.any(
srcs.map(async src => {
const htmlDom = await getIframe(url, $, { src })
const result = await findRule(audioRules, { htmlDom, url })
if (!has(result)) throw TypeError('no result')
return result
})
)
).then(({ value }) => value)
: undefined
const srcs = [...new $('iframe').map((_, element) => $(element).attr('src')).get().map(src => normalizeUrl(url, src))]
if (srcs.length === 0) return
return pReflect(
Promise.any(
srcs.map(async src => {
const htmlDom = await getIframe(url, $, { src })
const result = await findRule(audioRules, { htmlDom, url })
if (!has(result)) throw TypeError('no result')
return result
})
)
).then(({ value }) => value)
},
async ({ htmlDom: $, url }) => {
const src = $twitter($, 'twitter:player')
Expand Down
6 changes: 6 additions & 0 deletions packages/metascraper-helpers/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ test('.normalizeUrl', t => {
)
t.is(normalizeUrl('https://example.com/'), 'https://example.com/')
t.is(normalizeUrl('https://example.com'), 'https://example.com/')
t.is(normalizeUrl('https://www.example.com', 'https://www.example.com/foo'), 'https://www.example.com/foo')
t.is(normalizeUrl('https://www.example.com', '/foo'), 'https://www.example.com/foo')
t.is(normalizeUrl('https://www.example.com', 'file.html'), 'https://www.example.com/file.html')
t.is(normalizeUrl('https://www.example.com', 'data:text/html;base64,PGh0bWw+SGVsbG8sIHdvcmxkITwvaHRtbD4='), 'data:text/html;base64,PGh0bWw+SGVsbG8sIHdvcmxkITwvaHRtbD4=')
t.is(normalizeUrl('https://www.example.com', 'javascript:alert(\'Hello, world!\');'), undefined)
t.is(normalizeUrl('https://www.example.com', 'javascript:void(0)'), undefined)
})

test('.author', t => {
Expand Down

0 comments on commit 134ec60

Please sign in to comment.