Skip to content

PROD-88182: fix add-visitor-id middleware to use ctx.origin correctly after koa v3 upgrade - #529

Merged
Michalis-Apostolou merged 2 commits into
masterfrom
PROD-88182_fix_unparsable_koa_origin
Oct 1, 2026
Merged

Michalis-Apostolou merged 2 commits into
masterfrom
PROD-88182_fix_unparsable_koa_origin

Conversation

@Michalis-Apostolou

Copy link
Copy Markdown
Contributor

After upgrading to koa 3 there was a breaking change on ctx.origin.
https://github.com/koajs/koa/releases/tag/v3.0.0

req.origin should display the origin header if it exists, not the current hostname koajs/koa#1008. origin now aligns with the Origin header as used in CORS.

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.origin as 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.


describe('when callback for cookie domain is provided from config', function() {
let origConfig;
before(function() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

should(response.headers['set-cookie']).be.undefined();
});

describe('when origin header is invalid', function() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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);
});
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@Michalis-Apostolou
Michalis-Apostolou merged commit 7ccdaa2 into master Oct 1, 2026
14 checks passed
@Michalis-Apostolou
Michalis-Apostolou deleted the PROD-88182_fix_unparsable_koa_origin branch October 1, 2026 09:38
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.

3 participants