Handled invalid filters in members event repository

fix https://linear.app/tryghost/issue/SLO-82/query-error-unexpected-character-in-filter-at-char-1

- previously, we weren't handling a parsing error, and just bubbling it
  back up the chain
- this would result in an InternalServerError somewhere, which caused
  500s
- we can handle this, because it's just a bad filter
- this adds handling so we return a 422 upon receiving an invalid filter
This commit is contained in:
Daniel Lockyer 2024-05-01 11:48:16 +02:00 committed by Daniel Lockyer
parent ddac3a9e8b
commit 31bdef94cd
2 changed files with 15 additions and 1 deletions

View File

@ -777,7 +777,15 @@ module.exports = class EventRepository {
}
const allowList = ['data.created_at', 'data.member_id', 'data.post_id', 'type', 'id'];
const parsed = nql(filter).parse();
let parsed;
try {
parsed = nql(filter).parse();
} catch (e) {
throw new errors.BadRequestError({
message: e.message
});
}
const keys = getUsedKeys(parsed);
for (const key of keys) {

View File

@ -19,6 +19,12 @@ describe('EventRepository', function () {
});
});
it('throws when using invalid filter', function () {
should.throws(() => {
eventRepository.getNQLSubset('undefined');
}, errors.BadRequestError);
});
it('throws when using properties that aren\'t in the allowlist', function () {
should.throws(() => {
eventRepository.getNQLSubset('(types:1)');