Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fetch#Request): Implements determineRequestReferrer #1236

Merged
merged 21 commits into from Sep 28, 2022

Conversation

metcoder95
Copy link
Member

@metcoder95 metcoder95 commented Feb 15, 2022

Relates to #958

Pendings:

  • Tests

Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please paste in the spec test similar to our other implementations.

@metcoder95
Copy link
Member Author

Sure thing, I actually have two doubts in here:

  1. How do we treat step 7 of the step, is not something super clear to me. It quotes:

The user agent MAY alter referrerURL or referrerOrigin at this point to enforce arbitrary policy considerations in the interests of minimizing data leakage. For example, the user agent could strip the URL down to an origin, modify its host, replace it with an empty string, etc.

AFAIU means the agent has full control about the referrerURL, and treats it accordingly to our policies. Then, this step should be totally ignored or just leave it with a note to indicate this part of the spec?

  1. In order to cover all the referrer policy cases is needed to assess if the referrerURL is potentially trustworthy and the same applies for the currentURL. Shall I make a step forward and implement the asses algorithm by the spec? I think is also needed in other parts of Undici when handling the URL

I'll cover your suggestions, add documentation (on code and for implementation) 👍

@codecov-commenter
Copy link

codecov-commenter commented Feb 19, 2022

Codecov Report

Base: 94.89% // Head: 94.72% // Decreases project coverage by -0.17% ⚠️

Coverage data is based on head (e5ff949) compared to base (e5e5b97).
Patch coverage: 77.08% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1236      +/-   ##
==========================================
- Coverage   94.89%   94.72%   -0.18%     
==========================================
  Files          53       53              
  Lines        4803     4850      +47     
==========================================
+ Hits         4558     4594      +36     
- Misses        245      256      +11     
Impacted Files Coverage Δ
lib/fetch/util.js 83.93% <77.08%> (-2.37%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@metcoder95
Copy link
Member Author

I'm not sure about those errors, any hint? 🤔

@metcoder95 metcoder95 marked this pull request as ready for review March 1, 2022 22:13
@mcollina
Copy link
Member

mcollina commented Mar 3, 2022

  78) node-fetch
       should not allow cloning a response after its been used:
     TypeError: Cannot read properties of null (reading 'globalObject')
      at determineRequestsReferrer (lib/fetch/util.js:79:117)
      at Fetch.mainFetch (lib/fetch/index.js:152:129)
      at Fetch.fetching (lib/fetch/index.js:135:42)
      at Agent.fetch (lib/fetch/index.js:34:84)
      at fetch (index.js:1:15391)
      at Context.<anonymous> (test/node-fetch/main.js:1391:12)
      at processImmediate (node:internal/timers:466:21)

This seems pretty clear.

@metcoder95 metcoder95 force-pushed the request/get_request_referrer branch from ab81505 to 3c00c7f Compare March 8, 2022 11:53
@metcoder95
Copy link
Member Author

Thanks for the hint @mcollina, but it seems it was a bad check on the Request environment, as I didn't check for possible undefined from the root object. Now shall be fixed, looking forward for feedback 🙂

const policy = request.referrerPolicy

// Return no-referrer when empty or policy says so
if (policy === '' || policy === 'no-referrer') return 'no-referrer'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (policy === '' || policy === 'no-referrer') return 'no-referrer'
if (policy === '' || policy === 'no-referrer') {
return 'no-referrer'
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not part of spec?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not explicitly, but if there's no value or the policy explicitly says that there's no-referrer, there's no point in continuing executing the spec algorithm. Any suggestion on how to handle it? 🙂

lib/fetch/util.js Outdated Show resolved Hide resolved
@metcoder95
Copy link
Member Author

Hi @ronag, changes applied and conflicts fixed, let me know if there's anything else to change 👍

@ronag
Copy link
Member

ronag commented Sep 21, 2022

@metcoder95 CI is still failing.

*/
if (request.referrer === 'client') {
// Not defined in Node but part of the spec
if (environment?.globalObject instanceof Window) { // eslint-disable-line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to something similar to

request.client?.globalObject?.constructor?.name === 'Window'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, applied in 6ed09c1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem to have been done

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, sorry for that. Change was made but didn't stage it for the commit 😓
Here it is the new one: 681a755

lib/fetch/util.js Outdated Show resolved Hide resolved
@metcoder95 metcoder95 requested review from KhafraDev and removed request for mcollina September 23, 2022 07:05
@metcoder95
Copy link
Member Author

It seems the testing was failing due to some bad import. Fixed 🙂

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to add tests that use fetch directly, instead of just testing the utility methods?

let originAsURL

// If not valid because not semantically correct, return false
try { originAsURL = new URL(origin) } catch (e) { return false }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be able to throw

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, a bad bad origin cannot happen without being priorly identified. Removed in 2ca0941

@metcoder95
Copy link
Member Author

is it possible to add tests that use fetch directly, instead of just testing the utility methods?

Sure, by just changing to a different value from no-referrer on the Request#referrer when using Fetch should do the work. I'll add some during the day 🙂

@ronag
Copy link
Member

ronag commented Sep 28, 2022

@KhafraDev are we good here?

@ronag ronag merged commit 1b85001 into nodejs:main Sep 28, 2022
metcoder95 added a commit to metcoder95/undici that referenced this pull request Dec 26, 2022
* feat: poc of determineRequestReferrer

* refactor: apply shortcut

* feat(partial): apply switch referrer statement

* refactor: add in-code documentation

* feat: add check for window

* docs: add comments

* feat: add check for trustworthy/non-trustworthy urls

* docs: add documentation about pottentially trustworthy

* feat: expose pottentially trustworthy

* test: URL potentially trustworthy

* fix: check for possibly undefined

* test: initial round

* feat: smaller improvements

* docs: update in-code docs

* lint: ignore line

* tests: add more test scenarios

* refactor: small improvements

* refactor: apply review

* tests: adjust testing

* refactor: apply PR review

* refactor: smaller adjustements
crysmags pushed a commit to crysmags/undici that referenced this pull request Feb 27, 2024
* feat: poc of determineRequestReferrer

* refactor: apply shortcut

* feat(partial): apply switch referrer statement

* refactor: add in-code documentation

* feat: add check for window

* docs: add comments

* feat: add check for trustworthy/non-trustworthy urls

* docs: add documentation about pottentially trustworthy

* feat: expose pottentially trustworthy

* test: URL potentially trustworthy

* fix: check for possibly undefined

* test: initial round

* feat: smaller improvements

* docs: update in-code docs

* lint: ignore line

* tests: add more test scenarios

* refactor: small improvements

* refactor: apply review

* tests: adjust testing

* refactor: apply PR review

* refactor: smaller adjustements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants