Merge pull request #191 from appy-one/fix/empty-keys-check

Empty keys pre-check
This commit is contained in:
Ewout Stortenbeker 2022-12-19 11:27:35 +01:00 committed by GitHub
commit 39c7efbbbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View file

@ -3997,12 +3997,11 @@ async function _writeNode(storage: AceBaseStorage, path: string, value: any, loc
else {
// Store object
Object.keys(value).forEach(key => {
// eslint-disable-next-line no-control-regex
if (/[\x00-\x08\x0b\x0c\x0e-\x1f/[\]\\]/.test(key)) {
throw new Error(`Invalid key "${key}" for object to store at path "${path}". Keys cannot contain control characters or any of the following characters: \\ / [ ]`);
}
if (key.length > 128) { throw new Error(`Key "${key}" is too long to store for object at path "${path}". Max key length is 128`); }
if (key.length === 0) { throw new Error(`Child key for path "${path}" is not allowed be empty`); }
const childPath = PathInfo.getChildPath(path, key); // `${path}/${key}`;
const val = value[key];
if (typeof val === 'function' || val === null) {

View file

@ -29,6 +29,26 @@ describe('Keys', () => {
});
it('should not allow empty keys', async () => {
// Created for issue #172 (https://github.com/appy-one/acebase/issues/172)
// Test what happens with child ''
try {
db.ref('empty-keys').child('');
fail('ref.child should not allow empty keys');
}
catch (err) {
// ok
}
// Test 'set' operation
let p = db.ref('empty-keys').set({ '': 'Empty key name must not be allowed' });
await expectAsync(p).toBeRejected();
// Test 'update' operation
p = db.ref('empty-keys').update({ '': 'Empty key name must not be allowed' });
await expectAsync(p).toBeRejected();
});
it('should not allow special characters \\ / [ ] ', async () => {
let p = db.ref('invalid').set({ 'forward/slash': 'Forward slashes are used to access nested objects' });