Skip to content

Commit

Permalink
fix: cross-site POST requests (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nushio authored Sep 8, 2022
1 parent 410a103 commit e2d8081
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {manifest} from 'MANIFEST';
import {toSvelteKitRequest} from './firebase-to-svelte-kit.js';

const server = new Server(manifest);

/**
* Firebase Cloud Function handler for SvelteKit
*
Expand Down
5 changes: 4 additions & 1 deletion src/files/firebase-to-svelte-kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* @return {import('@sveltejs/kit').IncomingRequest}
*/
export function toSvelteKitRequest(request) {
const host = `${request.headers['x-forwarded-proto']}://${request.headers.host}`;
// Firebase sometimes omits the protocol used. Default to http.
const protocol = request.headers['x-forwarded-proto'] || 'http';
// Firebase forwards the request to sveltekit, use the forwarded host.
const host = `${protocol}://${request.headers['x-forwarded-host']}`;
const {href, pathname, searchParams: searchParameters} = new URL(request.url || '', host);
// eslint-disable-next-line no-undef
return new Request(href, {
Expand Down
4 changes: 2 additions & 2 deletions tests/end-to-end/test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ if [[ "${RESULT}" != *"${EXPECTED_SUBSTRING}"* ]]; then
fi

echo "${INDICATOR}Test POST to '/todos' API"
EXPECTED_SUBSTRING='"text":"asdf"'
EXPECTED_SUBSTRING='{"type":"success","status":204}'
# expected result = {"uid":"","created_at":01234,"text":"asdf","done":false}
# generated from the browser & copied with 'copy for cURL' browser context menu
RESULT="$(curl -X POST "http://localhost:${PORT}/todos" \
RESULT="$(curl -X POST "http://localhost:${PORT}/todos?/add" \
-H "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0" \
-H "Accept: */*" \
-H "Accept-Language: en-GB,en;q=0.5" \
Expand Down

0 comments on commit e2d8081

Please sign in to comment.