Skip to content

Latest commit

 

History

History
73 lines (61 loc) · 3.7 KB

File metadata and controls

73 lines (61 loc) · 3.7 KB

Exercise: Improve Code Security Using GitHub Copilot Chat

Overview

In this exercise, you will use GitHub Copilot Chat to generate suggestions that help improve code security in a sample application. The focus will be on enhancing authentication, data protection, secure error handling, and compliance with security standards.

Prerequisites

Before starting this exercise, ensure you have the following tools and resources installed:

  • Visual Studio Code
  • The C# Dev Kit extension for Visual Studio Code
  • GitHub Copilot and GitHub Copilot Chat extensions

Steps

1. Prepare the Sample Application

  1. Open the APL2007M5BankAccount-Security project in Visual Studio Code.
  2. Familiarize yourself with the Program.cs and BankAccount.cs files under the BankAccountClass project.
  3. Build and run the project to ensure it works as expected:
    • Open the terminal in Visual Studio Code and run the following command:
    cd BankAccountClass
    dotnet run
    • The application should generate random bank account details and perform basic operations like crediting, debiting, and transferring funds.
    • The output should display the account details and the results of the operations.

2. Identify Areas for Improvement

  1. Review the following methods in the BankAccount.cs file:
    • Credit
    • Debit
    • Transfer
    • CalculateInterest
  2. Consider aspects such as authentication, data protection, and secure error handling.

3. Use GitHub Copilot Chat to Generate Suggestions

  1. Open the Chat view in Ask mode in Visual Studio Code.
  2. Check that the the BankAccount.cs file is attached to the Chat context, if it is not then ensure you have it selected in the editor.
  3. Use the following prompt to generate suggestions:
    • "@workspace How can I improve authentication and data protection in the BankAccount class?"
  4. Review the suggestions provided by GitHub Copilot Chat.
  5. Implement updates that enhance code security, such as:
    • Adding authentication mechanisms.
    • Encrypting sensitive data.
    • Avoiding hardcoded passwords.

4. Secure Exception Handling

  1. Open the Chat view in Ask mode in Visual Studio Code.
  2. Check that the the Program.cs file is attached to the Chat context, if it is not then ensure you have it selected in the editor.
  3. Use the following prompt to generate suggestions:
    • "@workspace How can I implement secure exception handling in the Program.cs file?"
  4. Review the suggestions provided by GitHub Copilot Chat.
  5. Implement updates that enhance secure exception handling, such as:
    • Catching specific exceptions instead of generic ones.
    • Logging errors securely.

6. Iterate and Refine

  1. Use additional prompts to explore further improvements.

  2. Repeat the process of reviewing, implementing, and testing updates.

    Consider the following additional prompts:

    • "@workspace How can I avoid future security vulnerabilities in the Program.cs file?"
    • "@workspace How can I improve compliance with security standards in the BankAccount class?"
  3. Implement updates that enhance code security, such as:

    • Refactoring methods to handle sensitive data securely.
    • Improving authentication and authorization mechanisms.
    • Ensuring compliance with security standards and regulations.

Notes

  • The suggestions provided by GitHub Copilot Chat are not exhaustive. Use your judgment and expertise to evaluate and implement them.
  • Consider your workplace's coding standards and policies when choosing which updates to implement.
  • Ensure thorough code reviews and testing to maintain high-quality code.
  • If GitHub Copilot suggests creating a new file, use the "insert into new file" option to have it create the file for you.