|
| 1 | +# Checkpoint 14 — Static Application Security Testing (SAST) with Semgrep |
| 2 | + |
| 3 | +## Objective |
| 4 | + |
| 5 | +Understand how Static Application Security Testing (SAST) tools analyze source code to detect security vulnerabilities without executing the application. |
| 6 | + |
| 7 | +This checkpoint demonstrates how automated security scanners identify insecure coding practices before code reaches production. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +# What We Built |
| 12 | + |
| 13 | +We created a small Node.js application containing intentionally insecure code and scanned it using Semgrep. |
| 14 | + |
| 15 | +The objective was to understand how SAST tools inspect source code and report potential vulnerabilities. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +# Project Structure |
| 20 | + |
| 21 | +```text |
| 22 | +labs/security/sast-lab/ |
| 23 | +
|
| 24 | +├── app.js |
| 25 | +├── package.json |
| 26 | +├── README.md |
| 27 | +└── .gitignore |
| 28 | +``` |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +# Vulnerable Application |
| 33 | + |
| 34 | +```javascript |
| 35 | +const password = 'admin123'; |
| 36 | + |
| 37 | +function login(username) { |
| 38 | + eval(`console.log("Welcome ${username}")`); |
| 39 | +} |
| 40 | + |
| 41 | +console.log(password); |
| 42 | + |
| 43 | +login('WenDev'); |
| 44 | +``` |
| 45 | + |
| 46 | +The code intentionally contains insecure patterns to demonstrate how SAST tools work. |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +# Tools Used |
| 51 | + |
| 52 | +- Node.js |
| 53 | +- JavaScript |
| 54 | +- Semgrep |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +# Installation |
| 59 | + |
| 60 | +Install Semgrep: |
| 61 | + |
| 62 | +```bash |
| 63 | +python3 -m pip install --user semgrep |
| 64 | +``` |
| 65 | + |
| 66 | +Verify the installation: |
| 67 | + |
| 68 | +```bash |
| 69 | +semgrep --version |
| 70 | +``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +# Running the Scan |
| 75 | + |
| 76 | +Move into the project directory: |
| 77 | + |
| 78 | +```bash |
| 79 | +cd labs/security/sast-lab |
| 80 | +``` |
| 81 | + |
| 82 | +Run Semgrep: |
| 83 | + |
| 84 | +```bash |
| 85 | +semgrep --config=auto . |
| 86 | +``` |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +# Scan Results |
| 91 | + |
| 92 | +Semgrep detected the following vulnerability: |
| 93 | + |
| 94 | +```text |
| 95 | +javascript.browser.security.eval-detected.eval-detected |
| 96 | +
|
| 97 | +Detected the use of eval(). |
| 98 | +
|
| 99 | +eval() can be dangerous if used to evaluate dynamic content. |
| 100 | +If the content comes from external input, it may lead to code injection vulnerabilities. |
| 101 | +``` |
| 102 | + |
| 103 | +Affected code: |
| 104 | + |
| 105 | +```javascript |
| 106 | +eval(`console.log("Welcome ${username}")`); |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +# Why `eval()` Is Dangerous |
| 112 | + |
| 113 | +The `eval()` function executes JavaScript code stored inside a string. |
| 114 | + |
| 115 | +Example: |
| 116 | + |
| 117 | +```javascript |
| 118 | +eval("console.log('Hello')"); |
| 119 | +``` |
| 120 | + |
| 121 | +If attackers can control the input passed into `eval()`, they may execute arbitrary code. |
| 122 | + |
| 123 | +Potential risks: |
| 124 | + |
| 125 | +- Code Injection |
| 126 | +- Remote Code Execution |
| 127 | +- Data leaks |
| 128 | +- Application compromise |
| 129 | + |
| 130 | +Because of these risks, `eval()` should generally be avoided in production applications. |
| 131 | + |
| 132 | +--- |
| 133 | + |
| 134 | +# What Is Static Application Security Testing (SAST)? |
| 135 | + |
| 136 | +SAST analyzes source code without running the application. |
| 137 | + |
| 138 | +Instead of executing the program, the scanner reviews the code and searches for dangerous patterns. |
| 139 | + |
| 140 | +Workflow: |
| 141 | + |
| 142 | +```text |
| 143 | +Developer writes code |
| 144 | + ↓ |
| 145 | +SAST scans source files |
| 146 | + ↓ |
| 147 | +Potential vulnerabilities are detected |
| 148 | + ↓ |
| 149 | +Developers fix issues before deployment |
| 150 | +``` |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +# Security Layers Learned So Far |
| 155 | + |
| 156 | +| Security Layer | Tool | |
| 157 | +| -------------------- | --------------------- | |
| 158 | +| Secrets Management | `.env` + `.gitignore` | |
| 159 | +| Dependency Scanning | `npm audit` | |
| 160 | +| Static Code Analysis | Semgrep | |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +# Key Learnings |
| 165 | + |
| 166 | +- SAST analyzes code statically without execution. |
| 167 | +- Security issues can be detected before deployment. |
| 168 | +- Dangerous functions such as `eval()` can introduce serious vulnerabilities. |
| 169 | +- Security should be integrated into the development pipeline. |
| 170 | +- Automated scanners help developers identify insecure code early. |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +# Commands Learned |
| 175 | + |
| 176 | +```bash |
| 177 | +semgrep --version |
| 178 | + |
| 179 | +semgrep --config=auto . |
| 180 | +``` |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +# Outcome |
| 185 | + |
| 186 | +Successfully used Semgrep to perform Static Application Security Testing (SAST) and detect insecure coding patterns inside a Node.js application. |
| 187 | + |
| 188 | +This checkpoint demonstrates how modern DevSecOps pipelines automatically scan source code for vulnerabilities before applications are built and deployed. |
0 commit comments