|
1 |
| -## Step 3: Fix Security Vulnerabilities |
| 1 | +## Step 3: Review and Triage CodeQL Alerts |
2 | 2 |
|
3 |
| -Let's fix the existing security vulnerabilities already identified by CodeQL. Remember, at this point, we have introduced CodeQL into our repository and it has scanned the existing code. The vulnerabilities it found are real-world issues, and they need to be fixed! |
| 3 | +With our pull request changes now reviewed by CodeQL, let's take a moment to learn about managing alerts. |
4 | 4 |
|
5 |
| -Now that both of these alerts are open, let's fix them. If you look at the alerts, they both call out one specific file containing the issues: `server/routes.py`. The issue is in crafting the SQL query for the database. These queries are vulnerable to SQL injection attacks. We should rewrite these SQL statements more securely. |
| 5 | +GitHub provides a dedicated **Security** tab for securely managing all security related issues. CodeQL saves alerts using the same standard as many other analysis tools with the results showing up under the **Code scanning** area. |
6 | 6 |
|
7 |
| -If you expand the **More info** section at the bottom of the alert, there are very clear suggestions to fix this query. We're going to implement those suggestions in the next activity. |
| 7 | +<img width="500" alt="image" src="https://github.com/user-attachments/assets/cf4fc6ec-e40e-4df6-8984-b6ec35341737" /> |
8 | 8 |
|
9 |
| -### ⌨️ Activity: Resolve an open alert |
| 9 | +### What information do alerts provide? |
10 | 10 |
|
11 |
| -1. In the top navigation, select the **Security** tab. |
| 11 | +The main area of an alert provides the resolution status, affected branch, code location, and classification information like severity and [CVE identification number](https://docs.github.com/en/code-security/security-advisories/working-with-repository-security-advisories/about-repository-security-advisories#cve-identification-numbers). |
| 12 | + |
| 13 | + |
| 14 | +<img width="500" alt="additional information" src="https://github.com/user-attachments/assets/9a5aaf3f-e063-4d07-8cdd-6272eec8a411"/> |
| 15 | + |
| 16 | +<!-- > 💡 Tip: Clicking the **Show paths** link will provide additional insights about the alert's data flow from user input (source), through the application, and when it is acted on (sink). --> |
| 17 | + |
| 18 | +### What is 'CWE' |
12 | 19 |
|
13 |
| -1. In the left navigation, find the **Vulnerability alerts** area and select the **Code scanning** option. You should see two open alerts. |
| 20 | +Many of the patterns CodeQL scans for come from existing databases of vulnerabilities. |
14 | 21 |
|
15 |
| - > 🪧 Note: If any of the alerts are `Closed`, go to the alert's page and choose **Reopen alert**. |
| 22 | +The Common Weakness Enumeration (CWE) is a category system for hardware and software weaknesses and vulnerabilities. Think of it as a way to describe and categorize security issues in an application's source code. For more information on CWEs, see the Wikipedia article [Common Weakness Enumeration](https://en.wikipedia.org/wiki/Common_Weakness_Enumeration). |
16 | 23 |
|
17 |
| -1. Review the 2 open alerts and review the recommendations to decide changes to make. |
| 24 | +### ⌨️ Activity: View the status of a CodeQL scan |
18 | 25 |
|
19 |
| -1. In the top navigation, select the **Code** tab. |
| 26 | +1. In the top navigation, select the **Actions** tab. |
20 | 27 |
|
21 |
| -1. Navigate to the `server` folder and select the `routes.py` file. |
| 28 | +1. If needed, wait a moment for the CodeQL run to finish (about 4 minutes). |
22 | 29 |
|
23 |
| -1. In the top right of the preview, click the **Edit** button. |
| 30 | +1. Click on the **CodeQL Setup** workflow run entry to open a page showing more details. |
| 31 | + |
| 32 | + <img width="500" alt="codeql setup" src="https://github.com/user-attachments/assets/016a729e-3b41-466c-8edf-3d4b41a86b7d"/> |
| 33 | + |
| 34 | + > 💡 Tip: The workflow run contains additional CodeQL information such as the run duration, logs, and analysis artifacts. |
| 35 | +
|
| 36 | +### ⌨️ Activity: Review an Alert |
| 37 | + |
| 38 | +1. In the top navigation, select the **Security** tab. |
24 | 39 |
|
25 |
| - <img width="400" alt="edit button" src="https://github.com/user-attachments/assets/19462cc5-a360-4dae-a97b-ecfd571aa403"/> |
| 40 | +1. In the left navigation, find the **Vulnerability alerts** area and select the **Code scanning** option. |
26 | 41 |
|
27 |
| -1. Navigate to about **line 16** and modify it to the below. |
| 42 | +1. (Optional) Use the filters and search bar to explore the open and closed security alerts, including from the CodeQL scan. |
28 | 43 |
|
29 |
| - ```py |
30 |
| - "SELECT * FROM books WHERE name LIKE %s", name |
31 |
| - ``` |
| 44 | +1. Click on an alert. |
32 | 45 |
|
33 |
| -1. Above the editor in the top-right, click the **Commit changes...** button. Use the defaults options to commit the changes to `main`. |
| 46 | +1. Notice the description, related vulnerability information and a recommended solution. |
34 | 47 |
|
35 |
| - - CodeQL will now initiate a another scan. |
| 48 | + <img width="500" alt="recommendations" src="https://github.com/user-attachments/assets/a5653b45-b66f-4e5b-8e03-a7b8cd3b91b4"/> |
36 | 49 |
|
37 |
| -1. In the top navigation, navigate to the **Actions** tab. Wait for the **CodeQL** workflow to finish. |
| 50 | +1. (Optional) Click the **View source** link to view the CodeQL query that detected the alert. |
38 | 51 |
|
39 |
| -1. Return the the open alerts page and review the open alerts. |
| 52 | +1. (Optional) Click the **Show more** link to view the full recommendation. |
40 | 53 |
|
41 |
| - - There should be zero open alerts and two closed alerts. Nice work! 🎉 |
42 |
| - - Feel free to review the closed alerts, especially the audit trail. |
| 54 | +1. Inspect the audit trail to see a secure history of the alert, including open/close information. |
43 | 55 |
|
44 |
| -<!-- 1. With the CodeQL job finished, Mona will check your progress and share the next steps. --> |
| 56 | + <img width="500" alt="audit trail" src="https://github.com/user-attachments/assets/25ec5256-20c7-4e9d-8160-ff40f3763872"/> |
45 | 57 |
|
46 |
| -1. With the pull request started, Mona will check your progress and share a final review. Nice work! You are done! 🥳 |
| 58 | +### ⌨️ Activity: Dismiss and Reopen an Alert |
47 | 59 |
|
| 60 | +1. In the top right, click **Dismiss alert** dropdown. |
48 | 61 |
|
| 62 | +1. Select any reason and add a short explanation then click the **Dismiss alert** button. |
49 | 63 |
|
| 64 | + - The alert state will change to `Dismissed`. |
| 65 | + - An entry is added to the audit trail, which can't be removed or edited. |
50 | 66 |
|
51 |
| -<!-- 1. Navigate back to **Security** tab and **Code scanning alerts** area. |
| 67 | +1. Reopen the alert. |
52 | 68 |
|
53 |
| -1. Click the **1 Closed** text to switch to a view showing closed alerts. |
| 69 | + - The alert state will change to `Open`. |
| 70 | + - An entry is added to the audit trail, which can't be removed or edited. |
54 | 71 |
|
55 |
| - <img width="500" alt="one closed alert" src="https://github.com/user-attachments/assets/b10005b6-9ef8-4d46-a160-4c9849d2c898"/> --> |
|
0 commit comments