Skip to content

Commit 90af079

Browse files
committed
Prevent invalid column names
1 parent b398894 commit 90af079

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

spec/CloudCode.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,21 @@ describe('Cloud Code', () => {
216216
);
217217
});
218218

219+
it('test beforeSave with invalid field', async () => {
220+
Parse.Cloud.beforeSave('BeforeSaveChanged', function (req) {
221+
req.object.set('length', 0);
222+
});
223+
224+
const obj = new Parse.Object('BeforeSaveChanged');
225+
obj.set('foo', 'bar');
226+
try {
227+
await obj.save();
228+
expect(false).toBe(true);
229+
} catch (e) {
230+
expect(e.message).toBe('Invalid field name: length.');
231+
}
232+
});
233+
219234
it("test beforeSave changed object fail doesn't change object", async function () {
220235
Parse.Cloud.beforeSave('BeforeSaveChanged', function (req) {
221236
if (req.object.has('fail')) {

spec/ParseObject.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,4 +2045,15 @@ describe('Parse.Object testing', () => {
20452045
const object = new Parse.Object('CloudCodeIsNew');
20462046
await object.save();
20472047
});
2048+
2049+
it('cannot save object with invalid field', async () => {
2050+
const obj = new TestObject();
2051+
obj.set('className', 'bar');
2052+
try {
2053+
await obj.save();
2054+
expect(false).toBe(true);
2055+
} catch (e) {
2056+
expect(e.message).toBe('Invalid field name: className.');
2057+
}
2058+
});
20482059
});

spec/Schema.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ describe('SchemaController', () => {
123123
);
124124
});
125125

126+
it('validate invalid column names', async () => {
127+
const schema = await config.database.loadSchema();
128+
try {
129+
await schema.validateObject('Stuff', { className: 'Unknown' });
130+
expect(false).toBe(true);
131+
} catch (e) {
132+
expect(e.message).toBe('Invalid field name: className.');
133+
}
134+
});
135+
126136
it('updates when new fields are added', done => {
127137
config.database
128138
.loadSchema()

src/Controllers/SchemaController.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ const requiredColumns = Object.freeze({
155155
_Role: ['name', 'ACL'],
156156
});
157157

158+
const invalidColumns = ['className', 'length'];
159+
158160
const systemClasses = Object.freeze([
159161
'_User',
160162
'_Installation',
@@ -427,8 +429,9 @@ function classNameIsValid(className: string): boolean {
427429
}
428430

429431
// Valid fields must be alpha-numeric, and not start with an underscore or number
432+
// must not be a reserved key
430433
function fieldNameIsValid(fieldName: string): boolean {
431-
return classAndFieldRegex.test(fieldName);
434+
return classAndFieldRegex.test(fieldName) && !invalidColumns.includes(fieldName);
432435
}
433436

434437
// Checks that it's not trying to clobber one of the default fields of the class.

0 commit comments

Comments
 (0)