Skip to content

Commit 312d065

Browse files
committed
Throw error when query with wrongly encoded parameter
1 parent b1a9536 commit 312d065

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

spec/RestQuery.spec.js

Lines changed: 48 additions & 0 deletions
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
});

src/Routers/ClassesRouter.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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) {
@@ -33,6 +35,12 @@ export class ClassesRouter {
3335
if (typeof body.where === 'string') {
3436
body.where = JSON.parse(body.where);
3537
}
38+
39+
let count = typeof body.where === 'object' ? 1 : 0;
40+
if (body.length != options.length + count) {
41+
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Improper encode of parameter');
42+
}
43+
3644
return rest.find(req.config, req.auth, req.params.className, body.where, options)
3745
.then((response) => {
3846
if (response && response.results) {

0 commit comments

Comments
 (0)