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
2. Give it a few moments to load the repository. Codespaces allows you full access to a cloud compute environment to develop and debug your code. It's a great way to get started with a project quickly and to contribute to open source projects.
20
-
3. Now Josh has given you a new piece of code to add to the **routes/login.ts** file.
21
-
4.We need to create a new branch. Click **main** in the taskbar at the bottom of VSCode.
20
+
3. Now, Josh has given you a new piece of code to add to the `routes/login.ts` file.
21
+
4.Before making the code change, let's create a new branch. Click **main** in the taskbar at the bottom of VSCode (the **main** that appears just to the right of your Codespaces name).
22
22
5. Select **Create new branch**, enter **lab3/code-scanning-vulnerability**, and hit Enter. The branch will be created and VSCode will switch to the branch.
23
23
6. Open the **routes/login.ts** file.
24
24
7. Find lines 36-46 and delete them
25
25
26
-
```
27
-
models.sequelize.query(
28
-
'SELECT * FROM Users WHERE email = :email AND password = :password AND deletedAt IS NULL',
29
-
{
30
-
replacements: {
31
-
email: req.body.email || '',
32
-
password: security.hash(req.body.password || '')
33
-
},
34
-
model: UserModel,
35
-
plain: true
36
-
}
37
-
)
26
+
```diff
27
+
-models.sequelize.query(
28
+
- 'SELECT * FROM Users WHERE email = :email AND password = :password AND deletedAt IS NULL',
models.sequelize.query(`SELECT * FROM Users WHERE email = '${req.body.email||''}' AND password = '${security.hash(req.body.password||'')}' AND deletedAt IS NULL`, { model: UserModel, plain:true })
44
44
```
45
45
@@ -50,25 +50,27 @@ models.sequelize.query(`SELECT * FROM Users WHERE email = '${req.body.email || '
50
50
</details>
51
51
52
52
10. Copilot chat should open up and explain what this line is doing. And oh no, read it thoroughly - it tells us we have a vulnerability! 😱
53
-
11. We can ask Copilot chat how we could fix it. Better yet, do this: right click on line 36 and select **Copilot --> Fix**. ❗️❗️ We don't want to save anything though, so just review the fix for now. Don't accept this change, discard it.❗️❗️
54
-
12. Let's push our new branch and changes up to GitHub. Select the **Source Control** extension on the left side of Visual Studio Code
53
+
11. We can ask Copilot chat how we could fix it. Better yet, do this: right click on line 36 and select **Copilot --> Fix**. ❗️❗️ We don't want to save anything though, so just review the fix for now. Don't accept this change, click on the **Discard** button.❗️❗️
54
+
12. Let's push our new branch with the vulnerability up to GitHub. Select the **Source Control** extension on the left side of Visual Studio Code
55
55
13. Click the **+** button next to **login.ts** to stage the changes
56
56
14. Add a commit message and click **Commit**.
57
57
15. Click **Publish Branch** to push your new branch with the code changes to GitHub.
58
58
16. Let's create a pull request for this branch to attempt to merge it into main.
59
-
- In another browser tab, navigate back to the repository --> **Pull requests** tab --> **New pull request** button --> select the `lab3/code-scanning-vulnerability` to merge into `main`.
59
+
- In another browser tab, navigate back to the repository.
60
+
- Alternatively navigate back to the [workshop organization](https://github.com/ghuwsec1953) and find your repo.
61
+
- Click the **Pull requests** tab --> **New pull request** button --> select the `lab3/code-scanning-vulnerability` to merge into `main`.
60
62
- Click **Create pull request**
61
-
- In the pull request description, click the Copilot icon on the bar and have Copilot generate a pull request summary for you.
63
+
- In the pull request description, click the Copilot icon on the formatting bar and have Copilot generate a pull request **summary** for you.
62
64
- Click **Create pull request**
63
-
9. After the pull request is created, the code scanning job will have been initiated. You can see the status of the job in the pull request checks. It will take a few minutes to run.
65
+
17. After the pull request is created, the code scanning job will have been initiated. You can see the status of the job in the pull request checks. It will take a few minutes to run.
64
66
65
67
<details>
66
68
<imgsrc="images/lab-3-1-3.png"/>
67
69
</details>
68
70
69
71
10. CodeQL should find the vulnerability, so the check will fail. Also, we should see Copilot create us an autofix on the PR that we can review.
70
72
11. It might take Copilot a few moments to create the autofix.
71
-
12. Review the autofix - we can prevent a vulnerability from entering the repository now with a click of a button! 🎉**But don't commit the suggestion yet.**
73
+
12. Review the autofix - we can prevent a vulnerability from entering the repository now with a click of a button! 🎉 ⚠️⚠️ **But don't commit the suggestion yet.** ⚠️⚠️
72
74
73
75
<details>
74
76
<imgsrc="images/lab-3-1-4.png"/>
@@ -79,7 +81,7 @@ models.sequelize.query(`SELECT * FROM Users WHERE email = '${req.body.email || '
79
81
Without a ruleset (GitHub's new version of branch protections), even though CodeQL found the vulnerability, a developer could still merge the code mistakenly, or merge the code before the CodeQL checks finish. Let's prevent this!
80
82
81
83
> [!NOTE]
82
-
> We have to wait for the PR check to finish entirely (with a pass or fail) in order to create the ruleset properly!
84
+
> We want to wait for the PR check to finish entirely (with a pass or fail) before creating the ruleset!
83
85
84
86
1. Let's go into the **Settings** tab of the repository (we will be adding a branch ruleset).
85
87
2. On the left hand list of options, click on **Rules --> Rulesets**.
@@ -91,7 +93,7 @@ Without a ruleset (GitHub's new version of branch protections), even though Code
91
93
3. Click on **New ruleset ▾ --> New branch ruleset**
92
94
4. Create the ruleset:
93
95
1. Give the ruleset a **name** (any name is fine)
94
-
2. Change the enforcement status to **Active**.
96
+
2. Change the **enforcement status** to **Active**.
95
97
3. Under **target branches**, click **Add target** and select **Include default branch**.
96
98
4. Scroll down and check the **Require status checks to pass** box
97
99
5. Click on the **+ Add checks ▾** button
@@ -101,24 +103,30 @@ Without a ruleset (GitHub's new version of branch protections), even though Code
101
103
<imgsrc="images/lab-3-2-2.png"/>
102
104
</details>
103
105
104
-
7. Let's also search for **Analyze**. We should see a **Analyze (javascript-typescript)** check show up. Add it.
105
-
8. Scroll down and click the **Create** button.
106
+
5. Let's also search for **Analyze**. We should see a **Analyze (javascript-typescript)** check show up. Add it.
107
+
6. Scroll down and click the **Create** button.
106
108
107
109
<details>
108
110
<imgsrc="images/lab-3-2-3.png"/>
109
111
</details>
110
112
111
-
5. With the ruleset created, both the JavaScript scan has to finish and no vulnerabilities found with CodeQL in order to merge the code.
112
-
6. Navigate back to our open PR. The **Merge pull request** button should now be grayed out, preventing us from merging vulnerable code.
113
+
7. With the ruleset created, both the JavaScript scan has to finish and no vulnerabilities found with CodeQL in order to merge the code.
114
+
8. Navigate back to our open PR. The **Merge pull request** button should now be grayed out, preventing us from merging vulnerable code.
113
115
114
116
<details>
115
117
<imgsrc="images/lab-3-2-4.png"/>
116
118
</details>
117
119
118
-
7. Celebrate 🎉! We just prevented a security vulnerability from entering our codebase!
120
+
9. Review the **Copilot Autofix suggestion** - it offers a similar suggestion to what Copilot in our IDE did!
121
+
10. Click on the **commit suggestion** button and **commit changes**.
122
+
11. After another CodeQL scan, the PR should pass and the **Merge pull request** button should be enabled, allowing you to merge the change without the vulnerability.
123
+
- ➡️ For the purposes of this lab, you don't have to actually merge the PR, so **you don't have to wait fo the CodeQL scan to finish before moving on**.
124
+
12. Celebrate 🎉! We just prevented a security vulnerability from entering our codebase!
119
125
120
126
## Summary
121
127
122
128
Excellent! In this lab you learned how to use Codespaces and Copilot to understand your code base, and even find errors in your code. We saw how GitHub code scanning can find bugs in the pull request and suggest solutions for them. And we learned how to use repository rulesets to block a pull request from merging until the checks are resolved.
123
129
124
130
In the next lab, we are going to learn about Dependency Review, and how it can help us stop bad dependencies from making it to the default branch.
131
+
132
+
➡️ Head back to the [labs](README.md) page to continue on to the next lab.
0 commit comments