Skip to content

Commit 6992bc7

Browse files
Merge pull request #7 from Crowdhandler/support/85986
Prevent reording of query string parameters and strip special CrowdHa…
2 parents 31c2113 + cae0da5 commit 6992bc7

6 files changed

Lines changed: 59 additions & 2 deletions

File tree

dist/originOverride.zip

212 Bytes
Binary file not shown.

dist/viewerRequest.zip

534 Bytes
Binary file not shown.

dist/viewerResponse.zip

230 Bytes
Binary file not shown.

handlerViewerRequest.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ module.exports.viewerRequest = async (event) => {
7979
// Destructure special params from query string if they exist.
8080
let {
8181
"ch-code": chCode,
82+
"ch-fresh": chFresh,
8283
"ch-id": chID,
84+
"ch-id-signature": chIDSignature,
8385
"ch-public-key": chPublicKey,
86+
"ch-requested": chRequested,
8487
} = queryString || {};
8588

8689
// This is the right most address found in the x-forwarded-for header and can be trusted as it was discovered via the TCP connection.
@@ -94,17 +97,23 @@ module.exports.viewerRequest = async (event) => {
9497
// Remove special params from the queryString object now that we don't need them anymore
9598
if (queryString) {
9699
delete queryString["ch-code"];
100+
delete queryString["ch-fresh"]
97101
delete queryString["ch-id"];
102+
delete queryString["ch-id-signature"];
98103
delete queryString["ch-public-key"];
104+
delete queryString["ch-requested"];
99105
}
100106

101107
// Stringify queryString
102108
queryString = helpers.queryStringParse(queryString, "string");
103109
// Prepend & to the query string if it's not empty as we're always going to need to chain it to ?${FQDN}
104110
if (queryString) {
111+
//Update the querystring to remove special CH parameters
112+
request.querystring = queryString;
105113
queryString = `?${queryString}`;
106114
}
107115

116+
108117
//URL encode the targetURL to be used later in redirects
109118
let targetURL;
110119
if (queryString) {
@@ -118,9 +127,11 @@ module.exports.viewerRequest = async (event) => {
118127
let crowdhandlerCookieValue = parsedCookies["crowdhandler"];
119128

120129
// Prioritise tokens in the ch-id parameter and fallback to ones found in the cookie.
130+
let freshlyPromoted;
121131
let token;
122132
if (chID) {
123133
console.log("Using ch-id value as token");
134+
freshlyPromoted = true;
124135
token = chID;
125136
} else if (crowdhandlerCookieValue) {
126137
console.log("Using cookie value as token");
@@ -129,6 +140,16 @@ module.exports.viewerRequest = async (event) => {
129140
token = null;
130141
}
131142

143+
if (freshlyPromoted) {
144+
let redirectLocation
145+
if (queryString) {
146+
redirectLocation = `${FQDN}${queryString}`
147+
} else {
148+
redirectLocation = FQDN
149+
}
150+
return http_helpers.redirect302Response(redirectLocation, token);
151+
}
152+
132153
// Check in with CrowdHandler
133154
async function checkStatus() {
134155
let headers = {

helpers/http.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ export const redirect302Response = function (redirect_location, token) {
155155
value: `crowdhandler=${token}; path=/; Secure; HttpOnly`,
156156
},
157157
],
158+
"cache-control": [
159+
{
160+
key: "Cache-Control",
161+
value: "no-cache, no-store, must-revalidate",
162+
},
163+
],
164+
expires: [
165+
{
166+
key: "Expires",
167+
value: "Fri, 01 Jan 1970 00:00:00 GMT",
168+
},
169+
],
170+
pragma: [
171+
{
172+
key: "Pragma",
173+
value: "no-cache",
174+
},
175+
],
158176
},
159177
};
160178
} else {
@@ -168,6 +186,24 @@ export const redirect302Response = function (redirect_location, token) {
168186
value: redirect_location,
169187
},
170188
],
189+
"cache-control": [
190+
{
191+
key: "Cache-Control",
192+
value: "no-cache, no-store, must-revalidate",
193+
},
194+
],
195+
expires: [
196+
{
197+
key: "Expires",
198+
value: "Fri, 01 Jan 1970 00:00:00 GMT",
199+
},
200+
],
201+
pragma: [
202+
{
203+
key: "Pragma",
204+
value: "no-cache",
205+
},
206+
],
171207
},
172208
};
173209
}

helpers/misc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export const parseCookies = function (headers) {
4545

4646
export const queryStringParse = function (querystring, type) {
4747
if (querystring && type === "object") {
48-
return queryString.parse(querystring);
48+
return queryString.parse(querystring, {sort: false});
4949
} else if (querystring && type === "string") {
50-
return queryString.stringify(querystring);
50+
return queryString.stringify(querystring, {sort: false});
5151
}
5252
};

0 commit comments

Comments
 (0)