Repository navigation
PROD-88182: fix add-visitor-id middleware to use ctx.origin correctly after koa v3 upgrade #529
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
Merged
Michalis-Apostolou
merged 2 commits into
master
from
PROD-88182_fix_unparsable_koa_origin
Oct 1, 2026
+202
−6
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const { orka } = require('../../build'); | ||
|
|
||
| const w = orka({ | ||
| diamorphosis: { configFolder: './examples/default-middlewares-example' }, | ||
| routesPath: './examples/default-middlewares-example/routes.js', | ||
| beforeStart: () => { | ||
| const config = require('./config'); | ||
| console.log(`Going to start env: ${config.nodeEnv}`); | ||
| } | ||
| }); | ||
|
|
||
| if (!module.parent) { | ||
| w.start(); | ||
| } | ||
|
|
||
| module.exports = w; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| module.exports = { | ||
| nodeEnv: 'demo', | ||
| log: { | ||
| json: true | ||
| }, | ||
| app: { | ||
| name: 'foo' | ||
| }, | ||
| cors: { | ||
| publicPrefixes: ['/api/allowAll'], | ||
| allowedOrigins: ['localhost:3220', 'lvh.me', '*.lvh.me'], | ||
| }, | ||
| riviere: { | ||
| bodyKeysRegex: '.*' | ||
| }, | ||
| visitor: { | ||
| setCookie: true, | ||
| cookie: 'wmc', | ||
| maxAge: '7d', | ||
| variant: 'v1', | ||
| secure: true, | ||
| forwarded_headers: ['user-agent', 'x-request-id', 'referer', 'origin'], | ||
| waitForAttribution: false, | ||
| }, | ||
| port: 3220 | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module.exports = { | ||
| get: { | ||
| '/test': async (ctx, next) => (ctx.body = 'ok') | ||
| } | ||
| }; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import * as should from 'should'; | ||
| import * as supertest from 'supertest'; | ||
| import * as uuid from 'uuid'; | ||
|
|
||
| describe('Default middlewares examples', () => { | ||
| let server; | ||
| after(async () => { | ||
| if (server) await server.stop(); | ||
| }); | ||
|
|
||
| before(async () => { | ||
| const serverPath = '../../examples/default-middlewares-example/app'; | ||
| delete require.cache[require.resolve(serverPath)]; | ||
| server = require(serverPath); | ||
| await server.start(); | ||
| await new Promise(t => setTimeout(t, 1000)); | ||
| }); | ||
|
|
||
| describe('add-visitor-id middleware', function() { | ||
| it('/test sets a visitor cookie', async () => { | ||
| const response = await supertest('localhost:3220') | ||
| .get('/test') | ||
| .expect(200); | ||
|
|
||
| const cookies = response.headers['set-cookie']; | ||
| should(cookies).not.be.undefined(); | ||
|
|
||
| const visitorCookie = cookies.find(cookie => cookie.startsWith('wmc=')); | ||
| should(visitorCookie).not.be.undefined(); | ||
| visitorCookie.should.match(/domain=localhost/i); | ||
| visitorCookie.should.match(/secure/i); | ||
| visitorCookie.should.match(/samesite=none/i); | ||
|
|
||
| const [, encoded] = visitorCookie.split(';')[0].split('='); | ||
| const decoded = JSON.parse(decodeURIComponent(encoded)); | ||
| uuid.validate(decoded.cookie_id).should.be.true(); | ||
| }); | ||
|
|
||
| it('/test reuses the visitor cookie when already present', async () => { | ||
| const visitor = uuid.v4(); | ||
| const cookie = encodeURIComponent(JSON.stringify({ cookie_id: visitor })); | ||
|
|
||
| const response = await supertest('localhost:3220') | ||
| .get('/test') | ||
| .set('Cookie', `wmc=${cookie}`) | ||
| .expect(200); | ||
|
|
||
| should(response.headers['set-cookie']).be.undefined(); | ||
| }); | ||
|
|
||
| describe('when the Origin header is malformed', function() { | ||
| it('/test sets a visitor cookie', async () => { | ||
| const response = await supertest('localhost:3220') | ||
| .get('/test') | ||
| .set('origin', 'invalid') | ||
| .expect(200); | ||
|
|
||
| const cookies = response.headers['set-cookie']; | ||
| should(cookies).not.be.undefined(); | ||
|
|
||
| const visitorCookie = cookies.find(cookie => cookie.startsWith('wmc=')); | ||
| should(visitorCookie).not.be.undefined(); | ||
| visitorCookie.should.match(/domain=localhost/i); | ||
| visitorCookie.should.match(/secure/i); | ||
| visitorCookie.should.match(/samesite=none/i); | ||
|
|
||
| const [, encoded] = visitorCookie.split(';')[0].split('='); | ||
| const decoded = JSON.parse(decodeURIComponent(encoded)); | ||
| uuid.validate(decoded.cookie_id).should.be.true(); | ||
| }); | ||
|
|
||
| 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); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when callback for cookie domain is provided from config', function() { | ||
| before(function() { | ||
| server.config.visitor.getCookieDomain = () => 'domain-provider-cb'; | ||
| }); | ||
|
|
||
| after(function() { | ||
| delete server.config.visitor.getCookieDomain; | ||
| }); | ||
|
|
||
| it('uses the callback to set a visitor cookie domain', async () => { | ||
| const response = await supertest('localhost:3220') | ||
| .get('/test') | ||
| .set('origin', 'invalid') | ||
| .expect(200); | ||
|
|
||
| const cookies = response.headers['set-cookie']; | ||
| should(cookies).not.be.undefined(); | ||
|
|
||
| const visitorCookie = cookies.find(cookie => cookie.startsWith('wmc=')); | ||
| should(visitorCookie).not.be.undefined(); | ||
| visitorCookie.should.match(/domain=domain-provider-cb/i); | ||
| visitorCookie.should.match(/secure/i); | ||
| visitorCookie.should.match(/samesite=none/i); | ||
|
|
||
| const [, encoded] = visitorCookie.split(';')[0].split('='); | ||
| const decoded = JSON.parse(decodeURIComponent(encoded)); | ||
| uuid.validate(decoded.cookie_id).should.be.true(); | ||
| }); | ||
|
|
||
| it('precedes the origin header uses the callback to set a visitor cookie', async () => { | ||
| const response = await supertest('localhost:3220') | ||
| .get('/test') | ||
| .set('origin', 'http://localhost:3220') | ||
| .expect(200); | ||
|
|
||
| const cookies = response.headers['set-cookie']; | ||
| should(cookies).not.be.undefined(); | ||
|
|
||
| const visitorCookie = cookies.find(cookie => cookie.startsWith('wmc=')); | ||
| should(visitorCookie).not.be.undefined(); | ||
| visitorCookie.should.match(/domain=domain-provider-cb/i); | ||
| visitorCookie.should.match(/secure/i); | ||
| visitorCookie.should.match(/samesite=none/i); | ||
|
|
||
| const [, encoded] = visitorCookie.split(';')[0].split('='); | ||
| const decoded = JSON.parse(decodeURIComponent(encoded)); | ||
| uuid.validate(decoded.cookie_id).should.be.true(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to avoid leaking getCookieDomain
very minor as it is the last test in the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done