Skip to content

Commit 1c709dc

Browse files
Initial commit
0 parents  commit 1c709dc

File tree

11 files changed

+734
-0
lines changed

11 files changed

+734
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Step 1: Enable Secret Protection
2+
3+
If you check your email, you probably just got an alert from GitHub with the subject "Possible valid secrets detected". Oh no! 😮
4+
5+
Don't worry! We put some expired credentials in the exercise on purpose since public repositories get secret protection for free. Nice! 🕵️
6+
7+
In this step, you will enable secret protection on your repository. After it is enabled, you will add a new credential to see how secret protection identifies the credential and alerts you.
8+
9+
> [!WARNING]
10+
> If your repository is private, you will need [GitHub Advanced Security](https://docs.github.com/en/enterprise-cloud@latest/get-started/learning-about-github/about-github-advanced-security) to continue. We recommend [changing this exercise repository to public](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/managing-repository-settings/setting-repository-visibility) to enable it.
11+
12+
### What is a secret?
13+
14+
In our context, a secret (or credential) is a plain-text string, or a pair of strings, that authorizes access to a service. Examples could be AWS secret access keys/ID's, Google API keys, or GitHub Personal Access Tokens (PATs).
15+
16+
The GitHub Docs provides a list of [all supported patterns](https://docs.github.com/en/code-security/secret-scanning/secret-scanning-patterns#supported-secrets).
17+
18+
### What is secret protection?
19+
20+
Secret protection is a powerful tool which allows teams to identify these plain-text credentials, remove them, and create rules to prevent them from being written to GitHub in the first place.
21+
22+
Secret protection is available **for free for public repositories** on all plans. Enterprises that need secret protection capabilities for private repositories should review [GitHub Advanced Security](https://github.com/security/advanced-security). Not only does it include secret protection, it also provides advanced static analysis, software composition analysis (SCA), and enterprise tools to manage your entire AppSec pipeline and reduce your risk profile.
23+
24+
### :keyboard: Activity: Configure secret protection
25+
26+
1. In the header of your repository, open **Settings** in a new browser tab.
27+
28+
1. In the left navigation, under the **Security** section, select **Advanced Security**.
29+
30+
1. Scroll down past the **Code Scanning** and **Dependabot** sections until you find the **Secret Protection** section.
31+
32+
> 💡 **Tip:** We also have exercises about [code scanning](https://github.com/skills/introduction-to-codeql) and [supply chain protection](https://github.com/skills/secure-repository-supply-chain)!
33+
34+
1. Adjust the default configuration to match the below.
35+
36+
- **Secret Protection:** `enabled`
37+
- **Push Protection:** `disabled`
38+
39+
<img width="400" alt="Secret protection configuration settings" src="https://github.com/user-attachments/assets/7b999e54-dbf4-400d-8730-17b96bc06de1" />
40+
41+
### :keyboard: Activity: Commit a sensitive file
42+
43+
Now let's (accidentally) commit a sensitive file to see how it works. Don't worry, these are inactive credentials.
44+
45+
1. In the header of your repository, click the **Code** tab.
46+
47+
1. Above the list of files, click the **Add file** dropdown and select **Create new file**.
48+
49+
<img width="350" alt="New file button" src="https://github.com/user-attachments/assets/8f3f8da8-1471-485a-9df5-8c03ecba2d8e"/>
50+
51+
1. Enter the file name `credentials.yml` and copy following **inactive** example credentials into it.
52+
53+
<img width="400" alt="New file button" src="https://github.com/user-attachments/assets/40f5ce62-936c-4d71-8c51-02c724d5aac0"/>
54+
55+
```yaml
56+
default:
57+
aws_access_key_id: AKIAQYLPMN5HNM4OZ56B
58+
aws_secret_access_key: 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
59+
mongodb: mongodb+srv://svc-admin:[email protected]
60+
output: json
61+
region: us-east-2
62+
```
63+
64+
1. In the top right, use the **Commit changes...** button to commit directly to the `main` branch.
65+
66+
> ❗️ **Important:** Committing to your default branch is not usually a recommended practice. We only do this to simplify the exercise.
67+
68+
1. With our credentials file (accidentally) shared, Mona should quickly notice it and prepare the next step.

.github/steps/2-review-alerts.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Step 2: Review and close secret scanning alerts
2+
3+
In the last step, you enabled secret protection and committed a sensitive file to the repository. Now, let's review our open secret scanning alerts and close one.
4+
5+
### :keyboard: Activity: Triage secret scanning alerts
6+
7+
1. In the header of your repository, click the **Security** tab.
8+
9+
1. In the left navigation, select the **Secret scanning** option.
10+
11+
1. Notice various options in the header bar that can help triage our alerts.
12+
13+
<img width="400" alt="list of filtered open alerts" src="https://github.com/user-attachments/assets/2926c12f-2d39-4ea1-a8dd-816442f332b4" />
14+
15+
1. Click the **Provider** dropdown and select `Amazon AWS` to filter the view. Notice only 2 of the 3 entries are listed now.
16+
17+
<img width="400" alt="list of filtered open alerts" src="https://github.com/user-attachments/assets/8623dec7-5199-4c5b-8c5e-1b812106a510" />
18+
19+
### :keyboard: Activity: Review a secret scanning alert
20+
21+
1. In the list of open alerts, select the `Amazon AWS Access Key ID` alert. This will open a details page with more information.
22+
23+
1. At the top of the page, we can quickly view the alert status, when it was opened, the exposed secret, and some remediation steps.
24+
25+
<img width="400" alt="image" src="https://github.com/user-attachments/assets/61700b67-234c-47ae-a4de-552be25cc2bf" />
26+
27+
1. Scroll down slightly to the **Detected in X locations** area and you will see all the places where this secret was detected, including the `credentials.yml` file that you created. Notice that secret protection doesn't create duplicate alerts for the same secret found across multiple locations, for example in our learning issue.
28+
29+
<img width="400" alt="image" src="https://github.com/user-attachments/assets/0a40db10-0461-4732-8e5d-674082020c96" />
30+
31+
### :keyboard: Activity: Close an alert
32+
33+
When secret protection finds a secret in your repository, the first thing you should do is **disable that secret with the provider**. You should assume it has been exposed.
34+
35+
> [!TIP]
36+
> Some [supported secrets](https://docs.github.com/en/code-security/secret-scanning/introduction/supported-secret-scanning-patterns#default-patterns) are automatically sent to the provider when leaked.
37+
38+
1. Assuming you have taken remediation steps, we can update the status of our alert. In the top right, select the **Close as** dropdown.
39+
40+
> 🚨 **Caution:** Do **NOT** close an open alert without performing remediation steps. This simply hides the problem and provides a false sense of security. It might even trigger additional alerts with your cybersecurity department. 🤦
41+
42+
1. Choose the `Revoked` option and enter a useful description of your remediation steps in the comment box (example below). Then choose **Close alert**.
43+
44+
```txt
45+
The secret owner was contacted. They provided proof that the exposed secret was replaced.
46+
```
47+
48+
> 💡 **Tip:** This is important so the audit log can later provide critical information if an investigation is required.
49+
50+
<img width="250" alt="Screenshot of an alert being closed as revoked with a useful comment" src="https://github.com/user-attachments/assets/a65bf6be-2be3-4096-9afa-db7a3fb02ecd" />
51+
52+
53+
1. The alert status now displays `Closed` and the audit trail includes our explanation.
54+
55+
<img width="250" alt="image" src="https://github.com/user-attachments/assets/fdff5ad5-40ab-4f35-9f37-284dfe129ebd" />
56+
57+
<img width="450" alt="image" src="https://github.com/user-attachments/assets/29bde02e-88d8-4fe2-b39c-a85a7b705908" />
58+
59+
1. With at least one of our alerts resolved, let's add a comment to inform Mona we are done with this step, so she can share the next one.
60+
61+
```txt
62+
Hello @professortocat, I've resolved the security alert. What's next?
63+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Step 3: Enable push protection
2+
3+
In this section, you will configure your repository to prevent new secrets from being exposed.
4+
5+
### What is push protection?
6+
7+
When someone tries to send code changes to GitHub (a push), secret scanning checks for high-confidence secrets. Secret scanning lists any secrets it detects so the author can review the secrets and remove them or, if needed, allow those secrets to be pushed.
8+
9+
### :keyboard: Activity: Configure push protection
10+
11+
> [!IMPORTANT]
12+
> We disabled push protection in our first step for learning practice. It is normally enabled by default for all public repositories.
13+
14+
1. In the header of your repository, click the **Settings** tab.
15+
1. In the left navigation, under the **Security** section, select **Advanced Security**.
16+
1. Scroll down past the **Code Scanning** and **Dependabot** sections until you find the **Secret Protection** section.
17+
1. Adjust the default configuration to match the below.
18+
19+
- **Secret Protection:** `enabled`
20+
- **Push Protection:** `enabled`
21+
22+
<img width="400" alt="Secret protection configuration settings" src="https://github.com/user-attachments/assets/4ecbc9a5-f1b2-4b68-8a1e-667dee7a7661" />
23+
24+
### :keyboard: Activity: Attempt to push a secret
25+
26+
Now that secret push protection is enabled, let's give it a test!
27+
28+
1. In the header of your repository, click the **Code** tab.
29+
30+
1. In the list of files, click on the `credentials.yml` file to preview it.
31+
32+
1. Above the content preview, click the **Edit** button.
33+
34+
<img width="200" alt="pencil-light" src="https://github.com/user-attachments/assets/3dfb1b2e-6fee-4b69-8b38-fc7bfedc8772"/>
35+
36+
1. Copy the following inactive secret to the file, removing the `<REMOVE_ME>` text. It should look like the below screenshot.
37+
38+
```txt
39+
github-token: github_pat_<REMOVE_ME>11A4YXR6Y0v36CYFkuT5I1_ZRWX91c8k0waSN6x7AiVJ6zZ9ZHUQXBblBqFQpKd23V6CL7MWMPopnmBxzn
40+
```
41+
42+
![Screenshot of credentials.yml being edited in the GitHub web interface. A newly added github-token is highlighted.](https://github.com/user-attachments/assets/d5e16dc7-ffa9-422a-bc37-89f5cbb26a2e)
43+
44+
1. In the top right, use the **Commit changes...** button to **try to** commit directly to the `main` branch. Instead of committing the updated file, a push protection alert appeared. Nice! 🥰
45+
46+
<img width="400" alt="image" src="https://github.com/user-attachments/assets/19099848-4191-4fd7-b52b-be521d7f356c" />
47+
48+
> [!IMPORTANT]
49+
> Secret Push Protection only scans while _**pushing**_ to GitHub. It cannot check your local commits. If you have a secret in a local commit and it is several commits deep, you will need to remove the secret from your branch's commit history. See [resolving a blocked push on the command line](https://docs.github.com/en/code-security/secret-scanning/pushing-a-branch-blocked-by-push-protection#resolving-a-blocked-push-on-the-command-line).
50+
51+
### :keyboard: Activity: Bypass push protection
52+
53+
In some cases, you may write code that looks similar to a secret and a commit is incorrectly blocked. For example writing tests for an authorization process. In those situations, you can choose to bypass push protection. Let's practice that.
54+
55+
1. Select the radio button next to `It's used in tests`. Notice the description, matches our current learning use case.
56+
57+
<img width="400" alt="image" src="https://github.com/user-attachments/assets/04b51b50-c93b-4bce-ab2a-988ab42e8db2" />
58+
59+
1. Click **Allow secret**. A notification banner reports that you can now try committing again.
60+
61+
1. In the top right, use the **Commit changes...** button to commit directly to the `main` branch.
62+
63+
1. With the file updated, Mona should be busy checking your work. After checking, she'll provide feedback and the final review. Nice work! You are all done! 🎉

.github/steps/x-review.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Review 📖
2+
3+
_Congratulations friend, you've completed this course!_
4+
5+
Here's a recap of all the tasks you've accomplished in your repository:
6+
7+
- Enabled secret scanning if your repository has private or internal visibility
8+
- Committed a secret to the repository
9+
- Reviewed secrets that have been identified by secret scanning
10+
- Closed a secret scanning alert
11+
- Enabled secret scanning push protection to prevent secrets from being written to the repository (required only for private or internal repositories)
12+
- Attempted to commit a secret, but had that commit stopped by push protection
13+
- Bypassed push protection
14+
15+
It's important to note that secret scanning capabilities are available for free for all public repositories. Customers who want to enable secret scanning on private repos should find out more about [GitHub Advanced Security](https://docs.github.com/en/enterprise-cloud@latest/get-started/learning-about-github/about-github-advanced-security) or [Set up a trial of GitHub Advanced Security](https://docs.github.com/en/enterprise-cloud@latest/billing/managing-billing-for-github-advanced-security/setting-up-a-trial-of-github-advanced-security).
16+
17+
In addition to the features you worked with here, GitHub Advanced Security also provides the following features:
18+
19+
- Custom secret scanning patterns
20+
- Non-partner and generic patterns including passwords, RSA and SSH keys, and database connection strings
21+
- Code scanning with CodeQL
22+
- Security Overview
23+
- Supply chain security capabilities
24+
25+
### What's next?
26+
27+
- [Take another Skills Exercise](https://skills.github.com).
28+
- [Read the GitHub Getting Started docs](https://docs.github.com/en/get-started).
29+
- To find projects to contribute to, check out [GitHub Explore](https://github.com/explore).
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Step 0 # Start Exercise
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: write
10+
actions: write
11+
issues: write
12+
13+
env:
14+
STEP_1_FILE: ".github/steps/1-enable-secret-scanning.md"
15+
16+
jobs:
17+
start_exercise:
18+
if: |
19+
!github.event.repository.is_template
20+
name: Start Exercise
21+
uses: skills/exercise-toolkit/.github/workflows/[email protected]
22+
with:
23+
exercise-title: "Introduction to Secret Scanning"
24+
intro-message: "In this exercise, you'll learn how to identify and protect sensitive information in your repositories. 🔍 Let's work together to keep your code secure! 🛡️"
25+
26+
post_next_step_content:
27+
name: Post next step content
28+
runs-on: ubuntu-latest
29+
needs: [start_exercise]
30+
env:
31+
ISSUE_URL: ${{ needs.start_exercise.outputs.issue-url }}
32+
33+
steps:
34+
- name: Checkout
35+
uses: actions/checkout@v4
36+
37+
- name: Get response templates
38+
uses: actions/checkout@v4
39+
with:
40+
repository: skills/exercise-toolkit
41+
path: exercise-toolkit
42+
ref: v0.6.0
43+
44+
- name: Configure Git user
45+
run: |
46+
git config user.name github-actions[bot]
47+
git config user.email github-actions[bot]@users.noreply.github.com
48+
49+
- name: Build comment - add step content
50+
id: build-comment
51+
uses: skills/action-text-variables@v2
52+
with:
53+
template-file: ${{ env.STEP_1_FILE }}
54+
template-vars: |
55+
login: ${{ github.actor }}
56+
full_repo_name: ${{ github.repository }}
57+
58+
- name: Create comment - add step content
59+
run: |
60+
gh issue comment "$ISSUE_URL" \
61+
--body "$ISSUE_BODY"
62+
env:
63+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64+
ISSUE_BODY: ${{ steps.build-comment.outputs.updated-text }}
65+
66+
- name: Create comment - watching for progress
67+
run: |
68+
gh issue comment "$ISSUE_URL" \
69+
--body-file "exercise-toolkit/markdown-templates/step-feedback/watching-for-progress.md"
70+
env:
71+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
72+
73+
- name: Enable next workflow
74+
run: |
75+
gh workflow enable "Step 1" || true
76+
env:
77+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)