Skip to content

Commit

Permalink
refactor: apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Oct 26, 2022
1 parent 5954f4d commit 0b4c8e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
14 changes: 13 additions & 1 deletion lib/fetch/constants.js
Expand Up @@ -8,6 +8,17 @@ const nullBodyStatus = [101, 204, 205, 304]

const redirectStatus = [301, 302, 303, 307, 308]

// https://fetch.spec.whatwg.org/#block-bad-port
const badPorts = [
'1', '7', '9', '11', '13', '15', '17', '19', '20', '21', '22', '23', '25', '37', '42', '43', '53', '69', '77', '79',
'87', '95', '101', '102', '103', '104', '109', '110', '111', '113', '115', '117', '119', '123', '135', '137',
'139', '143', '161', '179', '389', '427', '465', '512', '513', '514', '515', '526', '530', '531', '532',
'540', '548', '554', '556', '563', '587', '601', '636', '989', '990', '993', '995', '1719', '1720', '1723',
'2049', '3659', '4045', '5060', '5061', '6000', '6566', '6665', '6666', '6667', '6668', '6669', '6697',
'10080'
]

// https://w3c.github.io/webappsec-referrer-policy/#referrer-policies
const referrerPolicy = [
'',
'no-referrer',
Expand Down Expand Up @@ -108,5 +119,6 @@ module.exports = {
redirectStatus,
corsSafeListedMethods,
nullBodyStatus,
safeMethods
safeMethods,
badPorts
}
32 changes: 5 additions & 27 deletions lib/fetch/util.js
@@ -1,6 +1,6 @@
'use strict'

const { redirectStatus } = require('./constants')
const { redirectStatus, badPorts, referrerPolicy: referrerPolicyTokens } = require('./constants')
const { performance } = require('perf_hooks')
const { isBlobLike, toUSVString, ReadableStreamFrom } = require('../core/util')
const assert = require('assert')
Expand All @@ -16,28 +16,6 @@ try {

}

// https://fetch.spec.whatwg.org/#block-bad-port
const badPorts = [
'1', '7', '9', '11', '13', '15', '17', '19', '20', '21', '22', '23', '25', '37', '42', '43', '53', '69', '77', '79',
'87', '95', '101', '102', '103', '104', '109', '110', '111', '113', '115', '117', '119', '123', '135', '137',
'139', '143', '161', '179', '389', '427', '465', '512', '513', '514', '515', '526', '530', '531', '532',
'540', '548', '554', '556', '563', '587', '601', '636', '989', '990', '993', '995', '1719', '1720', '1723',
'2049', '3659', '4045', '5060', '5061', '6000', '6566', '6665', '6666', '6667', '6668', '6669', '6697',
'10080'
]

// https://w3c.github.io/webappsec-referrer-policy/#referrer-policies
const referrerPolicyTokens = [
'no-referrer',
'no-referrer-when-downgrade',
'same-origin',
'origin',
'strict-origin',
'origin-when-cross-origin',
'strict-origin-when-cross-origin',
'unsafe-url'
]

function responseURL (response) {
// https://fetch.spec.whatwg.org/#responses
// A response has an associated URL. It is a pointer to the last URL
Expand Down Expand Up @@ -219,14 +197,14 @@ function setRequestReferrerPolicyOnRedirect (request, actualResponse) {
// 2. Let policy be the empty string.
// 3. For each token in policy-tokens, if token is a referrer policy and token is not the empty string, then set policy to token.
// 4. Return policy.
const token = headersList.get('referrer-policy') ?? ''
const policy = headersList.get('referrer-policy') ?? ''

// 2. If policy is not the empty string, then set request’s referrer policy to policy.
if (token !== '') {
if (policy !== '') {
for (const policyToken of referrerPolicyTokens) {
// if token is a referrer policy and token is not an empty string, then set policy to token.
if (token === policyToken) {
request.referrerPolicy = token
if (policy === policyToken) {
request.referrerPolicy = policy
break
}
}
Expand Down

0 comments on commit 0b4c8e1

Please sign in to comment.