Skip to content

Commit cfad0f1

Browse files
sabrowning1jc-clarklecoursen
authored
Add learner walkthrough for code scanning (#55192)
Co-authored-by: Joe Clark <31087804+jc-clark@users.noreply.github.com> Co-authored-by: Laura Coursen <lecoursen@github.com>
1 parent 08c1dcf commit cfad0f1

File tree

6 files changed

+84
-1
lines changed

6 files changed

+84
-1
lines changed
Loading
Loading
Loading
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: Finding and fixing your first code vulnerability
3+
shortTitle: Secure your code
4+
intro: 'Learn the basics of securing your code by resolving a {% data variables.product.prodname_code_scanning %} alert in a demo repository.'
5+
versions:
6+
fpt: '*'
7+
topics:
8+
- Code Security
9+
- Code scanning
10+
- Alerts
11+
---
12+
13+
As you're learning to code, it's normal to accidentally introduce vulnerabilities into your projects. If these security issues aren't addressed before you share your work, attackers can use them to manipulate your code and access sensitive data, so it's important to identify and patch them as soon as possible.
14+
15+
Thankfully, there are tools like {% data variables.product.prodname_code_scanning %} that automatically find, explain, and even fix vulnerabilities in your public repositories. In this tutorial, you'll learn how to keep your work secure with {% data variables.product.prodname_code_scanning %}.
16+
17+
## Setting up the demo repository
18+
19+
Let's get started by forking a demo project with a security vulnerability. This project builds a simple webpage, but since we won't deploy the project, there is **no security risk** in this exercise.
20+
21+
1. Navigate to the [`new2code/code-scanning-demo`](https://github.com/new2code/code-scanning-demo) repository.
22+
1. In the top right of the page, click {% octicon "repo-forked" aria-hidden="true" %} **Fork**.
23+
1. In the page that appears, click **Create fork**.
24+
25+
## Finding vulnerabilities in your code
26+
27+
Now that we've set up the project, let's turn on {% data variables.product.prodname_code_scanning %} for your fork to check for vulnerabilities in the code.
28+
29+
1. Back on {% data variables.product.github %}, on the home page of your repository, click {% octicon "shield" aria-hidden="true" %} **Security**.
30+
1. In the "{% data variables.product.prodname_code_scanning_caps %} alerts" row, click **Set up {% data variables.product.prodname_code_scanning %}**.
31+
1. In the "Tools" section, next to "{% data variables.product.prodname_codeql %} analysis", select **Set up**, then click **Default**.
32+
1. In the pop up that appears, click **Enable {% data variables.product.prodname_codeql %}**. This will trigger a {% data variables.product.prodname_actions %} workflow that scans your code for vulnerabilities.
33+
1. To check the status of your workflow, in the navigation bar, click {% octicon "play" aria-hidden="true" %} **Actions**.
34+
1. Once the workflow is complete, in the navigation bar, click {% octicon "shield" aria-hidden="true" %} **Security**.
35+
1. To view the {% data variables.product.prodname_code_scanning %} alert discovered by the workflow, in the side navigation, click {% octicon "codescan" aria-hidden="true" %} **{% data variables.product.prodname_code_scanning_caps %}**, then click **Reflected cross-site scripting**.
36+
37+
## Understanding a {% data variables.product.prodname_code_scanning %} alert
38+
39+
Now that {% data variables.product.prodname_code_scanning %} has identified a vulnerability in the code, let's break down the information provided in the alert.
40+
41+
### Location
42+
43+
The alert shows a small preview of a file, centered on the lines of code creating the vulnerability. In our case, the vulnerability is detected on line 8 of our `index.js` file, where we implement the user-provided input in our site.
44+
45+
If we take a closer look, we can see that the underlying issue occurs on line 7, when we assign `greet` to user input without checking for malicious code. To see a step-by-step view of the vulnerability in our code, in the callout below the vulnerable lines, click **Show paths**.
46+
47+
![Screenshot of the location of a {% data variables.product.prodname_code_scanning %} alert. A "Show paths" button is outlined in orange.](/assets/images/help/repository/code-scanning-alert-location-learners.png)
48+
49+
### Description and recommendation
50+
51+
Below the file preview, {% data variables.product.prodname_code_scanning %} provides a more detailed description of the vulnerability. To see the recommended fix, as well as examples of the vulnerable and fixed code, click **Show more** {% octicon "chevron-down" aria-hidden="true" %}.
52+
53+
![Screenshot of the details of a {% data variables.product.prodname_code_scanning %} alert. A dropdown labeled "Show more" is outlined in orange.](/assets/images/help/repository/code-scanning-alert-details-learners.png)
54+
55+
In our case, the recommendation is to sanitize user input before using it. This means we need to check the input for malicious code, then clean it up if necessary.
56+
57+
> [!TIP] If you don't fully understand the recommended fix, try [asking {% data variables.product.prodname_copilot_chat_short %}](https://github.com/copilot) to explain it.
58+
59+
### Timeline
60+
61+
Finally, you can see the timeline of the alert at the bottom of the page. Our timeline contains the commit where the vulnerability was first detected, and will be updated automatically when we fix the vulnerability.
62+
63+
![Screenshot of the timeline for a {% data variables.product.prodname_code_scanning %} alert.](/assets/images/help/repository/code-scanning-alert-timeline-learners.png)
64+
65+
## Fixing a vulnerability automatically
66+
67+
To secure our project quickly and easily, let's use {% data variables.product.prodname_copilot_autofix %} for {% data variables.product.prodname_code_scanning %}.
68+
69+
1. Below the alert title, in the box suggesting you "Speed up the remediation of this alert using {% data variables.product.prodname_copilot_autofix_short %} for {% data variables.product.prodname_codeql %}", click {% octicon "shield-check" aria-hidden="true" %} **Generate fix**.
70+
1. After {% data variables.product.prodname_copilot_short %} generates the fix suggestion, it will describe the changes it is suggesting, render a preview of the changes, and call out any added dependencies. Take a moment to read through {% data variables.product.prodname_copilot_short %}'s work.
71+
1. To create a pull request with the fix, click **Commit to new branch**, then click **Commit change**.
72+
1. Once the draft pull request is created, at the bottom of the page, click **Ready for review** to make it mergeable.
73+
1. To apply the fix, click **Merge pull request**, then click **Confirm merge**.
74+
75+
Once the pull request is merged and the {% data variables.product.prodname_code_scanning %} workflow runs again, the alert will be closed automatically, and the commit fixing the vulnerability will be added to the timeline.
76+
77+
> [!NOTE] In real projects, you should always review the changes suggested by {% data variables.product.prodname_copilot_short %} before committing them to your code.
78+
79+
## Next steps
80+
81+
Now that you've tried out {% data variables.product.prodname_code_scanning %} on a demo repository, **enable it on your own projects** to quickly find and fix current and future vulnerabilities.

content/get-started/learning-to-code/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ children:
1010
- /setting-up-copilot-for-learning-to-code
1111
- /learning-to-debug-with-github-copilot
1212
- /storing-your-secrets-safely
13+
- /finding-and-fixing-your-first-code-vulnerability
1314
shortTitle: Learn to code
1415
---
15-

content/get-started/learning-to-code/storing-your-secrets-safely.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,5 @@ Once the workflow has had time to complete, you should see a new comment appear.
166166
## Next steps
167167

168168
For a more in-depth dive into secret scanning and push protection, you can complete the [Introduction to secret scanning](https://github.com/skills/introduction-to-secret-scanning/tree/main) course in {% data variables.product.prodname_learning %}.
169+
170+
Another important part of code security is learning how to identify and patch code vulnerabilities in your projects. See [AUTOTITLE](/get-started/learning-to-code/finding-and-fixing-your-first-code-vulnerability).

0 commit comments

Comments
 (0)