Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 527039b

Browse files
author
Hans Kristian Flaatten
committed
Add query parameter whitelisting
1 parent ce0af14 commit 527039b

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ useful when building an API and accepting various user specificed queries.
1010

1111
* Aliased query parameters
1212
* Blacklisted query parameters
13+
* Whitelisted query parameters
1314
* Basic operators
1415
* `$ne`
1516
* `$gt`
@@ -38,6 +39,7 @@ var MongoQS = require('mongo-querystring');
3839
* `Array` ops - list of supported operators
3940
* `object` alias - query param aliases
4041
* `object` blacklist - blacklisted query params
42+
* `object` whitelist - whitelisted query params
4143
* `object` custom - custom query params
4244

4345
#### Bult in custom queries

src/index.litcoffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@ops = opts?.ops or ['!', '^', '$', '~', '>', '<']
77
@alias = opts?.alias or {}
88
@blacklist = opts?.blacklist or {}
9+
@whitelist = opts?.whitelist or {}
910
@custom = opts?.custom or {}
1011
1112
for param, field of @custom
@@ -83,6 +84,7 @@ Main query param parser method which follows the following order of operations.
8384
res = {}
8485
8586
for key, val of query
87+
continue if Object.keys(@whitelist).length and not @whitelist[key]
8688
continue if @blacklist[key]
8789
continue if typeof val isnt 'string'
8890
continue if not /^[a-zæøå0-9-_.]+$/i.test key

test/suite.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ describe 'parse()', ->
155155
qs = new MongoQS blacklist: foo: true, bar: true
156156
assert.deepEqual qs.parse({foo: 'bar', bar: 'foo', baz: 'bax'}), baz: 'bax'
157157

158+
describe 'whitelisting', ->
159+
it 'should allow key', ->
160+
qs = new MongoQS whitelist: foo: true
161+
assert.deepEqual qs.parse({foo: 'bar', bar: 'foo', baz: 'bax'}), foo: 'bar'
162+
163+
it 'should allow multiple keys', ->
164+
qs = new MongoQS whitelist: foo: true, bar: true
165+
assert.deepEqual qs.parse({foo: 'bar', bar: 'foo', baz: 'bax'}), foo: 'bar', bar: 'foo'
166+
158167
describe 'custom', ->
159168
it 'should enable built in bbox handler', ->
160169
qs = new MongoQS custom: bbox: 'geojson'

0 commit comments

Comments
 (0)