Security Improvements and Performance Fixes with GitHub Copilot
30-second answer
GitHub Copilot can review code for security and performance when you ask. Select the code and ask in Chat ("Is this safe?", "Find vulnerabilities", "This runs in a loop, can it be faster?") or use /fix. It spots common problems such as SQL injection, hard-coded secrets, missing input validation, N+1 queries and repeated work in loops, then proposes a fix. Copilot's filters also block some insecure patterns in suggestions, but not all. Treat its advice as a starting point: confirm with tests, code scanning (CodeQL), dependency checks and a human review.
Simulate it » No Azure account neededExam objective: 5.2.3 Suggest security improvements and performance optimizations. (Skills measured as of August 7, 2026)
Simulations
Feel it before you read it. No account needed, nothing leaves this page.
Ask Copilot to <strong>review for security</strong>. It spots common problems like SQL injection, hard-coded secrets and missing input validation, and suggests a fix.
👆 Click the glowing item on screen to continue
Read the steps as text
- Ask Copilot to review for security. It spots common problems like SQL injection, hard-coded secrets and missing input validation, and suggests a fix.
- For performance, describe how the code is used. Copilot can point out N+1 queries, repeated work in loops and missing caching.
- Back it up with tools: code scanning, dependency checks and tests in CI. Copilot’s security suggestions are a help, not a guarantee.
🧪 Simulation for learning. Screens are simplified versions of Visual Studio Code and github.com with GitHub Copilot; names, files and suggestions are examples. Not affiliated with GitHub or Microsoft, and nothing here touches your real account.
🖱️ Drag to rotate · scroll or pinch to zoom
Post-processing filters
Before you see it, the response is post-processed: checks for code quality and common insecure patterns, offensive content, and (if your policy says Block) a check for matches with public code of about 150 characters or more.
🤔 What happens if…
Read this tour as text
- You type, then pause. Copilot doesn’t read your mind. When you pause while typing, the Copilot extension in your IDE starts a request for a completion.
- The IDE builds a prompt. The extension gathers context: code before and after the cursor, the file name and language, and related snippets from other open tabs. That bundle is the prompt. A model has a limited context window, so the extension picks what matters most.
- Content exclusions are checked. An admin can exclude paths or whole repositories. Excluded files are not used as context, and Copilot gives no inline suggestions inside them.
- The Copilot proxy. The prompt goes over HTTPS to a GitHub-hosted proxy. It authenticates the request and runs pre-filters, for example for harmful or offensive content and prompt-injection attempts, before anything reaches the model.
- The model predicts code. A large language model predicts the most likely next tokens. It matches patterns from training data and your context; it doesn’t run, test or truly understand your code. That’s why output can be wrong, outdated or insecure.
- Post-processing filters. Before you see it, the response is post-processed: checks for code quality and common insecure patterns, offensive content, and (if your policy says Block) a check for matches with public code of about 150 characters or more.
- Ghost text: accept or reject. The suggestion appears as grey ghost text. Tab accepts, Esc dismisses, Alt+] / Alt+[ cycle alternatives, Ctrl+→ accepts word by word. You decide what goes into your code.
- What data is kept. Prompts and suggestions are encrypted in transit. For Copilot Business and Enterprise, your code is not used to train GitHub’s models, and prompts and suggestions from IDE code completion are not retained after the response. Engagement data (accepts, rejects, errors) is kept to run and improve the service and to power usage metrics.
A free terminal with GitHub Copilot CLI, git and gh. No tasks: install the CLI, start a session, try slash commands and tool permissions, or call the seats API.
🎓 Education-only simulation. Your lab is saved in this browser, so you can leave and continue later.
😟 THE PROBLEM
A query built with string concatenation is waiting to be exploited.
😊 THE SOLUTION
Ask Copilot to review for security and performance, then prove it with tools.
- SQL injection✓ Parameterised queries
- N+1 queries✓ Batch the work
- Trusting one tool✓ Code scanning in CI too
Start with a question
Your orders page is slow and a reviewer wrote "this looks injectable". Can Copilot help with both?
Yes, if you ask it the right way. Here’s the function in contoso-shop:
export async function findOrders(db, customer) {
return db.query(`SELECT * FROM orders WHERE customer = '${customer}'`);
}The customer name goes straight into the SQL. Type ' OR '1'='1 as a name and you get every order in the table. And the page calls this once per customer in a loop, so 200 customers means 200 queries.
Select it, ask Copilot “Is this safe, and can it be faster?”, and it’ll name both problems and suggest a fix. Your job is to prove the fix works.
Explain it like I’m 10
The technical explanation
Copilot doesn’t scan your code on its own. You get security and performance help by asking, and the more you say about the risk and how the code is used, the better the answer.
Asking for a security review
Select the code, open Chat, and be specific:
- “Review this for security issues, especially injection and input validation.”
- “Is there anything here that would leak a secret?”
/fixwhen you already know what’s wrong, for example after a test fails.
Here’s what Copilot usually suggests for the function above:
export async function findOrders(db, customer) {
return db.query('SELECT id, total FROM orders WHERE customer = $1', [customer]);
}
The value goes in as a parameter, so it can’t change the SQL. It also selects only the columns the page needs.
| Problem Copilot often spots | Typical fix it proposes |
|---|---|
| SQL built by joining strings | Parameterised query |
| API key written in the code | Read it from an environment variable or a secrets manager |
| No checks on user input | Validate type, length and range at the edge |
| Weak hashing for passwords | A slow password hash such as bcrypt or Argon2 |
| Unescaped HTML from user input | Escape the output, or use the framework’s safe rendering |
Asking for a performance fix
“Make this faster” on its own gets you a guess. Tell Copilot how the code runs: “This is called once per customer in a loop of a few hundred.”
| Problem | What it looks like | Fix |
|---|---|---|
| N+1 queries | One query per item in a loop | One query: WHERE customer = ANY($1) |
| Repeated work | Same calculation on every pass | Compute once, outside the loop |
| Too much data | SELECT * for two fields | Select only what you use |
| No caching | Same expensive call every request | Cache the result with a sensible expiry |
What Copilot’s filters do and don’t do
Before a suggestion reaches your editor, Copilot’s post-processing checks it for some common insecure patterns, such as hard-coded credentials. That helps. It doesn’t make suggestions secure. The model predicts likely code, and plenty of public code is insecure or out of date. It can also invent package names or use an API that’s been deprecated.
So every Copilot fix gets the same checks as human code:
-
1 Ask
Select the code and name the risk or how it runs.
-
2 Read
Understand the fix. Don't accept what you can't explain.
-
3 Test
Add a test that fails on the old code and passes on the new.
-
4 Scan
Code scanning and dependency checks run on the pull request.
-
5 Review
A teammate approves. Branch protection keeps that step.
Architect level Make good habits the default with custom instructions
You can put your security rules in .github/copilot-instructions.md, for example “Always use parameterised queries with the pg driver” and “Read secrets from process.env, never literals”. Chat, the coding agent and Copilot code review all read that file, so suggestions follow your standards more often. It nudges the output. It doesn’t replace code scanning.
Real-world business scenario
Contoso (fictional)
- The problem
- The order history page in contoso-shop took eight seconds for big accounts, and a pen test flagged two string-built SQL queries.
- What they used
- Developers asked Copilot Chat to review src/orders.js for injection and to batch the per-customer queries, then added regression tests and kept CodeQL code scanning on every pull request.
- Why that choice
- Copilot found both problems in minutes and drafted the fixes. The tests proved the queries returned the same rows, and code scanning confirmed the alerts closed.
- The result
- The page loads in under a second, the pen-test findings are closed, and the team added its SQL rule to the repository's custom instructions.
When to use it and when not to
✓ Use it when…
- A second pair of eyes on a function before you open a pull request.
- Explaining why a code scanning alert matters and drafting the fix.
- Spotting N+1 queries and repeated work when you describe how the code runs.
✕ Think twice when…
- As your only security check. Keep code scanning, secret scanning and human review.
- Pasting production data, keys or customer details into the prompt.
- Accepting a fix you can't explain or haven't tested.
↔ Alternatives to consider
- Code scanning with CodeQL for automatic analysis on every pull request.
- Copilot code review to get comments on a whole pull request.
- A profiler or query plan to measure a performance fix instead of guessing.
Test yourself with exercises
5 original practice questions in the GH-300 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.
Interview preparation
Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.
Beginner How would you ask Copilot to check a function for security problems?
Model answer
I'd select the function and ask in Chat, something like "Review this for security issues, for example injection or unsafe input handling." Being specific about the risk helps. Then I'd read the fix and run the tests before keeping it.
Interviewer follows up
Does Copilot find every vulnerability?
You
No. It catches common patterns, but it can miss things and it can be wrong. That's why I still run code scanning and keep a human review.
Intermediate Copilot suggests a fix for an N+1 query. What do you check before accepting it?
Model answer
That the new query returns the same results, including empty lists and duplicates, that the tests still pass, and that it's actually faster with realistic data. A quick timing or a query plan tells me more than the suggestion does.
Experienced developer Copilot has a filter for insecure code. Why do you still need code scanning?
Model answer
The filter checks suggestions for some common insecure patterns before they appear. It doesn't analyse data flow across the whole codebase, and it doesn't look at code humans wrote. CodeQL does, on every pull request, and it gives an alert I can track until it's fixed.
Interviewer follows up
Where does Copilot fit alongside CodeQL?
You
Before the pull request: explaining a risk, proposing a fix, writing a test that proves the bug. CodeQL is the check that runs whether or not anyone remembered to ask.
Scenario A teammate pastes a production stack trace with customer emails into Chat to get a performance fix. What do you say?
Model answer
Strip the personal data first, or reproduce the problem with fake data. Prompts leave the editor, and our policy is no customer data in prompts. The fix doesn't need real emails to be right.
Architecture & design How would you build Copilot into a team's secure development process without trusting it blindly?
Model answer
Use it where it's cheap to check: reviewing diffs, suggesting fixes, writing regression tests. Keep the gates that don't depend on it: code scanning and secret scanning on pull requests, dependency review, branch protection with human approval. Add security standards to the repository's custom instructions so suggestions follow them more often.
Quick revision
Key points
- Ask Copilot directly to review for security or performance, and name the risk you care about.
- Common security finds include SQL injection, hard-coded secrets, missing input validation and unsafe deserialisation.
- Common performance finds include N+1 queries, repeated work inside loops, selecting more data than needed and missing caching.
- Copilot's post-processing filters catch some insecure patterns in suggestions, not all.
- Confirm every fix with tests, code scanning, dependency checks and human review.
- Never paste secrets or real personal data into prompts to get help.
Important terminology
- SQL injection
- An attack where user input changes the meaning of a database query because the query was built by joining strings.
- Parameterised query
- A query with placeholders such as $1, where the database driver passes values separately so they can't change the SQL.
- N+1 query
- A performance problem where code runs one query to get a list and then one more query for each item, instead of a single query.
- Code scanning
- A GitHub Advanced Security feature that analyses code, usually with CodeQL, and raises alerts for vulnerabilities on pushes and pull requests.
- Insecure-pattern filter
- Part of Copilot's post-processing that checks suggestions for common insecure code, such as hard-coded credentials, before you see them.
- /fix
- A Copilot Chat slash command that proposes a fix for a problem in the selected code.
Common mistakes
- Accepting a security fix because it looks right, without a test that shows the old bug and the fix.
- Assuming Copilot's insecure-pattern filter means suggestions are secure.
- Asking "make it faster" with no context about how the code is called.
- Pasting production data or keys into Chat.
Exam tips
- "Use Copilot to find vulnerabilities in selected code" = select it and ask Chat (or /fix for a known problem).
- "Ensure a Copilot security fix actually works" = tests plus code scanning, not more prompts.
- "One query per item in a loop" = N+1; the fix is a single batched query.
- "Copilot filters insecure code, so review is optional" is always false.
Five quick revision questions
-
1.What's the usual fix for SQL injection?Show
A parameterised query with placeholders instead of string concatenation.
-
2.Name a performance problem Copilot can spot in loops.Show
An N+1 query, or repeating the same work on every pass.
-
3.Do Copilot's filters block all insecure code?Show
No, only some common patterns.
-
4.Which GitHub feature runs CodeQL on pull requests?Show
Code scanning.
-
5.Which slash command asks Copilot to propose a fix?Show
/fix.
Official references
Finished this lesson? Tick it off. Your progress is saved in this browser.
Next up: Content Exclusions and Editor Settings
Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges.