Skip to content

Add support for running the emulators over SSL #8967

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
7 changes: 7 additions & 0 deletions .changeset/gentle-laws-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@firebase/database-compat": patch
"@firebase/database": patch
"@firebase/firestore": patch
---

Add SSL checks to `connectDatabaseEmulator` and `connectFirestoreEmulator`
16 changes: 16 additions & 0 deletions packages/database-compat/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,22 @@ describe('Database Tests', () => {
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.false;
});

it('uses ssl when useEmulator is called with https is specified', () => {
const db = firebase.database();
db.useEmulator('https://localhost', 80);
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.true;
expect((db as any)._delegate._repo.repoInfo_.host).to.equal('localhost:80');
expect((db as any)._delegate._repo.repoInfo_.secure).to.be.true;
});

it('uses ssl when useEmulator is called with wss is specified', () => {
const db = firebase.database();
db.useEmulator('wss://localhost', 80);
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.true;
expect((db as any)._delegate._repo.repoInfo_.host).to.equal('localhost:80');
expect((db as any)._delegate._repo.repoInfo_.secure).to.be.true;
});

it('cannot call useEmulator after use', () => {
const db = (firebase as any).database();

Expand Down
15 changes: 13 additions & 2 deletions packages/database/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,19 @@ function repoManagerApplyEmulatorSettings(
emulatorOptions: RepoInfoEmulatorOptions,
tokenProvider?: AuthTokenProvider
): void {
let ssl = false;
let finalHost = hostAndPort;
if (/^https:\/\//.test(finalHost)) {
ssl = true;
finalHost = finalHost.substring(8);
}
if (/^wss:\/\//.test(finalHost)) {
ssl = true;
finalHost = finalHost.substring(6);
}
repo.repoInfo_ = new RepoInfo(
hostAndPort,
/* secure= */ false,
finalHost,
/* secure= */ ssl,
repo.repoInfo_.namespace,
repo.repoInfo_.webSocketOnly,
repo.repoInfo_.nodeAdmin,
Expand Down Expand Up @@ -352,6 +362,7 @@ export function connectDatabaseEmulator(
): void {
db = getModularInstance(db);
db._checkNotDeleted('useEmulator');

const hostAndPort = `${host}:${port}`;
const repo = db._repoInternal;
if (db._instanceStarted) {
Expand Down
7 changes: 6 additions & 1 deletion packages/firestore/src/lite-api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ export function connectFirestoreEmulator(
} = {}
): void {
firestore = cast(firestore, Firestore);
let ssl = false;
if (/^https:\/\//.test(host)) {
ssl = true;
host = host.substring(8);
}
const settings = firestore._getSettings();
const existingConfig = {
...settings,
Expand All @@ -340,7 +345,7 @@ export function connectFirestoreEmulator(
const newConfig = {
...settings,
host: newHostSetting,
ssl: false,
ssl,
emulatorOptions: options
};
// No-op if the new configuration matches the current configuration. This supports SSR
Expand Down
11 changes: 11 additions & 0 deletions packages/firestore/test/unit/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,17 @@ describe('Settings', () => {
expect(db._getEmulatorOptions()).to.equal(emulatorOptions);
});

it('sets ssl to true if prefixed with https://', () => {
// Use a new instance of Firestore in order to configure settings.
const db = newTestFirestore();
const emulatorOptions = { mockUserToken: 'test' };
connectFirestoreEmulator(db, 'https://127.0.0.1', 9000, emulatorOptions);

expect(db._getSettings().host).to.exist.and.to.equal('127.0.0.1:9000');
expect(db._getSettings().ssl).to.exist.and.to.be.true;
expect(db._getEmulatorOptions()).to.equal(emulatorOptions);
});

it('prefers host from useEmulator to host from settings', () => {
// Use a new instance of Firestore in order to configure settings.
const db = newTestFirestore();
Expand Down
Loading