Fix suggest on staging in Firefox (#2277)

Fix #2245
This commit is contained in:
Paul Melnikow
2018-11-09 14:06:13 -05:00
committed by GitHub
parent c4af2cac53
commit e4e5628207
4 changed files with 25 additions and 7 deletions

1
Procfile Normal file
View File

@@ -0,0 +1 @@
web: node server

View File

@@ -49,7 +49,9 @@ export default class SuggestionAndSearch extends React.Component {
let suggestions
try {
const json = await res.json()
suggestions = json.badges
// This doesn't validate the response. The default value here prevents
// a crash if the server returns {"err":"Disallowed"}.
suggestions = json.badges || []
} catch (e) {
suggestions = []
}

View File

@@ -107,16 +107,31 @@ function setRoutes(allowedOrigin, githubApiProvider, server) {
server.ajax.on('suggest/v1', (data, end, ask) => {
// The typical dev and production setups are cross-origin. However, in
// Heroku deploys and some self-hosted deploys these requests may come from
// the same host.
// the same host. Chrome does not send an Origin header on same-origin
// requests, but Firefox does.
//
// It would be better to solve this problem using some well-tested
// middleware.
const origin = ask.req.headers.origin
if (origin) {
if (allowedOrigin.includes(origin)) {
ask.res.setHeader('Access-Control-Allow-Origin', origin)
} else {
let host
try {
host = new URL(origin).hostname
} catch (e) {
ask.res.setHeader('Access-Control-Allow-Origin', 'null')
end({ err: 'Disallowed' })
return
}
if (host !== ask.req.headers.host) {
if (allowedOrigin.includes(origin)) {
ask.res.setHeader('Access-Control-Allow-Origin', origin)
} else {
ask.res.setHeader('Access-Control-Allow-Origin', 'null')
end({ err: 'Disallowed' })
return
}
}
}
let url

View File

@@ -195,8 +195,8 @@
"wait-promise": "^0.4.1"
},
"engines": {
"node": ">= 8.x",
"npm": "5.x"
"node": ">= 8",
"npm": ">= 5"
},
"babel": {
"presets": [