Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 5c27825

Browse files
authored
Merge pull request #12 from uniquelyparticular/fix/postrequests
Fix/postrequests
2 parents 554f2b1 + 5233ab4 commit 5c27825

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ Built with [Micro](https://github.com/zeit/micro)! 🤩
1111
Create a `.env` at the project root with the following credentials:
1212

1313
```dosini
14-
PROXY_REFERER_WHITELIST=localhost,*.zendesk.com,*.myshopify.com,*.now.sh
14+
PROXY_ORIGIN_WHITELIST=localhost,*.zendesk.com,*.myshopify.com,*.now.sh
1515
PROXY_DESTINATION_WHITELIST=api.stripe.com,api.goshippo.com,api.shipengine.com,api.moltin.com,*.myshopify.com,*.salesforce.com,*.demandware.net
1616
```
1717

18-
`PROXY_REFERER_WHITELIST` is a comma separated list of patterns to match against the incoming requests 'Referer' header (ex. `localhost,*.myawesomesite.com,*.now.sh`)
19-
_(and yes, 'REFERER' is intentionally misspelled to match the http header! 😉)_
18+
`PROXY_ORIGIN_WHITELIST` is a comma separated list of patterns to match against the incoming requests 'Origin' header (ex. `localhost,*.myawesomesite.com,*.now.sh`)
2019

2120
`PROXY_DESTINATION_WHITELIST` is a comma separated list of patterns to match against the URI you are proxying requests to. (ex. `api.somethingsecure.com,*.somotherapi.com`)
2221

now.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"env": {
55
"NODE_ENV": "production",
66
"PROXY_PREFIX": "@demo-proxy-prefix",
7-
"PROXY_REFERER_WHITELIST": "@demo-proxy-referer-whitelist",
7+
"PROXY_ORIGIN_WHITELIST": "@demo-proxy-origin-whitelist",
88
"PROXY_DESTINATION_WHITELIST": "@demo-proxy-destination-whitelist",
99
"PROXY_REPLACE_GATEWAY_PK": "@particular-gateway-pk",
1010
"PROXY_REPLACE_GATEWAY_SK": "@particular-gateway-sk"

src/index.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,11 @@ const parseURL = url => {
5353
}
5454

5555
const isAuthorized = (referer, whitelist = []) => {
56+
// console.log('referer', referer)
5657
// console.log('whitelist', whitelist)
57-
const { hostname, protocol } = parseURL(referer)
58+
const { hostname } = parseURL(referer)
5859
// console.log('hostname', hostname)
59-
// console.log('protocol', protocol)
60-
return (
61-
isWhitelisted(hostname, whitelist) &&
62-
(protocol === 'https:' ||
63-
(protocol === 'http:' && hostname === 'localhost'))
64-
)
60+
return isWhitelisted(hostname, whitelist)
6561
}
6662

6763
const toRegexArray = csv => {
@@ -71,7 +67,7 @@ const toRegexArray = csv => {
7167
.map(value => new RegExp(`^${prepareRegex(value)}$`))
7268
}
7369

74-
const refererWhiteList = toRegexArray(process.env.PROXY_REFERER_WHITELIST)
70+
const originWhiteList = toRegexArray(process.env.PROXY_ORIGIN_WHITELIST)
7571
const destinationWhiteList = toRegexArray(
7672
process.env.PROXY_DESTINATION_WHITELIST
7773
)
@@ -88,6 +84,16 @@ const filterValue = input => {
8884
return mustachReplace(input, envReplacements, proxyReplaceMatchPrefix)
8985
}
9086

87+
const getOrigin = (origin, referer) => {
88+
// console.log('getOrigin, origin', origin)
89+
// console.log('getOrigin, referer', referer)
90+
const subOrigin = referer.match(/\?origin=([^\?&]+)/)
91+
if (subOrigin) {
92+
origin = decodeURIComponent(subOrigin[1])
93+
}
94+
return origin
95+
}
96+
9197
const requestHeaders = headers => {
9298
const {
9399
host,
@@ -104,11 +110,11 @@ const requestHeaders = headers => {
104110

105111
const defaultHeaders = {
106112
'x-forwarded-by': `${name}-${version}`,
107-
'x-forwarded-origin': origin,
113+
'x-forwarded-origin': getOrigin(origin, referer),
108114
'x-forwarded-referer': referer
109115
}
110116
const modifiedHeaders = { ...filteredHeaders, ...defaultHeaders }
111-
console.log('requestHeaders, modifiedHeaders', modifiedHeaders)
117+
// console.log('requestHeaders, modifiedHeaders', modifiedHeaders)
112118
return modifiedHeaders
113119
}
114120

@@ -207,7 +213,13 @@ const handleProxy = async (req, res) => {
207213
if (!req.headers.referer) {
208214
return noReferer(req, res)
209215
}
210-
if (!isAuthorized(req.headers.referer, refererWhiteList)) {
216+
217+
if (
218+
!isAuthorized(
219+
getOrigin(req.headers.origin, req.headers.referer),
220+
originWhiteList
221+
)
222+
) {
211223
return notAuthorized(req, res)
212224
}
213225

@@ -229,15 +241,23 @@ const handleProxy = async (req, res) => {
229241
if (req.method !== 'GET') {
230242
const txt = await text(req)
231243
// console.log('txt', txt)
232-
if (txt) {
233-
const body = JSON.parse(txt)
244+
if (txt && txt !== '') {
245+
let body
246+
247+
if (req.headers['content-type'] === 'application/json') {
248+
body = JSON.parse(txt)
249+
} else {
250+
body = txt
251+
}
252+
234253
// console.log('body', body)
235254
if (body) {
236-
fetchOptions.body = JSON.stringify(body)
255+
fetchOptions.body = body
237256
}
238-
// console.log('body fetchOptions', fetchOptions)
257+
// console.log('fetchOptions.body', fetchOptions.body)
239258
}
240259
}
260+
// console.log('fetchOptions', fetchOptions)
241261
return processRequest(res, req.headers.origin, destinationURL, fetchOptions)
242262
} catch (error) {
243263
const jsonError = _toJSON(error)

0 commit comments

Comments
 (0)