Skip to content

Commit

Permalink
Set 'referrer' and 'referrerPolicy' when fetching inline images, re: #…
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo committed May 27, 2024
1 parent 387a94f commit cf4d959
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import humanDate from './src/util/human-date.js';
import outputPath from './src/util/output-path.js';
import getCssPageFormat from './src/util/get-css-page-format.js';
import { resolveSequence, resolveParallel } from './src/util/promises.js';
import { getUrlOrigin } from './src/util/url-origin.js';
import addExif from './src/exif.js';
import { hyphenateDom } from './src/hyphenate.js';
import { textToIso6391, getLanguageAttribute } from './src/util/language.js';
Expand Down Expand Up @@ -296,13 +297,25 @@ async function cleanup(url, options) {

err.write(' ✓\n');

console.log(getUrlOrigin(final_url));

if (options.inline) {
await inlineImages(
parsed.content,
{
headers: {
'user-agent': UA
},
/*
Send the referrer as the browser would
when fetching the image to render it.
The referrer policy would take care of
stripping the URL down to its origin,
but just in case, let’s strip it ourselves.
*/
referrer: final_url,
referrerPolicy: 'strict-origin-when-cross-origin',
timeout: 10 * 1000
},
options.debug ? out : undefined
Expand Down
9 changes: 9 additions & 0 deletions src/util/url-origin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function getUrlOrigin(str) {
let origin;
try {
origin = new URL(str).origin;
} catch (err) {
// ignore
}
return origin && origin !== 'null' ? origin : undefined;
}
9 changes: 9 additions & 0 deletions test/url-origin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import tape from 'tape';
import { getUrlOrigin } from '../src/util/url-origin.js';

tape('getUrlOrigin', t => {
t.equal(getUrlOrigin('invalid'), undefined);
t.equal(getUrlOrigin('file:///Users/myuser/'), undefined);
t.equal(getUrlOrigin('https://github.com/user/repo'), 'https://github.com');
t.end();
});

0 comments on commit cf4d959

Please sign in to comment.