Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/evolution-backend/src/models/interviews.db.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,18 @@ const getRawWhereClause = (
): string | [string, string | boolean | number] | undefined => {
// Make sure the field is a legitimate field to avoid sql injection. Field
// is either the name of a field, or a dot-separated path in a json object
// of the 'responses' field. We should not accept anything else.
// of the 'responses' field, or an audit name for validateParams,
// which includes "-" and ":". We should not accept anything else.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If different fields have different regexes, we should not have only one regex here. Instead, we should make the match with - and : in the case of audits and with . in the case of responses. Otherwise, there may be eventual unexpected results

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a check for field = audits

// TODO Once the individual surveys are typed and the expected
// responses are known in advance, try to completely type the responses
// object and make sure the field here matches an actual path
const dotSeparatedStringRegex = /^[\w\.]*$/g;
const match = field.match(dotSeparatedStringRegex);
let regex: RegExp;
if (field === 'audits') {
regex = /^[\w\:\-\.]*$/g;
} else {
regex = /^[\w\.]*$/g;
}
const match = field.match(regex);
if (match === null) {
throw new TrError(
`Invalid field for where clause in ${tableName} database`,
Expand Down Expand Up @@ -430,7 +436,7 @@ const getRawWhereClause = (
if (typeof filter.value !== 'string') {
return undefined;
}
const match = filter.value.match(dotSeparatedStringRegex);
const match = filter.value.match(regex);
if (match === null) {
throw new TrError(
`Invalid value for where clause in ${tableName} database`,
Expand Down