Skip to content

Commit

Permalink
Do not follow cross-origin non-anchor relations in internalOnly mode
Browse files Browse the repository at this point in the history
Fixes #198, #181
  • Loading branch information
papandreou committed Jan 9, 2022
1 parent 63cdd47 commit bfaa8dc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ async function hyperlink(
}
} else if (relation.type === 'JavaScriptFetch') {
follow = false;
} else if (internalOnly) {
follow = !relation.crossorigin;
} else {
follow = true;
}
Expand Down
48 changes: 48 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,54 @@ describe('hyperlink', function () {
expect(t.close(), 'to satisfy', { fail: 0 });
});

// Regression test for https://github.com/Munter/hyperlink/issues/198
it('should not follow image/non-anchor relations to external assets', async function () {
httpception();
const t = new TapRender();
sinon.spy(t, 'push');
await hyperlink(
{
recursive: false,
internalOnly: true,
root: pathModule.resolve(__dirname, '..', 'testdata', 'issue198'),
inputUrls: ['index.html'],
},
t
);

expect(t.close(), 'to satisfy', {
count: 1,
pass: 1,
fail: 0,
skip: 0,
todo: 0,
});
});

// Regression test for https://github.com/Munter/hyperlink/issues/181
it('should not an <embed src=...> relation pointing at an external asset', async function () {
httpception();
const t = new TapRender();
sinon.spy(t, 'push');
await hyperlink(
{
recursive: false,
internalOnly: true,
root: pathModule.resolve(__dirname, '..', 'testdata', 'issue181'),
inputUrls: ['index.html'],
},
t
);

expect(t.close(), 'to satisfy', {
count: 1,
pass: 1,
fail: 0,
skip: 0,
todo: 0,
});
});

it('should follow fragment links within the same page', async () => {
const t = new TapRender();
// t.pipe(process.stdout);
Expand Down
1 change: 1 addition & 0 deletions testdata/issue181/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<embed src='http://www.thedailyshow.com/sitewide/video_player/view/default/swf.jhtml'></embed>
1 change: 1 addition & 0 deletions testdata/issue198/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img src="https://github.com/danielfdickinson/image-handling-mod-hugo-dfd/actions/workflows/build-and-verify.yml/badge.svg" alt="build-and-verify">

0 comments on commit bfaa8dc

Please sign in to comment.