Skip to content

Commit ca4f778

Browse files
authored
Add validation on instance name (firebase#374)
* Add validation on instance name * changelog
1 parent 52fc6a9 commit ca4f778

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
added - validation on instance name for realtime database triggers

spec/providers/database.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ describe('Database Functions', () => {
245245
expect(instance).to.equal('https://foo.firebaseio.com');
246246
expect(path).to.equal('/bar');
247247
});
248+
249+
it('should throw an error if the given instance name contains anything except alphanumerics and dashes', () => {
250+
expect(() => {
251+
return database.resourceToInstanceAndPath(
252+
'projects/_/instances/a.bad.name/refs/bar'
253+
);
254+
}).to.throw(Error)
255+
expect(() => {
256+
return database.resourceToInstanceAndPath(
257+
'projects/_/instances/a_different_bad_name/refs/bar'
258+
);
259+
}).to.throw(Error)
260+
expect(() => {
261+
return database.resourceToInstanceAndPath(
262+
'projects/_/instances/BAD!!!!/refs/bar'
263+
);
264+
}).to.throw(Error)
265+
});
248266
});
249267

250268
describe('DataSnapshot', () => {

src/providers/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class RefBuilder {
240240
/* Utility function to extract database reference from resource string */
241241
/** @internal */
242242
export function resourceToInstanceAndPath(resource: string) {
243-
let resourceRegex = `projects/([^/]+)/instances/([^/]+)/refs(/.+)?`;
243+
let resourceRegex = `projects/([^/]+)/instances/([a-zA-Z0-9\-^/]+)/refs(/.+)?`;
244244
let match = resource.match(new RegExp(resourceRegex));
245245
if (!match) {
246246
throw new Error(

0 commit comments

Comments
 (0)