/
avoid-injections.test.js
88 lines (82 loc) · 2.51 KB
/
avoid-injections.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const { RuleTester } = require("eslint");
const rule = require("./avoid-injections");
function invalidCase(code, errors = [], others = {}) {
return Object.assign(
{
code,
errors,
},
others,
);
}
const tester = new RuleTester({
parserOptions: { ecmaVersion: 2015 },
});
tester.run("avoid-injections", rule, {
valid: [
"knex.raw('select ? from users', ['email'])",
"knex.raw(`select * from users`)",
"knex.raw('select ? from users', ['email'])",
`const query = 'SELECT * FROM users'; const result = knex.raw(query);`,
`
const query = \`now() + interval '123 seconds'\`;
function run() {
return knex.raw(query);
}
`,
"knex('users').whereRaw('id = ?', [1]);",
"knex('users').whereRaw(`id = 1`);",
"const joinCondition = `blog_posts ON users.id = blog_posts.author`; knex('users').select(['email']).joinRaw(joinCondition)",
`function sharp() { return { raw: () => {}, }; } sharp().raw();`,
{
code: "knex('users').whereRaw(`id = ` + id);",
settings: {
knex: {
builderName: /(transaction|trx)/i,
},
},
},
],
invalid: [
// .raw()
invalidCase("knex.raw(`select * from ${table}`);", [
{ messageId: "avoid", data: { query: "raw" } },
]),
invalidCase('knex.raw("select * from " + table);', [
{ messageId: "avoid", data: { query: "raw" } },
]),
invalidCase(
"const email = 'user@domain.com'; const query = `SELECT * FROM users WHERE email='${email}'`; function run() { knex.raw(query); }",
[{ messageId: "avoid", data: { query: "raw" } }],
),
// .whereRaw()
invalidCase("knex('users').whereRaw(`id = ${id}`);", [
{ messageId: "avoid", data: { query: "whereRaw" } },
]),
invalidCase("knex('users').whereRaw(`id = ` + id);", [
{ messageId: "avoid", data: { query: "whereRaw" } },
]),
invalidCase("knex('users').whereRaw(`id = ${getId()}`);", [
{ messageId: "avoid", data: { query: "whereRaw" } },
]),
invalidCase("knex('users').whereRaw(`id = ${getId()}`);", [
{ messageId: "avoid" },
]),
// .joinRaw()
invalidCase(
"knex('users').select(['email']).joinRaw(`blog_posts ON users.id = ${userId}`)",
[{ messageId: "avoid", data: { query: "joinRaw" } }],
),
invalidCase(
"lorem.raw(`select * from ${table}`);",
[{ messageId: "avoid", data: { query: "raw" } }],
{
settings: {
knex: {
builderName: /lorem/i,
},
},
},
),
],
});