Skip to content

Commit 39f8143

Browse files
committed
Merge pull request #410 from Marco129/query-constraints
Throw when query is encoded incorrectly
2 parents 5ed037f + 06b8157 commit 39f8143

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Diff for: spec/RestQuery.spec.js

+48
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ var cache = require('../src/cache');
44
var Config = require('../src/Config');
55
var rest = require('../src/rest');
66

7+
var querystring = require('querystring');
8+
var request = require('request');
9+
710
var config = new Config('test');
811
var nobody = auth.nobody(config);
912

@@ -92,4 +95,49 @@ describe('rest query', () => {
9295
}).catch((error) => { console.log(error); });
9396
});
9497

98+
it('query with wrongly encoded parameter', (done) => {
99+
rest.create(config, nobody, 'TestParameterEncode', {foo: 'bar'}
100+
).then(() => {
101+
return rest.create(config, nobody,
102+
'TestParameterEncode', {foo: 'baz'});
103+
}).then(() => {
104+
var headers = {
105+
'X-Parse-Application-Id': 'test',
106+
'X-Parse-REST-API-Key': 'rest'
107+
};
108+
request.get({
109+
headers: headers,
110+
url: 'http://localhost:8378/1/classes/TestParameterEncode?'
111+
+ querystring.stringify({
112+
where: '{"foo":{"$ne": "baz"}}',
113+
limit: 1
114+
}).replace('=', '%3D'),
115+
}, (error, response, body) => {
116+
expect(error).toBe(null);
117+
var b = JSON.parse(body);
118+
expect(b.code).toEqual(Parse.Error.INVALID_QUERY);
119+
expect(b.error).toEqual('Improper encode of parameter');
120+
done();
121+
});
122+
}).then(() => {
123+
var headers = {
124+
'X-Parse-Application-Id': 'test',
125+
'X-Parse-REST-API-Key': 'rest'
126+
};
127+
request.get({
128+
headers: headers,
129+
url: 'http://localhost:8378/1/classes/TestParameterEncode?'
130+
+ querystring.stringify({
131+
limit: 1
132+
}).replace('=', '%3D'),
133+
}, (error, response, body) => {
134+
expect(error).toBe(null);
135+
var b = JSON.parse(body);
136+
expect(b.code).toEqual(Parse.Error.INVALID_QUERY);
137+
expect(b.error).toEqual('Improper encode of parameter');
138+
done();
139+
});
140+
});
141+
});
142+
95143
});

Diff for: src/Routers/ClassesRouter.js

+11
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22
import PromiseRouter from '../PromiseRouter';
33
import rest from '../rest';
44

5+
import url from 'url';
6+
57
export class ClassesRouter {
68
// Returns a promise that resolves to a {response} object.
79
handleFind(req) {
810
let body = Object.assign(req.body, req.query);
911
let options = {};
12+
let allowConstraints = ['skip', 'limit', 'order', 'count', 'keys',
13+
'include', 'redirectClassNameForKey', 'where'];
14+
15+
for (var key in body) {
16+
if (allowConstraints.indexOf(key) === -1) {
17+
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Improper encode of parameter');
18+
}
19+
}
20+
1021
if (body.skip) {
1122
options.skip = Number(body.skip);
1223
}

0 commit comments

Comments
 (0)