@preprio/toolkit 0.2.0-beta.4 → 0.2.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "0.2.0-beta.4";
2
+ var VERSION = "0.2.0-beta.6";
3
3
 
4
4
  // src/core/constants.ts
5
5
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -38,6 +38,11 @@ var UTM_PARAMS = [
38
38
  "utm_content",
39
39
  "utm_campaign"
40
40
  ];
41
+ var MAX_HEADER_VALUE_LENGTH = 2048;
42
+ function sanitizeHeaderValue(value) {
43
+ const stripped = value.replace(/[\u0000-\u001f\u007f]/g, "");
44
+ return stripped.length > MAX_HEADER_VALUE_LENGTH ? stripped.slice(0, MAX_HEADER_VALUE_LENGTH) : stripped;
45
+ }
41
46
  function serializeCookie(cookie) {
42
47
  const parts = [
43
48
  `${cookie.name}=${encodeURIComponent(cookie.value)}`,
@@ -69,6 +74,15 @@ function parseCookies(request) {
69
74
  function processPreprRequest(request, opts) {
70
75
  const requestHeaders = new Headers(request.headers);
71
76
  const responseCookies = [];
77
+ const inboundPreprHeaders = [];
78
+ requestHeaders.forEach((_value, key) => {
79
+ if (key.toLowerCase().startsWith("prepr-")) {
80
+ inboundPreprHeaders.push(key);
81
+ }
82
+ });
83
+ for (const key of inboundPreprHeaders) {
84
+ requestHeaders.delete(key);
85
+ }
72
86
  const requestCookies = parseCookies(request);
73
87
  const url = new URL(request.url);
74
88
  const searchParams = url.searchParams;
@@ -78,25 +92,28 @@ function processPreprRequest(request, opts) {
78
92
  for (const key of UTM_PARAMS) {
79
93
  const value = searchParams.get(key);
80
94
  if (value !== null) {
81
- requestHeaders.set(`Prepr-Context-${key}`, value);
95
+ requestHeaders.set(`Prepr-Context-${key}`, sanitizeHeaderValue(value));
82
96
  }
83
97
  }
84
98
  const referer = request.headers.get("referer");
85
99
  if (referer) {
86
- requestHeaders.set("Prepr-Context-initial_referral", referer);
100
+ requestHeaders.set(
101
+ "Prepr-Context-initial_referral",
102
+ sanitizeHeaderValue(referer)
103
+ );
87
104
  }
88
105
  const userAgent = request.headers.get("user-agent");
89
106
  if (userAgent) {
90
- requestHeaders.set("Prepr-User-Agent", userAgent);
107
+ requestHeaders.set("Prepr-User-Agent", sanitizeHeaderValue(userAgent));
91
108
  }
92
109
  requestHeaders.set("Prepr-Package", `@preprio/toolkit@${opts?.version ?? VERSION}`);
93
110
  const ip = request.headers.get("Cf-Connecting-Ip") ?? request.headers.get("x-real-ip");
94
111
  if (ip) {
95
- requestHeaders.set("Prepr-Visitor-IP", ip);
112
+ requestHeaders.set("Prepr-Visitor-IP", sanitizeHeaderValue(ip));
96
113
  }
97
114
  const hutkCookie = requestCookies.get(COOKIE_HUBSPOT);
98
115
  if (hutkCookie) {
99
- requestHeaders.set("Prepr-Hubspot-Id", hutkCookie);
116
+ requestHeaders.set("Prepr-Hubspot-Id", sanitizeHeaderValue(hutkCookie));
100
117
  }
101
118
  let uid = requestCookies.get(COOKIE_UID);
102
119
  if (!uid) {
@@ -136,16 +153,16 @@ function processPreprRequest(request, opts) {
136
153
  if (!isLivePreview) {
137
154
  const segmentCookie = features.segments ? requestCookies.get(COOKIE_SEGMENT) : void 0;
138
155
  if (segmentCookie) {
139
- requestHeaders.set("Prepr-Segments", segmentCookie);
156
+ requestHeaders.set("Prepr-Segments", sanitizeHeaderValue(segmentCookie));
140
157
  }
141
158
  const abCookie = features.abTesting ? requestCookies.get(COOKIE_VARIANT) : void 0;
142
159
  if (abCookie) {
143
- requestHeaders.set("Prepr-ABtesting", abCookie);
160
+ requestHeaders.set("Prepr-ABtesting", sanitizeHeaderValue(abCookie));
144
161
  }
145
162
  }
146
163
  const previewAb = features.abTesting ? searchParams.get(PARAM_VARIANT) : null;
147
164
  if (previewAb !== null) {
148
- requestHeaders.set("Prepr-ABtesting", previewAb);
165
+ requestHeaders.set("Prepr-ABtesting", sanitizeHeaderValue(previewAb));
149
166
  if (!isLivePreview) {
150
167
  responseCookies.push({
151
168
  name: COOKIE_VARIANT,
@@ -158,7 +175,7 @@ function processPreprRequest(request, opts) {
158
175
  }
159
176
  const previewSegment = features.segments ? searchParams.get(PARAM_SEGMENT) : null;
160
177
  if (previewSegment !== null) {
161
- requestHeaders.set("Prepr-Segments", previewSegment);
178
+ requestHeaders.set("Prepr-Segments", sanitizeHeaderValue(previewSegment));
162
179
  if (!isLivePreview) {
163
180
  responseCookies.push({
164
181
  name: COOKIE_SEGMENT,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preprio/toolkit",
3
- "version": "0.2.0-beta.4",
3
+ "version": "0.2.0-beta.6",
4
4
  "description": "Prepr personalization, A/B testing and preview toolkit for React, Next.js, Nuxt, Astro, SvelteKit and Express",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Prepr B.V. (https://prepr.io)",