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

Add host checking, restrict URLs #1780

Merged
merged 5 commits into from
Nov 12, 2019
Merged

Add host checking, restrict URLs #1780

merged 5 commits into from
Nov 12, 2019

Conversation

samtstern
Copy link
Contributor

@samtstern samtstern commented Nov 9, 2019

Description

Fixes #1722
Fixes #1773

Scenarios Tested

functions

exports.helloWorld = functions.https.onRequest(async (req, res) => {
    res.json({
        hello: 'world',
        date: new Date()
    });
});

exports.firestoreWriter = functions.https.onRequest(async (req, res) => {
    await admin.firestore().doc('foo/bar').set({
        date: new Date()
    });

    res.json({ ok: 'ok' })
});

exports.firestoreReader = functions.firestore.document('foo/bar').onWrite(async (change, ctx) => {
    console.log("Firestore Write:", JSON.stringify(change.after.data()));
});

firebase.json

{
  "emulators": {
    "functions": {
      "host": "0.0.0.0",
      "port": 5001
    },
    "database": {
      "port": 9000
    }
  }
}

Start the emulators:

$ npx firebase emulators:start --only functions,firestore
i  Starting emulators: ["functions","firestore"]
✔  functions: Using node@10 from host.
✔  functions: Emulator started at http://0.0.0.0:5001
i  firestore: Serving ALL traffic (including WebChannel) on http://localhost:8080
⚠  firestore: Support for WebChannel on a separate port (8081) is DEPRECATED and will go away soon. Please use port above instead.
i  firestore: Emulator logging to firestore-debug.log
✔  firestore: Emulator started at http://localhost:8080
i  firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
i  functions: Watching "/tmp/tmp.ITmKYw5141/functions" for Cloud Functions...
✔  functions[helloWorld]: http function initialized (http://0.0.0.0:5001/fir-dumpster/us-central1/helloWorld).
✔  functions[firestoreWriter]: http function initialized (http://0.0.0.0:5001/fir-dumpster/us-central1/firestoreWriter).
✔  functions[firestoreReader]: firestore function initialized.
✔  All emulators started, it is now safe to connect.

Run some functions:

$ http http://0.0.0.0:5001/fir-dumpster/us-central1/firestoreWriter
HTTP/1.1 200 OK
connection: keep-alive
content-length: 11
content-type: application/json; charset=utf-8
date: Sat, 09 Nov 2019 00:19:41 GMT
etag: W/"b-2F/2BWc0KYbtLqL5U2Kv5B6uQUQ"
x-powered-by: Express

{
    "ok": "ok"
}

Logs look good:

i  functions: Beginning execution of "firestoreWriter"
i  functions: Finished "firestoreWriter" in ~1s
i  functions: Beginning execution of "firestoreReader"
>  Firestore Write: {"date":{"_seconds":1573258780,"_nanoseconds":854000000}}
i  functions: Finished "firestoreReader" in ~1s

Sample Commands

@googlebot googlebot added the cla: yes Manual indication that this has passed CLA. label Nov 9, 2019
@samtstern samtstern requested a review from bkendall November 9, 2019 00:21
@coveralls
Copy link

Coverage Status

Coverage remained the same at 66.248% when pulling 72b6ec2 on ss-functions-host into 5f77df9 on master.

@samtstern samtstern requested a review from abeisgoat November 11, 2019 22:08
Copy link
Contributor

@bkendall bkendall left a comment

Choose a reason for hiding this comment

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

One test nit, otherwise looks fine.

src/test/emulators/functionsEmulator.spec.ts Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Manual indication that this has passed CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Functions emulator matches too many URLs Functions emulator not respecting the host argument
4 participants