Skip to content

Commit 98e668b

Browse files
fix: Fix redaction of credentials in Firestore settings (#1989)
* Revert "fix: Remove incorrect,unreachable and unused code (#1983)" This reverts commit 133f4da. * fix: Fix redaction of credentials in Firestore settings.
1 parent 99d60a6 commit 98e668b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

dev/src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,13 @@ export class Firestore implements firestore.Firestore {
745745
}
746746

747747
this._settings = settings;
748+
this._settings.toJSON = function () {
749+
const temp = Object.assign({}, this);
750+
if (temp.credentials) {
751+
temp.credentials = {private_key: '***', client_email: '***'};
752+
}
753+
return temp;
754+
};
748755
this._serializer = new Serializer(this);
749756
}
750757

dev/test/index.ts

+35
Original file line numberDiff line numberDiff line change
@@ -1372,3 +1372,38 @@ describe('getAll() method', () => {
13721372
});
13731373
});
13741374
});
1375+
1376+
describe('toJSON', () => {
1377+
it('Serializing Firestore settings redacts credentials', () => {
1378+
const firestore = new Firestore.Firestore({
1379+
projectId: 'myProjectId',
1380+
credentials: {client_email: 'foo@bar', private_key: 'asdf1234'},
1381+
});
1382+
1383+
const serializedSettings = JSON.stringify(firestore._settings);
1384+
1385+
// Instead of validating the serialized string for redacted credentials,
1386+
// parse the settings and check the credential values.
1387+
const parsedSettings = JSON.parse(serializedSettings);
1388+
expect(parsedSettings.credentials.client_email).to.equal('***');
1389+
expect(parsedSettings.credentials.private_key).to.equal('***');
1390+
});
1391+
1392+
it('Serializing Firestore instance', () => {
1393+
const firestore = new Firestore.Firestore({
1394+
projectId: 'myProjectId',
1395+
credentials: {client_email: 'foo@bar', private_key: 'asdf1234'},
1396+
});
1397+
1398+
const serializedFirestore = JSON.stringify(firestore);
1399+
1400+
// Instead of validating the serialized string,
1401+
// parse the JSON back to an object and check the properties.
1402+
const expectedParsedFirestore = {
1403+
projectId: 'myProjectId',
1404+
};
1405+
1406+
const parsedFirestore = JSON.parse(serializedFirestore);
1407+
expect(parsedFirestore).to.deep.equal(expectedParsedFirestore);
1408+
});
1409+
});

0 commit comments

Comments
 (0)