Skip to content

Commit

Permalink
fix: Fix redaction of credentials in Firestore settings (#1989)
Browse files Browse the repository at this point in the history
* Revert "fix: Remove incorrect,unreachable and unused code (#1983)"

This reverts commit 133f4da.

* fix: Fix redaction of credentials in Firestore settings.
  • Loading branch information
MarkDuckworth authored Jan 25, 2024
1 parent 99d60a6 commit 98e668b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dev/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,13 @@ export class Firestore implements firestore.Firestore {
}

this._settings = settings;
this._settings.toJSON = function () {
const temp = Object.assign({}, this);
if (temp.credentials) {
temp.credentials = {private_key: '***', client_email: '***'};
}
return temp;
};
this._serializer = new Serializer(this);
}

Expand Down
35 changes: 35 additions & 0 deletions dev/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1372,3 +1372,38 @@ describe('getAll() method', () => {
});
});
});

describe('toJSON', () => {
it('Serializing Firestore settings redacts credentials', () => {
const firestore = new Firestore.Firestore({
projectId: 'myProjectId',
credentials: {client_email: 'foo@bar', private_key: 'asdf1234'},
});

const serializedSettings = JSON.stringify(firestore._settings);

// Instead of validating the serialized string for redacted credentials,
// parse the settings and check the credential values.
const parsedSettings = JSON.parse(serializedSettings);
expect(parsedSettings.credentials.client_email).to.equal('***');
expect(parsedSettings.credentials.private_key).to.equal('***');
});

it('Serializing Firestore instance', () => {
const firestore = new Firestore.Firestore({
projectId: 'myProjectId',
credentials: {client_email: 'foo@bar', private_key: 'asdf1234'},
});

const serializedFirestore = JSON.stringify(firestore);

// Instead of validating the serialized string,
// parse the JSON back to an object and check the properties.
const expectedParsedFirestore = {
projectId: 'myProjectId',
};

const parsedFirestore = JSON.parse(serializedFirestore);
expect(parsedFirestore).to.deep.equal(expectedParsedFirestore);
});
});

0 comments on commit 98e668b

Please sign in to comment.