Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 26 of 30
❮Prev Next❯

Security Improvements and Performance Fixes with GitHub Copilot

Module 5: Productivity ⏱ 10 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    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 needed

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

    VS Code & GitHub: Security improvements and performance fixes Fix a SQL injection and an N+1 query with Copilot.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading…

    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
    1. Ask Copilot to review for security. It spots common problems like SQL injection, hard-coded secrets and missing input validation, and suggests a fix.
    2. For performance, describe how the code is used. Copilot can point out N+1 queries, repeated work in loops and missing caching.
    3. 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.

    😟 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
    🛠️ Solved with: Security and performance

    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?”
    • /fix when 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 spotsTypical fix it proposes
    SQL built by joining stringsParameterised query
    API key written in the codeRead it from an environment variable or a secrets manager
    No checks on user inputValidate type, length and range at the edge
    Weak hashing for passwordsA slow password hash such as bcrypt or Argon2
    Unescaped HTML from user inputEscape 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.”

    ProblemWhat it looks likeFix
    N+1 queriesOne query per item in a loopOne query: WHERE customer = ANY($1)
    Repeated workSame calculation on every passCompute once, outside the loop
    Too much dataSELECT * for two fieldsSelect only what you use
    No cachingSame expensive call every requestCache 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. 1 Ask

      Select the code and name the risk or how it runs.

    2. 2 Read

      Understand the fix. Don't accept what you can't explain.

    3. 3 Test

      Add a test that fails on the old code and passes on the new.

    4. 4 Scan

      Code scanning and dependency checks run on the pull request.

    5. 5 Review

      A teammate approves. Branch protection keeps that step.

    From a Copilot suggestion to a fix you can trust
    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

    Online retail

    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.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    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.

    Question 1

    Question 1 of 5

    A developer selects a Node.js function that builds a SQL statement by inserting a request parameter into a template string. The developer asks GitHub Copilot Chat whether the function is safe. Which change should Copilot most likely recommend?

    Question 2

    Question 2 of 5

    A function retrieves a list of 300 customers and then runs one database query per customer to load their orders. A developer wants GitHub Copilot to suggest a performance improvement. Which prompt is most likely to produce a useful suggestion?

    Question 3

    Question 3 of 5 · Select 2

    GitHub Copilot proposes a fix for a security vulnerability in a pull request. Which TWO actions should the team take to validate the fix before merging? Each correct answer presents part of the solution.

    Question 4

    Question 4 of 5

    A team lead states that developers can skip security review of Copilot-generated code because Copilot filters insecure code before showing suggestions. Which statement correctly describes Copilot's behavior?

    Question 5

    Question 5 of 5

    A developer wants Copilot Chat to help diagnose a slow endpoint. The only available example is a production log that contains customer email addresses. What should the developer do?

    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. 1.What's the usual fix for SQL injection?Show

      A parameterised query with placeholders instead of string concatenation.

    2. 2.Name a performance problem Copilot can spot in loops.Show

      An N+1 query, or repeating the same work on every pass.

    3. 3.Do Copilot's filters block all insecure code?Show

      No, only some common patterns.

    4. 4.Which GitHub feature runs CodeQL on pull requests?Show

      Code scanning.

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

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.