Skip to content

Commit 60c4314

Browse files
committed
lab 3 updates
1 parent 8388658 commit 60c4314

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

_labs/lab3.md

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ With Code Scanning enabled, we want to block vulnerable code from entering the c
44

55
This lab covers parts of the following exam domains:
66

7-
Domain 4: Configure and use code scanning
8-
Domain 5: Use code scanning with CodeQL
9-
Domain 6: Describe GitHub Advanced Security best practices
7+
- Domain 4: Configure and use code scanning
8+
- Domain 5: Use code scanning with CodeQL
9+
- Domain 6: Describe GitHub Advanced Security best practices
1010

1111
## Exercise 1: Introduce a code scanning security vulnerability
1212

@@ -17,29 +17,29 @@ Domain 6: Describe GitHub Advanced Security best practices
1717
</details>
1818

1919
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).
2222
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.
2323
6. Open the **routes/login.ts** file.
2424
7. Find lines 36-46 and delete them
2525

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',
29+
- {
30+
- replacements: {
31+
- email: req.body.email || '',
32+
- password: security.hash(req.body.password || '')
33+
- },
34+
- model: UserModel,
35+
- plain: true
36+
- }
37+
- )
3838
```
3939

4040
8. At line 36, add the following code:
4141

42-
```
42+
```javascript
4343
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 })
4444
```
4545

@@ -50,25 +50,27 @@ models.sequelize.query(`SELECT * FROM Users WHERE email = '${req.body.email || '
5050
</details>
5151

5252
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
5555
13. Click the **+** button next to **login.ts** to stage the changes
5656
14. Add a commit message and click **Commit**.
5757
15. Click **Publish Branch** to push your new branch with the code changes to GitHub.
5858
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`.
6062
- 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.
6264
- 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.
6466

6567
<details>
6668
<img src="images/lab-3-1-3.png"/>
6769
</details>
6870

6971
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.
7072
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.** ⚠️⚠️
7274

7375
<details>
7476
<img src="images/lab-3-1-4.png"/>
@@ -79,7 +81,7 @@ models.sequelize.query(`SELECT * FROM Users WHERE email = '${req.body.email || '
7981
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!
8082

8183
> [!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!
8385
8486
1. Let's go into the **Settings** tab of the repository (we will be adding a branch ruleset).
8587
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
9193
3. Click on **New ruleset ▾ --> New branch ruleset**
9294
4. Create the ruleset:
9395
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**.
9597
3. Under **target branches**, click **Add target** and select **Include default branch**.
9698
4. Scroll down and check the **Require status checks to pass** box
9799
5. Click on the **+ Add checks ▾** button
@@ -101,24 +103,30 @@ Without a ruleset (GitHub's new version of branch protections), even though Code
101103
<img src="images/lab-3-2-2.png"/>
102104
</details>
103105

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.
106108

107109
<details>
108110
<img src="images/lab-3-2-3.png"/>
109111
</details>
110112

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.
113115

114116
<details>
115117
<img src="images/lab-3-2-4.png"/>
116118
</details>
117119

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!
119125

120126
## Summary
121127

122128
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.
123129

124130
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

Comments
 (0)