eslint-plugin-sql-template 2.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +50 -28
- package/index.js +7 -2
- package/package.json +23 -10
- package/rules/no-unsafe-query.js +83 -34
- package/.npmignore +0 -37
- package/.travis.yml +0 -7
- package/CHANGELOG.md +0 -10
- package/test/rules/no-unsafe-query_test.js +0 -54
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,47 +1,40 @@
|
|
|
1
1
|
# eslint-plugin-sql-template
|
|
2
2
|
|
|
3
|
-
ESLint plugin with rules for using the `sql` template tag from a library such as [sql-tag](https://github.com/
|
|
3
|
+
ESLint plugin with rules for using the `sql` template tag from a library such as [sql-tag](https://github.com/ruimarinho/sql-tag) on raw SQL queries.
|
|
4
4
|
|
|
5
5
|
That library escapes data provided to an SQL query statement via interpolation. This prevents, for instance, potential SQL injection attacks.
|
|
6
6
|
|
|
7
7
|
This ESLint plugin helps teams enforce the usage of that tag, to avoid overlooked vulnerabilities from creeping into their codebases.
|
|
8
8
|
|
|
9
|
+
## Status
|
|
10
|
+
|
|
11
|
+
[![npm version][npm-image]][npm-url]
|
|
12
|
+
[![build status][ci-image]][ci-url]
|
|
13
|
+
|
|
9
14
|
## Installation
|
|
10
15
|
|
|
11
16
|
```sh
|
|
12
|
-
|
|
17
|
+
npm install eslint eslint-plugin-sql-template --save-dev
|
|
13
18
|
```
|
|
14
19
|
|
|
15
20
|
## Usage
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
```yaml
|
|
20
|
-
plugins:
|
|
21
|
-
- sql-template
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
Then, you can add the custom rules to the `.eslint.yml` file:
|
|
25
|
-
|
|
26
|
-
```yaml
|
|
27
|
-
rules:
|
|
28
|
-
- sql-template/no-unsafe-query: 2
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
To lint your project with ESLint, add the following `script` to your `package.json`:
|
|
22
|
+
Add `sql-template` to both the `plugins` and `rules` sections of your `ESLint` configuration file. Example:
|
|
32
23
|
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
24
|
+
```js
|
|
25
|
+
// eslint.config.js
|
|
26
|
+
import sqlTemplate from 'eslint-plugin-sql-template';
|
|
27
|
+
|
|
28
|
+
module.exports = [
|
|
29
|
+
{
|
|
30
|
+
plugins: {
|
|
31
|
+
'sql-template': sqlTemplate
|
|
32
|
+
},
|
|
33
|
+
rules: {
|
|
34
|
+
'sql-template/no-unsafe-query': 'error'
|
|
35
|
+
}
|
|
37
36
|
}
|
|
38
|
-
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
and run the linter with:
|
|
42
|
-
|
|
43
|
-
```sh
|
|
44
|
-
$ npm run lint
|
|
37
|
+
];
|
|
45
38
|
```
|
|
46
39
|
|
|
47
40
|
## Rules
|
|
@@ -81,3 +74,32 @@ Users.query(`SELECT id, name FROM users`);
|
|
|
81
74
|
const punctuation = '!';
|
|
82
75
|
foo.bar(`Not SQL${punctuation}`);
|
|
83
76
|
```
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
[MIT](https://opensource.org/licenses/MIT)
|
|
81
|
+
|
|
82
|
+
## Contributing
|
|
83
|
+
|
|
84
|
+
### Development
|
|
85
|
+
|
|
86
|
+
Install dependencies:
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
npm i
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Run tests:
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
npm run test
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Cutting a release
|
|
99
|
+
|
|
100
|
+
The release process is automated via the [release](https://github.com/uphold/eslint-plugin-sql-template/actions/workflows/release.yaml) GitHub workflow. Run it by clicking the "Run workflow" button.
|
|
101
|
+
|
|
102
|
+
[npm-image]: https://img.shields.io/npm/v/eslint-plugin-sql-template.svg
|
|
103
|
+
[npm-url]: https://www.npmjs.com/package/eslint-plugin-sql-template
|
|
104
|
+
[ci-image]: https://github.com/uphold/eslint-plugin-sql-template/actions/workflows/ci.yaml/badge.svg?branch=master
|
|
105
|
+
[ci-url]: https://github.com/uphold/eslint-plugin-sql-template/actions/workflows/ci.yaml
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-sql-template",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "ESLint plugin with rules for using the `sql` template tag on raw SQL queries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"plugin",
|
|
@@ -21,19 +21,32 @@
|
|
|
21
21
|
"type": "git",
|
|
22
22
|
"url": "git+ssh://git@github.com/uphold/eslint-plugin-sql-template.git"
|
|
23
23
|
},
|
|
24
|
-
"dependencies": {
|
|
25
|
-
"sql-parse": "^0.1.5"
|
|
26
|
-
},
|
|
27
24
|
"engines": {
|
|
28
|
-
"node": ">=
|
|
25
|
+
"node": ">=20"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"index.js",
|
|
29
|
+
"rules"
|
|
30
|
+
],
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
29
33
|
},
|
|
30
34
|
"scripts": {
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
35
|
+
"lint": "eslint .",
|
|
36
|
+
"release": "release-it",
|
|
37
|
+
"test": "mocha test --recursive"
|
|
34
38
|
},
|
|
35
39
|
"devDependencies": {
|
|
36
|
-
"eslint": "^
|
|
37
|
-
"
|
|
40
|
+
"@eslint/js": "^9.12.0",
|
|
41
|
+
"@uphold/github-changelog-generator": "^3.4.0",
|
|
42
|
+
"eslint": "^9.12.0",
|
|
43
|
+
"eslint-config-prettier": "^9.1.0",
|
|
44
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
45
|
+
"mocha": "^10.7.3",
|
|
46
|
+
"prettier": "^3.3.3",
|
|
47
|
+
"release-it": "^17.9.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"eslint": ">=9"
|
|
38
51
|
}
|
|
39
52
|
}
|
package/rules/no-unsafe-query.js
CHANGED
|
@@ -1,60 +1,109 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Helper function to check if an expression contains a variable.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
function containsVariableExpression(expression) {
|
|
8
|
+
if (!expression) return false;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
if (['Identifier', 'CallExpression', 'MemberExpression'].includes(expression.type)) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return false;
|
|
14
|
+
if (expression.type === 'ConditionalExpression') {
|
|
15
|
+
return containsVariableExpression(expression.consequent) || containsVariableExpression(expression.alternate);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} catch (error) {
|
|
21
|
-
return false;
|
|
18
|
+
if (expression.type === 'TemplateLiteral') {
|
|
19
|
+
return expression.expressions.some(containsVariableExpression);
|
|
22
20
|
}
|
|
23
21
|
|
|
24
|
-
return
|
|
22
|
+
return false;
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
/**
|
|
28
|
-
*
|
|
26
|
+
* Helper function to check if a node has a parent that is a template literal.
|
|
29
27
|
*/
|
|
30
28
|
|
|
31
|
-
function
|
|
32
|
-
if (!node)
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
29
|
+
function hasParentTemplateLiteral(node) {
|
|
30
|
+
if (!node?.parent) return false;
|
|
35
31
|
|
|
36
|
-
if (node.type === '
|
|
37
|
-
|
|
32
|
+
if (node.parent.type === 'TemplateLiteral') {
|
|
33
|
+
return true;
|
|
38
34
|
}
|
|
39
35
|
|
|
40
|
-
|
|
41
|
-
const literal = node.quasis.map(quasi => quasi.value.raw).join('x');
|
|
42
|
-
|
|
43
|
-
if (isSqlQuery(literal)) {
|
|
44
|
-
context.report(node, 'Use the `sql` tagged template literal for raw queries');
|
|
45
|
-
}
|
|
46
|
-
}
|
|
36
|
+
return hasParentTemplateLiteral(node.parent);
|
|
47
37
|
}
|
|
48
38
|
|
|
49
39
|
/**
|
|
50
|
-
*
|
|
40
|
+
* SQL starting keywords to detect inside the template literal.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
const sqlKeywords = /^`\s*(SELECT|INSERT\s+INTO|UPDATE|DELETE\s+FROM|WITH|GRANT|BEGIN|DROP)\s/i;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Rule definition.
|
|
51
47
|
*/
|
|
52
48
|
|
|
53
|
-
module.exports =
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
module.exports = {
|
|
50
|
+
meta: {
|
|
51
|
+
type: 'suggestion',
|
|
52
|
+
hasSuggestions: true,
|
|
53
|
+
fixable: 'code',
|
|
54
|
+
docs: {
|
|
55
|
+
description: 'Enforce safe SQL query handling using tagged templates',
|
|
56
|
+
recommended: false,
|
|
57
|
+
url: 'https://github.com/uphold/eslint-plugin-sql-template#rules'
|
|
58
|
+
},
|
|
59
|
+
messages: {
|
|
60
|
+
missingSqlTag: 'Use the `sql` tagged template literal for raw queries'
|
|
61
|
+
},
|
|
62
|
+
schema: []
|
|
56
63
|
},
|
|
57
|
-
|
|
58
|
-
|
|
64
|
+
create(context) {
|
|
65
|
+
return {
|
|
66
|
+
TemplateLiteral(node) {
|
|
67
|
+
// Only check interpolated template literals.
|
|
68
|
+
if (node?.type !== 'TemplateLiteral' || node.expressions.length === 0) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Skip if the template literal has in it's chain a parent that is a TemplateLiteral.
|
|
73
|
+
if (hasParentTemplateLiteral(node)) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Skip if the template literal is already tagged with `sql`.
|
|
78
|
+
if (node.parent.type === 'TaggedTemplateExpression' && node.parent.tag.name === 'sql') {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Check if the template literal has SQL.
|
|
83
|
+
const hasSQL = sqlKeywords.test(context.sourceCode.getText(node));
|
|
84
|
+
|
|
85
|
+
// Recursively check if any expression is a variable (Identifier, MemberExpression, or nested TemplateLiteral)
|
|
86
|
+
const hasVariableExpression = node.expressions.some(containsVariableExpression);
|
|
87
|
+
|
|
88
|
+
if (hasSQL && hasVariableExpression) {
|
|
89
|
+
context.report({
|
|
90
|
+
node,
|
|
91
|
+
messageId: 'missingSqlTag',
|
|
92
|
+
suggest: [
|
|
93
|
+
{
|
|
94
|
+
desc: 'Wrap with sql tag',
|
|
95
|
+
fix(fixer) {
|
|
96
|
+
if (node.parent?.type === 'TaggedTemplateExpression') {
|
|
97
|
+
return fixer.replaceText(node.parent.tag, 'sql');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return fixer.insertTextBefore(node, 'sql');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
59
108
|
}
|
|
60
|
-
}
|
|
109
|
+
};
|
package/.npmignore
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
# Logs
|
|
2
|
-
logs
|
|
3
|
-
*.log
|
|
4
|
-
npm-debug.log*
|
|
5
|
-
|
|
6
|
-
# Runtime data
|
|
7
|
-
pids
|
|
8
|
-
*.pid
|
|
9
|
-
*.seed
|
|
10
|
-
|
|
11
|
-
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
12
|
-
lib-cov
|
|
13
|
-
|
|
14
|
-
# Coverage directory used by tools like istanbul
|
|
15
|
-
coverage
|
|
16
|
-
|
|
17
|
-
# nyc test coverage
|
|
18
|
-
.nyc_output
|
|
19
|
-
|
|
20
|
-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
|
21
|
-
.grunt
|
|
22
|
-
|
|
23
|
-
# node-waf configuration
|
|
24
|
-
.lock-wscript
|
|
25
|
-
|
|
26
|
-
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
|
27
|
-
build/Release
|
|
28
|
-
|
|
29
|
-
# Dependency directories
|
|
30
|
-
node_modules
|
|
31
|
-
jspm_packages
|
|
32
|
-
|
|
33
|
-
# Optional npm cache directory
|
|
34
|
-
.npm
|
|
35
|
-
|
|
36
|
-
# Optional REPL history
|
|
37
|
-
.node_repl_history
|
package/.travis.yml
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [2.0.0](https://github.com/uphold/eslint-plugin-sql-template/tree/2.0.0) (2016-10-03)
|
|
4
|
-
**Merged pull requests:**
|
|
5
|
-
|
|
6
|
-
- Remove lodash dependency [\#4](https://github.com/uphold/eslint-plugin-sql-template/pull/4) ([kurayama](https://github.com/kurayama))
|
|
7
|
-
- Use mocha --recursive flag [\#3](https://github.com/uphold/eslint-plugin-sql-template/pull/3) ([kurayama](https://github.com/kurayama))
|
|
8
|
-
- Update README [\#2](https://github.com/uphold/eslint-plugin-sql-template/pull/2) ([kurayama](https://github.com/kurayama))
|
|
9
|
-
- Create project with `no-unsafe-query` rule [\#1](https://github.com/uphold/eslint-plugin-sql-template/pull/1) ([rplopes](https://github.com/rplopes))
|
|
10
|
-
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Module dependencies.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const RuleTester = require('eslint').RuleTester;
|
|
8
|
-
const rule = require('../../rules/no-unsafe-query');
|
|
9
|
-
|
|
10
|
-
RuleTester.setDefaultConfig({
|
|
11
|
-
parserOptions: {
|
|
12
|
-
ecmaVersion: 6
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Test `no-unsafe-query`.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
const ruleTester = new RuleTester();
|
|
21
|
-
|
|
22
|
-
ruleTester.run('no-unsafe-query', rule, {
|
|
23
|
-
invalid: [{
|
|
24
|
-
code: 'const column = "*"; foo.query(`SELECT ${column} FROM foobar`);',
|
|
25
|
-
errors: [{
|
|
26
|
-
message: 'Use the `sql` tagged template literal for raw queries'
|
|
27
|
-
}]
|
|
28
|
-
}, {
|
|
29
|
-
code: 'const column = "*"; const query = `SELECT ${column} FROM foobar`; foo.query(query);',
|
|
30
|
-
errors: [{
|
|
31
|
-
message: 'Use the `sql` tagged template literal for raw queries'
|
|
32
|
-
}]
|
|
33
|
-
}, {
|
|
34
|
-
code: 'const column = "*"; foo.query(foobar`SELECT ${column} FROM foobar`);',
|
|
35
|
-
errors: [{
|
|
36
|
-
message: 'Use the `sql` tagged template literal for raw queries'
|
|
37
|
-
}]
|
|
38
|
-
}, {
|
|
39
|
-
code: 'const column = "*"; const query = foobar`SELECT ${column} FROM foobar`; foo.query(query);',
|
|
40
|
-
errors: [{
|
|
41
|
-
message: 'Use the `sql` tagged template literal for raw queries'
|
|
42
|
-
}]
|
|
43
|
-
}],
|
|
44
|
-
valid: [
|
|
45
|
-
'const column = "*"; foo.query(sql`SELECT ${column} FROM foobar`);',
|
|
46
|
-
'const column = "*"; const query = sql`SELECT ${column} FROM foobar`; foo.query(query);',
|
|
47
|
-
'foo.query(`SELECT column FROM foobar`);',
|
|
48
|
-
'const query = `SELECT column FROM foobar`; foo.query(query);',
|
|
49
|
-
'const foo = "bar"; baz.greet(`hello ${foo}`);',
|
|
50
|
-
'const foo = "bar"; const baz = `hello ${foo}`; qux.greet(baz);',
|
|
51
|
-
'foo.greet(`hello`);',
|
|
52
|
-
'const foo = `bar`; baz.greet(foo);'
|
|
53
|
-
]
|
|
54
|
-
});
|