Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set CORS headers #304

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/routes/questions/glossary.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {jsonCORS} from '../../server-utils/responses'
import {LoaderArgs} from '@remix-run/cloudflare'
import {reloadInBackgroundIfNeeded} from '~/server-utils/kv-cache'
import {loadGlossary} from '~/server-utils/stampy'

export const loader = async ({request}: LoaderArgs) => {
return await loadGlossary(request)
const data = await loadGlossary(request)
return jsonCORS<typeof data>(data)
}
type Data = ReturnType<typeof loader>
type Data = ReturnType<typeof loadGlossary>

export function fetchGlossary() {
const url = `/questions/glossary`
Expand Down
4 changes: 3 additions & 1 deletion app/routes/questions/search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {LoaderArgs} from '@remix-run/cloudflare'
import {jsonCORS} from '../../server-utils/responses'

export const loader = async ({request}: LoaderArgs) => {
const url = new URL(request.url)
Expand All @@ -7,7 +8,8 @@ export const loader = async ({request}: LoaderArgs) => {

if (!question) return []

return await search(question, onlyLive)
const results = await search(question, onlyLive)
return jsonCORS(results)
}

export function search(question: string, onlyLive: boolean) {
Expand Down
9 changes: 9 additions & 0 deletions app/server-utils/responses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {json} from '@remix-run/cloudflare'

export const jsonCORS = <T>(data: T) =>
json(data, {
headers: {
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Origin': ALLOW_ORIGINS,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be set for each request to the endpoints that use this function. Should be ok, but I can change it to only do so when called from a domain what is allowed

},
})
1 change: 1 addition & 0 deletions remix.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ declare const CODA_TOKEN: string
declare const CODA_INCOMING_TOKEN: string
declare const CODA_WRITES_TOKEN: string
declare const NLP_SEARCH_ENDPOINT: string
declare const ALLOW_ORIGINS: string
29 changes: 27 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
import {createEventHandler} from '@remix-run/cloudflare-workers'
import url from 'url'
import {createEventHandler, handleAsset} from '@remix-run/cloudflare-workers'
import * as build from '@remix-run/dev/server-build'

addEventListener('fetch', createEventHandler({build, mode: process.env.NODE_ENV}))
const CORS_ASSETS = ['/tfWorker.js']

const isCorsEnabledAsset = (event) => {
const parsedUrl = url.parse(event.request.url)
const pathname = parsedUrl.pathname

return CORS_ASSETS.includes(pathname)
}

const fetchCorsAsset = (event) => {
const resp = handleAsset(event, build)
return event.respondWith(
resp.then((res) => {
const headers = new Headers(res.headers)
headers.set('Access-Control-Allow-Origin', ALLOW_ORIGINS || '')
headers.set('Access-Control-Allow-Methods', 'GET, OPTIONS')
return new Response(res.body, {headers})
})
)
}

const handler = createEventHandler({build, mode: process.env.NODE_ENV})
addEventListener('fetch', async (event) =>
isCorsEnabledAsset(event) ? fetchCorsAsset(event) : handler(event)
)
1 change: 1 addition & 0 deletions wrangler.toml.template
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ CODA_TOKEN = "{CODA_TOKEN}"
CODA_INCOMING_TOKEN = "{CODA_INCOMING_TOKEN}"
CODA_WRITES_TOKEN = "{CODA_WRITES_TOKEN}"
NLP_SEARCH_ENDPOINT = "https://stampy-nlp-t6p37v2uia-uw.a.run.app/"
ALLOW_ORIGINS = "https://chat.aisafety.info"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered also setting this in the github deployment script, but left it out for now - this is pretty much the only allowed external site. If there are more in the future, then it can be changed to '*' or set via an actions env variable

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy for GET request to be allowed from any origin (*), we don't send sensitive data 🤷

but if you want to maintain the list of allowed origins, that is also fine