|
| 1 | +# Lesson 03 – 15-Minute Demo: Copilot + CodeQL |
| 2 | + |
| 3 | +**Course:** GitHub Copilot for Cybersecurity Pros |
| 4 | +**Runtime target:** ~15 minutes, single smooth narrative |
| 5 | + |
| 6 | +## Flow at a Glance |
| 7 | + |
| 8 | +- **Part 1 (~8 min):** Use **Copilot Agent mode** to generate and run a security‑focused test suite. |
| 9 | +- **Part 2 (~7 min):** Turn on **CodeQL code scanning** with GitHub Actions and point it at this lesson’s code. |
| 10 | + |
| 11 | +Keep the story: |
| 12 | + |
| 13 | +> Copilot helps you **create** security tests. GHAS and CodeQL help you **enforce** them continuously. |
| 14 | +
|
| 15 | +--- |
| 16 | + |
| 17 | +## Pre-Demo Setup (Do Before Recording) |
| 18 | + |
| 19 | +1. **Repo + Branch** |
| 20 | + - Push the `lesson-03` folder to a **private GHEC repo** with GHAS turned on. |
| 21 | + - Create a branch like `feature/lesson-03-security-tests`. |
| 22 | + |
| 23 | +2. **Node/Test Harness** |
| 24 | + - In the repo root (or inside `lesson-03/demo-01-oauth-tests`), run: |
| 25 | + |
| 26 | + ```bash |
| 27 | + npm init -y |
| 28 | + npm install --save-dev jest |
| 29 | + ``` |
| 30 | + |
| 31 | + - Add to `package.json`: |
| 32 | + |
| 33 | + ```json |
| 34 | + { |
| 35 | + "scripts": { |
| 36 | + "test": "jest" |
| 37 | + } |
| 38 | + } |
| 39 | + ``` |
| 40 | + |
| 41 | + (Merge this into the existing `scripts` section if needed.) |
| 42 | + |
| 43 | +3. **VS Code + Copilot** |
| 44 | + - Confirm **GitHub Copilot** and **Copilot Chat** are enabled in VS Code. |
| 45 | + - In the Copilot Chat panel, make sure you can switch to **Agent mode** (mode selector: Ask / Edit / Agent). |
| 46 | + |
| 47 | +4. **GHAS / CodeQL entitlement** |
| 48 | + - In GitHub, repo **Settings → Security → Advanced Security**: verify GitHub Advanced Security is enabled for the repo. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## Part 1 – Copilot Agent: Generate & Run Security Tests (~8 min) |
| 53 | + |
| 54 | +**Goal:** Show Copilot Agent understanding the repo, building a test suite, and running it for you. |
| 55 | + |
| 56 | +### 1. Set the Scene (30–60 seconds) |
| 57 | + |
| 58 | +- Open `lesson-03/demo-01-oauth-tests/oauth-security-tests.js` in VS Code. |
| 59 | +- Talk track bullets: |
| 60 | + - “This lesson is about **automated security testing**.” |
| 61 | + - “We’ll let **Copilot Agent mode** design and wire up a test suite.” |
| 62 | + - “Then we’ll run the tests straight from chat to prove it’s not just code-as-confetti.” |
| 63 | + |
| 64 | +### 2. Ask Copilot to Analyze the Code (Agent Mode) (1–2 min) |
| 65 | + |
| 66 | +1. Open **Copilot Chat**. |
| 67 | +2. Switch to **Agent** mode. |
| 68 | +3. Use a prompt like: |
| 69 | + |
| 70 | + ```text |
| 71 | + You are helping a security engineer. |
| 72 | +
|
| 73 | + Analyze this repo, focusing on lesson-03/demo-01-oauth-tests/oauth-security-tests.js. |
| 74 | + Summarize what is being tested now, and list the top 5 security behaviors that are currently NOT covered: |
| 75 | + - token expiration and invalidation |
| 76 | + - refresh token rotation |
| 77 | + - brute-force / rate limiting behavior |
| 78 | + - misuse of scopes |
| 79 | + - logging and audit trails for suspicious auth events |
| 80 | +
|
| 81 | + Then propose a minimal but realistic Jest test suite structure to cover those risks. |
| 82 | + ``` |
| 83 | + |
| 84 | +- Let Agent mode show its plan and file selections. |
| 85 | + |
| 86 | +### 3. Have Copilot Implement the Test Suite (2–3 min) |
| 87 | + |
| 88 | +1. In the same Agent session, follow up: |
| 89 | + |
| 90 | + ```text |
| 91 | + Great. Please implement your proposed Jest test suite. |
| 92 | +
|
| 93 | + - Create a new file: lesson-03/demo-01-oauth-tests/oauth-security-tests.spec.js |
| 94 | + - Use Jest best practices: describe blocks by threat, not function name. |
| 95 | + - Include tests for: |
| 96 | + - expired access tokens |
| 97 | + - refresh token rotation and invalidation of the old refresh token |
| 98 | + - rate limiting / lockout after repeated failed logins |
| 99 | + - scope misuse (reject tokens lacking the required scope) |
| 100 | + - Assume we can import the auth module from ../src/auth.js (create a simple stub if needed). |
| 101 | + - Add any missing devDependencies and npm scripts to package.json to run these tests with `npm test`. |
| 102 | + ``` |
| 103 | +
|
| 104 | +2. Accept the Agent’s edits (review the diff quickly). |
| 105 | +3. Talk track bullets: |
| 106 | + - “Agent mode is not just completing a single function.” |
| 107 | + - “It’s planning, touching multiple files, wiring scripts, and keeping tests consistent with the existing style.” |
| 108 | +
|
| 109 | +### 4. Ask Copilot to Run the Tests (1–2 min) |
| 110 | +
|
| 111 | +You want to show the agent actually executing tooling. |
| 112 | +
|
| 113 | +1. In the same Agent conversation, ask: |
| 114 | +
|
| 115 | + ```text |
| 116 | + Now run the Jest test suite you just created and show me the results. |
| 117 | + ``` |
| 118 | +
|
| 119 | +2. Agent mode should run `npm test` in a background shell and stream the output. |
| 120 | +
|
| 121 | +3. If some tests fail, that’s your moment: |
| 122 | + - “Security tests failing are good; they reveal gaps.” |
| 123 | + - Then: |
| 124 | +
|
| 125 | + ```text |
| 126 | + Fix the failing tests by adjusting either the test data or the auth stub so they reflect a realistic, secure implementation, then re-run the tests. |
| 127 | + ``` |
| 128 | +
|
| 129 | +4. End Part 1 with a quick bow: |
| 130 | + - “We just turned a vague security requirement into an executable test suite and verified it, all from chat.” |
| 131 | +
|
| 132 | +--- |
| 133 | +
|
| 134 | +## Part 2 – CodeQL via GitHub Actions on This Lesson (~7 min) |
| 135 | +
|
| 136 | +**Goal:** Show CodeQL code scanning on the same repo, tuned to this lesson’s code. |
| 137 | +
|
| 138 | +You’re shifting from local, Copilot-assisted testing to cloud-side, GHAS-backed SAST. |
| 139 | +
|
| 140 | +### 1. Enable CodeQL Default Setup (UI) (2–3 min) |
| 141 | +
|
| 142 | +1. In the browser, open the same repo on GitHub. |
| 143 | +2. Click **Settings**. |
| 144 | +3. In the left sidebar under **Security**, click **Advanced Security**. |
| 145 | +4. Under **Code Security → CodeQL analysis**, click **Set up → Default**. |
| 146 | +5. Choose: |
| 147 | + - **Languages:** ensure **JavaScript** (and others if present) are selected. |
| 148 | + - **Events:** keep the defaults (`push`, `pull_request`, and a schedule). |
| 149 | +
|
| 150 | +This generates a `.github/workflows/codeql.yml` workflow that uses the CodeQL Action. |
| 151 | +
|
| 152 | +### 2. Customize CodeQL Config with Copilot (2–3 min) |
| 153 | +
|
| 154 | +Now you tune CodeQL to your course materials, using Copilot inside the workflow file. |
| 155 | +
|
| 156 | +1. From the repo in GitHub, open `.github/workflows/codeql.yml` in the web editor (or pull it locally in VS Code). |
| 157 | +2. Point out that this is advanced config as code for code scanning. |
| 158 | +3. In VS Code, open the workflow file and use Copilot Chat (Ask or Edit mode): |
| 159 | +
|
| 160 | + ```text |
| 161 | + Update this CodeQL workflow so it focuses on the lesson-03 folder for JavaScript. |
| 162 | +
|
| 163 | + - Limit the analysis to the lesson-03 directory tree. |
| 164 | + - Keep the default query suite, but add configuration so I can plug in custom queries from lesson-03/demo-03-sast-dast/codeql/queries if needed. |
| 165 | + - Make sure the job still runs on: |
| 166 | + - pushes to main |
| 167 | + - pull requests targeting main |
| 168 | + - a nightly schedule |
| 169 | + ``` |
| 170 | +
|
| 171 | +4. Accept Copilot’s edits. You might see: |
| 172 | + - A `paths:` filter limiting scanning to `lesson-03/**`. |
| 173 | + - A comment block hinting at a custom query directory. |
| 174 | +
|
| 175 | +Talking point: |
| 176 | +
|
| 177 | +> “Now CodeQL is wired to this training code, and I can easily add my own queries for things like SQL injection beyond the stock packs.” |
| 178 | +
|
| 179 | +### 3. Trigger a Run and Show Alerts (2 min) |
| 180 | +
|
| 181 | +1. Commit the workflow changes. |
| 182 | +2. In GitHub: |
| 183 | + - Go to **Actions**, show the `CodeQL` workflow run kicking off. |
| 184 | + - Then go to **Security → Code scanning alerts** and show the alert list once the run finishes. |
| 185 | +
|
| 186 | +3. Quick bullet commentary: |
| 187 | + - “CodeQL is static analysis as a service.” |
| 188 | + - “GHAS ties this to other features like secret scanning and Copilot-powered fixes.” |
| 189 | +
|
| 190 | +--- |
| 191 | +
|
| 192 | +## Close – Tie Copilot, GHAS, and GHEC Together (≤1 min) |
| 193 | +
|
| 194 | +Final talking points: |
| 195 | +
|
| 196 | +- **Copilot Agent Mode:** turns vague security requirements into tests and commands, executed from chat. |
| 197 | +- **CodeQL + GHAS:** turn those security expectations into repeatable, repo-wide gates. |
| 198 | +- **GHEC context:** all of this lives behind enterprise controls, policy, and auditability. |
| 199 | +
|
| 200 | +> This is the pattern: agent-assisted development on your laptop, CodeQL and GHAS enforcing the same intent on every pull request. |
0 commit comments