PROD-88182: fix add-visitor-id middleware to use ctx.origin correctly after koa v3 upgrade - #529
Merged
Conversation
… after koa v3 upgrade
Michalis-Apostolou
requested review from
echetzakis,
klesgidis,
kzacharakis,
nikosd23 and
stavros-wb
as code owners
September 30, 2026 11:56
klesgidis
approved these changes
Oct 1, 2026
kkyriakidis
approved these changes
Oct 1, 2026
|
|
||
| describe('when callback for cookie domain is provided from config', function() { | ||
| let origConfig; | ||
| before(function() { |
Contributor
There was a problem hiding this comment.
Suggested change
| before(function() { | |
| before(function() { | |
| server.config.visitor.getCookieDomain = () => 'domain-provider-cb'; | |
| }); | |
| after(function() { | |
| delete server.config.visitor.getCookieDomain; | |
| }); |
to avoid leaking getCookieDomain
very minor as it is the last test in the file
| should(response.headers['set-cookie']).be.undefined(); | ||
| }); | ||
|
|
||
| describe('when origin header is invalid', function() { |
Contributor
There was a problem hiding this comment.
an invalid Origin header doesn't affect the request.URL which tehn parses from Host: localhost:3220
maybe we rename this test to
Suggested change
| describe('when origin header is invalid', function() { | |
| describe('when the Origin header is malformed', function() { |
we can also add a test for a valid but foreign origin
Suggested change
| describe('when origin header is invalid', function() { | |
| describe('when the Origin header is a different origin', function() { | |
| it('derives the cookie domain from the host, not from Origin', async () => { | |
| const response = await supertest('localhost:3220') | |
| .get('/test') | |
| .set('origin', 'https://evil.example.com') | |
| .expect(200); | |
| const visitorCookie = response.headers['set-cookie'].find(c => c.startsWith('wmc=')); | |
| should(visitorCookie).not.be.undefined(); | |
| visitorCookie.should.match(/domain=localhost/i); | |
| visitorCookie.should.not.match(/evil\.example\.com/i); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After upgrading to koa 3 there was a breaking change on ctx.origin.
https://github.com/koajs/koa/releases/tag/v3.0.0
on add-visitor-id middleware we try to set the a cookie.
Instead of using ctx.origin to calculate the domain of the cookie , we use
ctx.request.URL.originas per discussion koajs/koa#1008 (comment)Also changed the order on how the domain resolved. Previously we calculated the default way and then overwrite it with the callback provided.
Issue
If a request had an invalid origin header , it couldn't be parsed by
new URL(ctx.origin)and it threw , resulting to 500 response.