Skip to content

Commit 1a804ca

Browse files
Merge pull request #479 from vtex/fix/session-cookie/3.x
[3.x] Fix / Janus / Extract session cookie in a safe manner
2 parents 2cf626e + e4945cf commit 1a804ca

4 files changed

Lines changed: 13 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
## [3.77.3] - 2021-10-22
11+
### Fixed
12+
- Extract session cookie in a safe manner
13+
14+
## [3.77.2] - 2021-08-26
15+
### Fixed
16+
- Extract session cookie in a safe manner
17+
1018
## [3.77.1] - 2021-02-05
1119
- Increase HTTP agents connection pools freeSockets and lifetime
1220

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vtex/api",
3-
"version": "3.77.2",
3+
"version": "3.77.3",
44
"description": "VTEX I/O API client",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",

src/clients/Segment.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import parseCookie from 'cookie'
21
import { prop } from 'ramda'
32

43
import { PRODUCT_HEADER } from '../constants'
54
import { inflightUrlWithQuery, JanusClient } from '../HttpClient'
5+
import { extractSessionCookie } from './Session'
66

77
export interface SegmentData {
88
campaigns?: any
@@ -19,7 +19,6 @@ export interface SegmentData {
1919
[key: string]: any
2020
}
2121

22-
const SEGMENT_COOKIE = 'vtex_segment'
2322
const SEGMENT_MAX_AGE_S = 60 * 60 // 60 minutes - segment is actually immutable
2423
const ALLOWED_QUERY_PREFIXES = ['utm', 'cultureInfo', 'supportedLocales']
2524

@@ -71,15 +70,11 @@ export class Segment extends JanusClient {
7170
public getOrCreateSegment = async (query?: Record<string, string>, token?: string) => {
7271
const {
7372
data: segmentData,
74-
headers: {
75-
'set-cookie': [setCookies],
76-
},
73+
headers,
7774
} = await this.rawSegment(token, query)
78-
const parsedCookie = parseCookie.parse(setCookies)
79-
const segmentToken = prop(SEGMENT_COOKIE, parsedCookie)
8075
return {
8176
segmentData,
82-
segmentToken,
77+
segmentToken: extractSessionCookie(headers) || token,
8378
}
8479
}
8580

src/clients/Session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class Session extends JanusClient {
5151
}
5252
}
5353

54-
function extractSessionCookie(headers: Record<string, string>) {
54+
export function extractSessionCookie(headers: Record<string, string>) {
5555
for (const setCookie of headers['set-cookie'] || []) {
5656
const parsedCookie = parseCookie.parse(setCookie)
5757
const sessionCookie = parsedCookie[SESSION_COOKIE]

0 commit comments

Comments
 (0)