You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# For math expressions, use a purpose-built parser instead of eval
27
30
```
28
31
32
+
> **Note:** Avoid `eval()`/`exec()` entirely. Even with hardcoded strings, it normalizes a dangerous pattern. Use `ast.literal_eval()` for parsing data literals, or purpose-built parsers for expressions.
33
+
29
34
**Incorrect (JavaScript - eval with dynamic content):**
30
35
31
36
```javascript
@@ -38,15 +43,20 @@ function evalSomething(something) {
38
43
}
39
44
```
40
45
41
-
**Correct (JavaScript - static eval strings):**
46
+
**Correct (JavaScript - avoid eval, use safe alternatives):**
42
47
43
48
```javascript
44
-
eval('var x = "static strings are okay";');
49
+
// Instead of eval for JSON parsing:
50
+
constdata=JSON.parse(jsonString);
51
+
52
+
// Instead of eval for dynamic property access:
53
+
constvalue= obj[propertyName];
45
54
46
-
constconstVar="function staticStrings() { return 'static strings are okay';}";
47
-
eval(constVar);
55
+
// Instead of eval for math: use a sandboxed expression parser
48
56
```
49
57
58
+
> **Note:** There is almost never a legitimate reason to use `eval()`. Use `JSON.parse()`, computed property access, or a sandboxed parser. Avoid `new Function()` as well — it executes arbitrary code just like `eval()`.
// Instead of eval for templates, use a template engine (Twig, Blade)
116
124
```
117
125
126
+
> **Note:**`exec()`/`shell_exec()`/`system()` are OS command execution — see the command-injection rule for those. This rule covers code evaluation via `eval()`, `assert()`, `preg_replace` with `/e`, and similar.
127
+
118
128
## Key Prevention Patterns
119
129
120
-
1.**Never pass user input to eval/exec functions** - Treat all user input as untrusted
121
-
2.**Use hardcoded strings** - Static strings in eval/exec calls are safe
122
-
3.**Validate and sanitize** - If dynamic code execution is unavoidable, validate against a strict whitelist
123
-
4.**Use parameterized alternatives** - Many languages offer safer alternatives to eval
124
-
5.**Escape shell arguments** - Use escapeshellarg() in PHP or equivalent functions
Copy file name to clipboardExpand all lines: skills/code-security/rules/csrf.md
+44-10Lines changed: 44 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,30 +42,64 @@ def my_view(request):
42
42
43
43
#### Missing CSRF Middleware
44
44
45
-
**Incorrect (Express app without csurf middleware):**
45
+
> **⚠ Deprecation Notice:** The `csurf` npm package is **deprecated** and should not be used in new projects. Use a maintained alternative such as `csrf-csrf` (Double-Submit Cookie pattern) or `csrf-sync` (Synchronizer Token pattern).
46
+
47
+
**Incorrect (Express app without CSRF protection):**
> **Note:**`MessageDigest` (SHA-256/SHA-512) is appropriate for data integrity checks but not for password storage. Use BCrypt, scrypt, or Argon2 for passwords.
114
+
107
115
**Incorrect (DES cipher):**
108
116
109
117
```java
110
118
Cipher c =Cipher.getInstance("DES/ECB/PKCS5Padding");
0 commit comments