Skip to content

Commit

Permalink
Do not continue crawling on the other origin when the origin-crossing…
Browse files Browse the repository at this point in the history
… relation results in an HTTP redirect

#196 (comment)
  • Loading branch information
papandreou committed Nov 21, 2021
1 parent a6ef642 commit 240f584
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,11 @@ async function hyperlink(
} else if (
['HtmlAnchor', 'SvgAnchor', 'HtmlIFrame'].includes(relation.type)
) {
if (!relation.crossorigin && recursive) {
if (
!relation.crossorigin &&
!relation.from.crossedOrigins &&
recursive
) {
follow = true;
} else if (relation.from !== relation.to) {
// If we are handling local file-urls, follow but mark as end-of-line in processing
Expand Down Expand Up @@ -689,7 +693,8 @@ async function hyperlink(

if (follow) {
// Save information about cross origin navigations for later
relation.to.crossedOrigins = relation.crossorigin;
relation.to.crossedOrigins =
relation.crossorigin || relation.from.crossedOrigins;

if (assetTypesWithoutRelations.includes(relation.to.type)) {
// If we are handling local file-urls, follow but mark as end-of-line in processing
Expand Down
53 changes: 53 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,59 @@ describe('hyperlink', function () {
});
});
});

// Regression test for https://github.com/Munter/hyperlink/issues/196#issuecomment-974726167
it('should not follow relations after a redirect upon already crossing origins', async function () {
httpception([
{
request: 'GET https://docs.ovh.com/gb/en/dedicated/firewall-network',
response: {
statusCode: 301,
headers: {
'Content-Type': 'text/html',
Location:
'https://docs.ovh.com/gb/en/dedicated/firewall-network/',
},
body: '<html><head><title>301 Moved Permanently</title></head></html>',
},
},
{
request: 'GET https://docs.ovh.com/gb/en/dedicated/firewall-network/',
response: {
statusCode: 200,
headers: { 'Content-Type': 'text/html' },
body: `<html><head></head><body><a id="objective" href="/otherpage.html"></body></html>`,
},
},
]);

const t = new TapRender();
sinon.spy(t, 'push');
await hyperlink(
{
recursive: true,
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'externalLinkRedirect'
),
inputUrls: ['index.html'],
},
t
);
expect(t.close(), 'to satisfy', { fail: 1 });
expect(t.push, 'to have a call satisfying', () => {
t.push(null, {
operator: 'fragment-check',
name: 'fragment-check testdata/externalLinkRedirect/index.html --> https://docs.ovh.com/gb/en/dedicated/firewall-network#objective',
expected: 'id="objective"',
at: 'testdata/externalLinkRedirect/index.html:1:10 <a href="https://docs.ovh.com/gb/en/dedicated/firewall-network#objective">...</a>',
ok: true,
actual: 'id="objective"',
});
});
});
});

describe('with a relation that points at an asset that returns 404', function () {
Expand Down
1 change: 1 addition & 0 deletions testdata/externalLinkRedirect/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="https://docs.ovh.com/gb/en/dedicated/firewall-network#objective">hey</a>

0 comments on commit 240f584

Please sign in to comment.