Skip to content

Commit

Permalink
fix(sort): prevent sql injection by get column name using raw bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
kostyazgara committed Jun 10, 2023
1 parent 073b9ab commit 12368c4
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/sort/lib/interceptors/sort.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,31 @@ export class SortInterceptor<TRecord, TResult>
if (sort && Array.isArray(sort)) {
sort.forEach((property) => {
const [direction, path] = getSortDirection(property);
const [placeholder, bindings] = this.buildPath(path);
queryBuilder.orderByRaw(
`${addPrefixColumn(
this.buildPath(path),
options.alias,
)} ${direction} nulls ${direction === 'desc' ? 'last' : 'first'}`,
`${addPrefixColumn(placeholder, options.alias)} ${direction} nulls ${
direction === 'desc' ? 'last' : 'first'
}`,
bindings,
);
});
}

return next.handle();
}

private buildPath(path: string): string {
private buildPath(path: string): [string, string[]] {
const [column, ...jsonPath] = path.split('.');
if (!jsonPath.length) {
return path;
return ['??', [column]];
}
return `${column}->>${jsonPath
.map((property) => `'${property}'`)
.join('->>')}`;
const [lastProperty] = jsonPath.splice(-1);
if (!jsonPath.length) {
return [`??->>?`, [column, lastProperty]];
}
return [
`??->${jsonPath.map(() => '??').join('->')}->>?`,
[column, ...jsonPath, lastProperty],
];
}
}

0 comments on commit 12368c4

Please sign in to comment.