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

Commit

Permalink
Merge pull request #12 from uniquelyparticular/fix/postrequests
Browse files Browse the repository at this point in the history
Fix/postrequests
  • Loading branch information
agrohs authored May 25, 2019
2 parents 554f2b1 + 5233ab4 commit 5c27825
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ Built with [Micro](https://github.com/zeit/micro)! 🤩
Create a `.env` at the project root with the following credentials:

```dosini
PROXY_REFERER_WHITELIST=localhost,*.zendesk.com,*.myshopify.com,*.now.sh
PROXY_ORIGIN_WHITELIST=localhost,*.zendesk.com,*.myshopify.com,*.now.sh
PROXY_DESTINATION_WHITELIST=api.stripe.com,api.goshippo.com,api.shipengine.com,api.moltin.com,*.myshopify.com,*.salesforce.com,*.demandware.net
```

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

`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`)

Expand Down
2 changes: 1 addition & 1 deletion now.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"env": {
"NODE_ENV": "production",
"PROXY_PREFIX": "@demo-proxy-prefix",
"PROXY_REFERER_WHITELIST": "@demo-proxy-referer-whitelist",
"PROXY_ORIGIN_WHITELIST": "@demo-proxy-origin-whitelist",
"PROXY_DESTINATION_WHITELIST": "@demo-proxy-destination-whitelist",
"PROXY_REPLACE_GATEWAY_PK": "@particular-gateway-pk",
"PROXY_REPLACE_GATEWAY_SK": "@particular-gateway-sk"
Expand Down
50 changes: 35 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,11 @@ const parseURL = url => {
}

const isAuthorized = (referer, whitelist = []) => {
// console.log('referer', referer)
// console.log('whitelist', whitelist)
const { hostname, protocol } = parseURL(referer)
const { hostname } = parseURL(referer)
// console.log('hostname', hostname)
// console.log('protocol', protocol)
return (
isWhitelisted(hostname, whitelist) &&
(protocol === 'https:' ||
(protocol === 'http:' && hostname === 'localhost'))
)
return isWhitelisted(hostname, whitelist)
}

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

const refererWhiteList = toRegexArray(process.env.PROXY_REFERER_WHITELIST)
const originWhiteList = toRegexArray(process.env.PROXY_ORIGIN_WHITELIST)
const destinationWhiteList = toRegexArray(
process.env.PROXY_DESTINATION_WHITELIST
)
Expand All @@ -88,6 +84,16 @@ const filterValue = input => {
return mustachReplace(input, envReplacements, proxyReplaceMatchPrefix)
}

const getOrigin = (origin, referer) => {
// console.log('getOrigin, origin', origin)
// console.log('getOrigin, referer', referer)
const subOrigin = referer.match(/\?origin=([^\?&]+)/)
if (subOrigin) {
origin = decodeURIComponent(subOrigin[1])
}
return origin
}

const requestHeaders = headers => {
const {
host,
Expand All @@ -104,11 +110,11 @@ const requestHeaders = headers => {

const defaultHeaders = {
'x-forwarded-by': `${name}-${version}`,
'x-forwarded-origin': origin,
'x-forwarded-origin': getOrigin(origin, referer),
'x-forwarded-referer': referer
}
const modifiedHeaders = { ...filteredHeaders, ...defaultHeaders }
console.log('requestHeaders, modifiedHeaders', modifiedHeaders)
// console.log('requestHeaders, modifiedHeaders', modifiedHeaders)
return modifiedHeaders
}

Expand Down Expand Up @@ -207,7 +213,13 @@ const handleProxy = async (req, res) => {
if (!req.headers.referer) {
return noReferer(req, res)
}
if (!isAuthorized(req.headers.referer, refererWhiteList)) {

if (
!isAuthorized(
getOrigin(req.headers.origin, req.headers.referer),
originWhiteList
)
) {
return notAuthorized(req, res)
}

Expand All @@ -229,15 +241,23 @@ const handleProxy = async (req, res) => {
if (req.method !== 'GET') {
const txt = await text(req)
// console.log('txt', txt)
if (txt) {
const body = JSON.parse(txt)
if (txt && txt !== '') {
let body

if (req.headers['content-type'] === 'application/json') {
body = JSON.parse(txt)
} else {
body = txt
}

// console.log('body', body)
if (body) {
fetchOptions.body = JSON.stringify(body)
fetchOptions.body = body
}
// console.log('body fetchOptions', fetchOptions)
// console.log('fetchOptions.body', fetchOptions.body)
}
}
// console.log('fetchOptions', fetchOptions)
return processRequest(res, req.headers.origin, destinationURL, fetchOptions)
} catch (error) {
const jsonError = _toJSON(error)
Expand Down

0 comments on commit 5c27825

Please sign in to comment.