Skip to content

Commit 0111e43

Browse files
committed
Details page for cql-select rule
1 parent 544d388 commit 0111e43

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const cds = require('@sap/cds')
2+
module.exports = class AdminService extends cds.ApplicationService { init() {
3+
const { Authors } = cds.entities('AdminService')
4+
5+
this.before (['CREATE', 'UPDATE'], Authors, async (req) => {
6+
await SELECT`ID`.from `Authors`.where `name = ${req.data.name}` // [!code highlight]
7+
})
8+
9+
return super.init()
10+
}}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const cds = require('@sap/cds')
2+
module.exports = class AdminService extends cds.ApplicationService { init() {
3+
const { Authors } = cds.entities('AdminService')
4+
5+
this.before (['CREATE', 'UPDATE'], Authors, async (req) => {
6+
await SELECT`ID`.from `Authors`.where (`name = ${req.data.name}`) // [!code error]
7+
})
8+
9+
return super.init()
10+
}}

tools/cds-lint/rules/_menu.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
# [start-elements-lowercase](start-elements-lowercase)
1818
# [start-entities-uppercase](start-entities-uppercase)
1919
# [valid-csv-header](valid-csv-header)
20+
# [use-cql-select-template-strings](use-cql-select-template-strings)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
status: released
3+
---
4+
5+
<script setup>
6+
import PlaygroundBadge from '../components/PlaygroundBadge.vue'
7+
</script>
8+
9+
# use-cql-select-template-strings
10+
11+
## Rule Details
12+
13+
Discourage use of <code>SELECT(\`...\`)</code>, which allows [SQL injection attacks](../../../node.js/cds-ql#avoiding-sql-injection), in favour of <code>SELECT \`...\`</code>.
14+
15+
#### Version
16+
This rule was introduced in `@sap/eslint-plugin-cds 4.0.2`.
17+
18+
## Examples
19+
20+
### &nbsp; Correct example
21+
22+
In the following example, the `where` clause is a proper tagged template literal, so that the `req.data.name` expression can be validated before the SELECT is executed:
23+
24+
::: code-group
25+
<<< ../examples/use-cql-select-template-strings/correct/srv/admin-service.js#snippet{js:line-numbers} [srv/admin-service.js]
26+
:::
27+
<PlaygroundBadge
28+
name="use-cql-select-template-strings"
29+
kind="correct"
30+
:files="['srv/admin-service.js']"
31+
/>
32+
33+
### &nbsp; Incorrect example
34+
35+
In the following example, the `where` clause is *not* a proper tagged template literal as it's enclosed by parentheses.
36+
In consequence, the `req.data.name` expression *cannot* be validated but is added as is to the SELECT statement.
37+
This is prone to SQL injection attacks.
38+
39+
::: code-group
40+
<<< ../examples/use-cql-select-template-strings/incorrect/srv/admin-service.js#snippet{js:line-numbers} [srv/admin-service.js]
41+
:::
42+
<PlaygroundBadge
43+
name="use-cql-select-template-strings"
44+
kind="incorrect"
45+
:files="['srv/admin-service.js']"
46+
/>

0 commit comments

Comments
 (0)