diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..8ac6b8c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "monthly" diff --git a/.github/steps/-step.txt b/.github/steps/-step.txt new file mode 100644 index 0000000..573541a --- /dev/null +++ b/.github/steps/-step.txt @@ -0,0 +1 @@ +0 diff --git a/.github/steps/0-welcome.md b/.github/steps/0-welcome.md new file mode 100644 index 0000000..9ff13a5 --- /dev/null +++ b/.github/steps/0-welcome.md @@ -0,0 +1 @@ + diff --git a/.github/steps/1-enable-codeql.md b/.github/steps/1-enable-codeql.md new file mode 100644 index 0000000..c9baeef --- /dev/null +++ b/.github/steps/1-enable-codeql.md @@ -0,0 +1,42 @@ + + +## Step 1: Enable CodeQL + +πŸ‘‹ Hello! Welcome to the GitHub Skills course: Enable code scanning! + +Let's get started! + +In this first step, we'll be learning more about CodeQL and how to use it to secure your source code. + +**What is GitHub code scanning**: _[Code scanning](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/about-code-scanning)_ is a capability that allows development teams to integrate security testing tools into the software development process. This is done using GitHub Actions. With code scanning, you can integrate many different types of tools including SAST, container, and infrastructure as code security tools. + +**What is CodeQL**: _[CodeQL](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/about-code-scanning-with-codeql)_ is a static analysis testing tool that helps you identify security weaknesses such as SQL injection, cross-site scripting, and code injection issues. + +### :keyboard: Activity: Enable code scanning with CodeQL + +First, we will enable code scanning with CodeQL in our repository. + +1. Open a new browser tab, and work on the steps in your second tab while you read the instructions in this tab. +2. Navigate the to **Settings** tab at the top of your newly created repository. +3. Under the **Security** section on the left side, select **Code security and analysis**. +4. Scroll down to the section titled **Code scanning**. For the purpose of this course, we will focus on CodeQL analysis. +5. Click on the **Set up** dropdown menu and choose **Default**. +![enable-code-scanning-default.png](/images/enable-code-scanning-default.png) + +Let's take a look at the configuration options in the modal: + + - **Languages to analyze:** These are the languages that will be scanned by CodeQL. In this case, we will be scanning in `Python`. + - **Query suites:** CodeQL [queries](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/about-code-scanning-with-codeql#about-codeql-queries) are packaged in bundles called "suites". This section allows you to choose which query suite to use. We'll leave this set as **Default** for this exercise. For more information, see "[About CodeQL queries](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/about-code-scanning-with-codeql#about-codeql-queries)." + - **Events:** This section tells CodeQL when to scan. In this case, it's set to scan on any pull request to the `main` branch. + +![codeql-default-configuration-box.png](/images/codeql-default-configuration-box.png) + +6. Click **Enable CodeQL** +7. Wait about 20 seconds then refresh this page (the one you're following instructions from). [GitHub Actions](https://docs.github.com/en/actions) will automatically update to the next step. diff --git a/.github/steps/2-review-and-triage-codeql-alerts.md b/.github/steps/2-review-and-triage-codeql-alerts.md new file mode 100644 index 0000000..28478b8 --- /dev/null +++ b/.github/steps/2-review-and-triage-codeql-alerts.md @@ -0,0 +1,88 @@ + + +## Step 2: Review and Triage CodeQL Alerts + +_Way to go! You got CodeQL running! :tada:_ + +In this exercise, we'll review the CodeQL scan results, triage an alert, and create a GitHub issue to track an alert. + +**What is GitHub Actions**: GitHub Actions is the automation and CI/CD platform within GitHub. We use GitHub Actions to orchestrate and execute security scans with code scanning. GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. For more information on GitHub Actions, see "[Understanding GitHub Actions](https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions)." + +**What is CWE**: Common Weakness Enumeration (CWE) is a category system for hardware and software weaknesses and vulnerabilities. Think of it as a way to describe and categorize security issues in an application's source code. For more information on CWEs, see the Wikipedia article "[Common Weakness Enumeration](https://en.wikipedia.org/wiki/Common_Weakness_Enumeration)." + +### :keyboard: Activity 1: View the status of a CodeQL scan + +In this activity, we'll explore GitHub Actions to view the status of a CodeQL scan. +1. In your new repository, go to your Actions page by selecting **Actions** from the top navigation bar. If the CodeQL Action run is still executing, you will see a yellow spinner indicating the scan is still in progress. This typically takes about 4 minutes to complete. +2. Select the run by clicking on **CodeQL Setup**. + +![codeql-setup](/images/codeql-setup.png) + +Notice that more information is available inside the Actions run. Feel free to explore this section to view information such as the CodeQL logs, duration, status, and artifacts generated by CodeQL. + +Once the scan is complete, a green check will show next to the execution. + +### :keyboard: Activity 2: View all CodeQL Alerts + +In this activity, we will view the CodeQL findings in the Security page of your repository. The Security page is where all security related information is displayed. + +1. Navigate to the **Security** tab in the top navigation bar of your repository. +2. Select **Code scanning** under the "Vulnerability alerts" heading in left-side navigation bar. + +This screen will contain all the vulnerabilities identified by CodeQL inside this repository's codebase. Explore the different filters and search capabilities in this page. These filtering capabilities become very helpful when you're working with many findings! + + +### :keyboard: Activity 3: Review an Alert + +In this activity, we will explore the alert UI. We'll review the data flow of the vulnerability, indentify what part of the code the alert impacts, and get more information about the alert. + +**Alert status:** This section displays the current alert status (open or closed), identifies the branch where the scan detected the alert, and shows the timestamp of the alert. + +![alert-status](/images/alert-status.png) + +**Location information:** This section describes which part of the code is vulnerable. + +![location-information](/images/location-information.png) + +**Paths:** Clicking on "Show paths" will give you additional insights into the alert's data flow. The modal shows us where the user input (we call that a "source") flows through the application until it's acted on (we call this the "sink"). This visualizes the flow of data through your application. + +**Recommendations:** This section provides a quick overview of the tool (CodeQL in this case), Rule ID, and even allows you to view the CodeQL query used to find this vulnerability. You can view the query by clicking **View source**. Additionally, this pane includes recommendations for fixing this vulnerability. Click **Show more** to view the full recommendation. + +![recommendations](/images/recommendations.png) + +**Audit trail:** The audit trail shows the history of the alert. This trail will show the status as users mark an alert as closed or fix an alert in the code. + +![audit-trail](/images/audit-trail.png) + +**Alert triage:** Use the buttons at the top right of the alert to triage or create a new issue for the alert. Don't do anything yet. We'll get into these buttons in a moment. πŸ˜„ + +**Additional info:** Finally, the right-side panel contains information such as tags, CWE information, and the severity of the alert + ![additional-information.png](/images/additiona-information.png) + + +### :keyboard: Activity 4: Dismiss an Alert +Now that we're familiar with the alert layout, let's work through the process of closing one. + +1. Inside this same alert, click **Dismiss alert**, choose any reason for dismissal, and add a short note. +2. Click **Dismiss alert**. +3. At this point, the alert will change its state to "Dismissed". You can now see the change you made in the audit trail at the bottom of the alert. +4. Navigate back to **Security** > **Code scanning alerts**. You'll see that you only have 1 alert listed. +5. Click **1 Closed**. This will bring you to the closed alerts where you can view the alert you just closed. + ![one-closed-alert.png](/images/one-closed-alert.png) + +7. (Optional) You can also reopen the alert by opening it, then selecting **Reopen alert**. + +### :keyboard: Activity 5: Create a GitHub Issue for an Alert +This last step will show you how to create a GitHub Issue to track the work that goes into resolving a vulnerability. Issues provide a space for collaboration for a security problem and can be assigned to people or teams. + +1. Open one of the open alerts that CodeQL from the scan. +2. Click the green **Create issue** button at the top right of the alert. If you don't see this button, check the status of the alert to make sure it's an open alert. +3. Add any details you would like to include in the new issue form. +4. Click **Submit new issue**. +5. To view the your issue, click **Issues** in the top navigation bar of your repository. +6. Wait about 20 seconds then refresh this page (the one you're following instructions from). [GitHub Actions](https://docs.github.com/en/actions) will automatically update to the next step. diff --git a/.github/steps/3-fix-security-vulnerabilities.md b/.github/steps/3-fix-security-vulnerabilities.md new file mode 100644 index 0000000..a8ccffc --- /dev/null +++ b/.github/steps/3-fix-security-vulnerabilities.md @@ -0,0 +1,41 @@ + + +## Step 3: Fix Security Vulnerabilities + +_Nice work finishing Step 2: Reviewing and Triaging CodeQL Alerts :sparkles:_ + +In this step, we will work to fix the existing security vulnerabilities already identified by CodeQL. Remember, at this point, we have introduced CodeQL into our repository and had it scan the existing code. The vulnerabilities it found are real-world issues, and they need to be fixed! We'll fix this issue by editing the `/server/routes.py` file. + +### :keyboard: Activity 1: Review alerts +First, before we fix these alerts, we need to make sure the alerts are still open. We'll also need to gather information on which files to fix and how best to fix them. + +1. Navigate to your code scanning alerts page: **Security** > **Code scanning**. +1. You should see two alerts listed as "**Open**". If any of the alerts are listed as "**Closed**", open the alert page and choose **Reopen alert**. + +Now that both of these alerts are open, let's fix them. If you look at the alerts, they both call out one specific file containing the issues: `server/routes.py`. The issue is in crafting the SQL query for the database. These queries are vulnerable to SQL injection attacks. We should rewrite these SQL statements more securely. + +If you expand the **More info** section at the bottom of the alert, there are very clear suggestions to fix this query. We're going to implement those suggestions in the next activity. + +### :keyboard: Activity 2: Edit routes.py +We now know where the issues exist and how to fix them. We'll start by modifying the file `routes.py`. Again, you'll want to do these next steps in a separate browser window or tab. + +1. Click the **Code** tab in your repository. +2. Select the `server` folder. +3. Select the `routes.py` file. +4. Click the **Edit** button to the right. + + ![edit-button.png](/images/edit-button.png) + +5. Edit line 16 by highlighting the SQL statement and replace it with this text: `"SELECT * FROM books WHERE name LIKE %s", name`. + +6. Edit line 22 to replace the SQL statement with this text: `"SELECT * FROM books WHERE author LIKE %s", author`. + +7. Click **Commit changes...** from the top right. The "Propose changes" window will pop up. Leave the defaults configured, and click **Commit changes** again. +8. CodeQL will now initiate a new scan. Check the status of that scan by navigating to **Actions** then choose the **CodeQL** action. Once the scan job completes, Actions will display a green check next to the last run. +9. Once that CodeQL scan is done, navigate to **Security** > **Code scanning** to review the alerts. You should have zero open alerts and two closed alerts πŸŽ‰. Feel free to review the closed alerts, especially the audit trail. +10. Wait about 20 seconds then refresh this page (the one you're following instructions from). [GitHub Actions](https://docs.github.com/en/actions) will automatically update to the next step. diff --git a/.github/steps/4-prevent-vulnerabilities-in-the-pull-request.md b/.github/steps/4-prevent-vulnerabilities-in-the-pull-request.md new file mode 100644 index 0000000..80a2cd9 --- /dev/null +++ b/.github/steps/4-prevent-vulnerabilities-in-the-pull-request.md @@ -0,0 +1,68 @@ + + +## Step 4: Prevent Vulnerabilities in the Pull Request + +_Nicely done! You finished Step 3: Fix Security Vulnerabilites! :partying_face:_ + +Way to go! You made it this far. We're almost done! The last step is to test out the pull request integration with CodeQL. In this step, we will add a vulnerability back into the `routes.py` file to trigger an alert for a SQL injection vulnerability. This is going to be the same issue initially saw. + +Our goal is to understand what developers experience when they find a new vulnerability. + +In this step, we will: +- edit the `routes.py` file. +- change the SQL statement to make it insecure. +- commit those changes and merge the insecure code into the main branch. +- experience the alert inside the pull request. + +Let's get started πŸ‘ + +**What is pull request**: Pull requests are proposed changes to a repository submitted by a user and accepted or rejected by a repository's collaborators. This allows multiple people to work on the same code at the same time. For more information, check out the GitHub Skills course "[Introduction to GitHub](https://github.com/skills/introduction-to-github)" or "[About pull requests](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests)" from the GitHub docs. + +**What is branch**: A branch is a parallel version of your repository. By default, your repository has one branch named main and it is considered to be the definitive branch. Creating additional branches allows you to copy the main branch of your repository and safely make any changes without disrupting the main project. For more information, see "[About branches](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-branches#)" in the GitHub docs. + +### :keyboard: Activity 1: Edit `routes.py` and create a new pull request + +In this first activity, we'll introduce the same insecure SQL statement from before to the `routes.py` file. Once we update the file, we'll commit it to a new branch, then create a pull request. + + 1. Click the **Code** tab in your repository. + 2. Select the `server` folder. + 3. Select the `routes.py` file. + 4. Click the **Edit** button to the right. + +![edit-button.png](/images/edit-button.png) + + 5. Edit line 16 by highlighting the SQL statement and replace it with this text: `"SELECT * FROM books WHERE name LIKE '%" + name + "%'"`. + 6. Click **Commit changes...** from the top right. The "Propose changes" window will pop up. + 7. This time, select the radio button next to **Create a new branch**. You can create a new name for this branch or leave it as the default suggestion. + 8. Click **Propose changes**. This opens a new pull request. + 9. In the "Open a pull request" window, click **Create pull request**. + + +### :keyboard: Activity 2: Review pull request + +At this point, we've edited the file `routes.py` to add our vulnerable code, committed those changes to our new branch, and created a pull request to merge the new branch into our `main` branch. These are the same steps a developer would take to introduce new, vulnerable code into a repository. + +Now, let's take a look at the pull request to see what the experience is like. + +1. In the previous activity, we created the pull request. After creating the pull request, you were brought directly to the pull request page. At the bottom of the pull request, you will see a check called "Code scanning/CodeQL". This is the CodeQL analysis job scanning the code introduced in the pull request. + +![pr-panel](/images/pr-panel.png) + +2. Once the check is complete, you will see a new comment in the pull request from CodeQL indicating a new security vulnerability; a SQL query built from user-controlled data. This is our SQL injection vulnerability. + + image + +3. Review the data flow paths by clicking **Show paths**. + +4. If you would like, add a comment and tag one of your friends by using their GitHub handle (example: `@username`). This will notify them that you made a comment on the issue and need their help solving the problem. πŸ˜„ + +If this were a real-world situation, the developer would fix the SQL statement in their branch. Once fixed, the vulnerability will automatically close out. + +If you would like to learn more about pull request integrations for code scanning, see "[Triaging code scanning alerts in pull requests](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/triaging-code-scanning-alerts-in-pull-requests)." + +5. Wait about 20 seconds then refresh this page (the one you're following instructions from). [GitHub Actions](https://docs.github.com/en/actions) will automatically update to the next step. diff --git a/.github/steps/X-finish.md b/.github/steps/X-finish.md new file mode 100644 index 0000000..39d2918 --- /dev/null +++ b/.github/steps/X-finish.md @@ -0,0 +1,26 @@ + + +## Finish + +_Congratulations! You have completed this course!_ πŸ† + +Here's a recap of all the tasks you accomplished in your repository: + - Enabled CodeQL on your repository. + - CodeQL scanned the code in your repository and notified us of two SQL injection vulenrabilities. + - Reviewed the findings, marked the findings as closed, and explored the audit trail. + - Fixed your code in the main branch and saw that the findings automatically closed out. + - Introduced a new vulnerability in a new branch. + - Created a pull request, and were notified of the vulnerability. + +### What's next? + +- Continue your learning! Our [code scanning documentation](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/about-code-scanning) is a great resource for learning more about CodeQL. +- Learn more about CodeQL. Take a look at the [CodeQL documenation](https://codeql.github.com/docs/) site to learn about all of the features of this powerful tool. +- [We'd love to hear what you thought of this course](https://github.com/orgs/skills/discussions/405). +- [Take another Skills course.](https://github.com/skills). +- [Read the GitHub Getting Started docs](https://docs.github.com/en/get-started). +- To find projects to contribute to, check out [GitHub Explore](https://github.com/explore). + diff --git a/.github/workflows/0-welcome.yml b/.github/workflows/0-welcome.yml new file mode 100644 index 0000000..df06d1d --- /dev/null +++ b/.github/workflows/0-welcome.yml @@ -0,0 +1,66 @@ +name: Step 0, Welcome + +# This step triggers after the learner creates a new repository from the template. +# This workflow updates from step 0 to step 1. + +# This will run every time we create push a commit to `main`. +# Reference: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + push: + branches: + - main + +# Reference: https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository. + # Need `contents: write` to update the step metadata. + contents: write + +jobs: + # Get the current step to only run the main job when the learner is on the same step. + get_current_step: + name: Check current step number + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - id: get_step + run: | + echo "current_step=$(cat ./.github/steps/-step.txt)" >> $GITHUB_OUTPUT + outputs: + current_step: ${{ steps.get_step.outputs.current_step }} + + on_start: + name: On start + needs: get_current_step + + # We will only run this action when: + # 1. This repository isn't the template repository. + # 2. The step is currently 0. + # Reference: https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference: https://docs.github.com/en/actions/learn-github-actions/expressions + if: >- + ${{ !github.event.repository.is_template + && needs.get_current_step.outputs.current_step == 0 }} + + # We'll run Ubuntu for performance instead of Mac or Windows. + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README. + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Let's get all the branches. + + # TBD-course-set-up + + # In README.md, switch step 0 for step 1. + - name: Update to step 1 + uses: skills/action-update-step@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 0 + to_step: 1 + branch_name: tbd-branch-name diff --git a/.github/workflows/1-enable-codeql.yml b/.github/workflows/1-enable-codeql.yml new file mode 100644 index 0000000..e34aa7b --- /dev/null +++ b/.github/workflows/1-enable-codeql.yml @@ -0,0 +1,68 @@ +name: Step 1, Enable CodeQL + +# This step triggers after enabling codeql. +# This workflow updates from step 1 to step 2. + +# This will run every time we enable codeql. +# Reference: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + # Add events that trigger this workflow. + workflow_run: + workflows: [CodeQL] + types: + - in_progress + +# Reference: https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository. + # Need `contents: write` to update the step metadata. + contents: write + +jobs: + # Get the current step to only run the main job when the learner is on the same step. + get_current_step: + name: Check current step number + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - id: get_step + run: | + echo "current_step=$(cat ./.github/steps/-step.txt)" >> $GITHUB_OUTPUT + outputs: + current_step: ${{ steps.get_step.outputs.current_step }} + + on_enable-codeql: + name: On enable codeql + needs: get_current_step + + # We will only run this action when: + # 1. This repository isn't the template repository. + # 2. The step is currently 1. + # Reference: https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference: https://docs.github.com/en/actions/learn-github-actions/expressions + if: >- + ${{ !github.event.repository.is_template + && needs.get_current_step.outputs.current_step == 1 }} + + # We'll run Ubuntu for performance instead of Mac or Windows. + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README. + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Let's get all the branches. + + # enable-codeql-additional-steps + + # In README.md, switch step 1 for step 2. + - name: Update to step 2 + uses: skills/action-update-step@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 1 + to_step: 2 + branch_name: tbd-branch-name diff --git a/.github/workflows/2-review-and-triage-codeql-alerts.yml b/.github/workflows/2-review-and-triage-codeql-alerts.yml new file mode 100644 index 0000000..2cb3c37 --- /dev/null +++ b/.github/workflows/2-review-and-triage-codeql-alerts.yml @@ -0,0 +1,66 @@ +name: Step 2, review-and-triage-codeql-alerts + +# This step triggers after submitting a new issues after review and triage. +# This workflow updates from step 2 to step 3. + +# This will run every time we review-and-triage-codeql-alerts-event-desc. +# Reference: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + # Add events that trigger this workflow. + issues: + types: [opened] + +# Reference: https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository. + # Need `contents: write` to update the step metadata. + contents: write + +jobs: + # Get the current step to only run the main job when the learner is on the same step. + get_current_step: + name: Check current step number + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - id: get_step + run: | + echo "current_step=$(cat ./.github/steps/-step.txt)" >> $GITHUB_OUTPUT + outputs: + current_step: ${{ steps.get_step.outputs.current_step }} + + on_review-and-triage-codeql-alerts-event: + name: On review and triage codeql alerts + needs: get_current_step + + # We will only run this action when: + # 1. This repository isn't the template repository. + # 2. The step is currently 2. + # Reference: https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference: https://docs.github.com/en/actions/learn-github-actions/expressions + if: >- + ${{ !github.event.repository.is_template + && needs.get_current_step.outputs.current_step == 2 }} + + # We'll run Ubuntu for performance instead of Mac or Windows. + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README. + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Let's get all the branches. + + # review-and-triage-codeql-alerts-additional-steps + + # In README.md, switch step 2 for step 3. + - name: Update to step 3 + uses: skills/action-update-step@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 2 + to_step: 3 + branch_name: tbd-branch-name diff --git a/.github/workflows/3-fix-security-vulnerabilities.yml b/.github/workflows/3-fix-security-vulnerabilities.yml new file mode 100644 index 0000000..9bbcacd --- /dev/null +++ b/.github/workflows/3-fix-security-vulnerabilities.yml @@ -0,0 +1,67 @@ +name: Step 3, fix-security-vulnerabilities + +# This step triggers after fix-security-vulnerabilities-event-desc. +# This workflow updates from step 3 to step 4. + +# This will run every time we fix-security-vulnerabilities-event-desc. +# Reference: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + # Add events that trigger this workflow. + push: + paths: + - server/routes.py + +# Reference: https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository. + # Need `contents: write` to update the step metadata. + contents: write + +jobs: + # Get the current step to only run the main job when the learner is on the same step. + get_current_step: + name: Check current step number + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - id: get_step + run: | + echo "current_step=$(cat ./.github/steps/-step.txt)" >> $GITHUB_OUTPUT + outputs: + current_step: ${{ steps.get_step.outputs.current_step }} + + on_fix-security-vulnerabilities-event: + name: On fix security vulnerabilities + needs: get_current_step + + # We will only run this action when: + # 1. This repository isn't the template repository. + # 2. The step is currently 3. + # Reference: https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference: https://docs.github.com/en/actions/learn-github-actions/expressions + if: >- + ${{ !github.event.repository.is_template + && needs.get_current_step.outputs.current_step == 3 }} + + # We'll run Ubuntu for performance instead of Mac or Windows. + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README. + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Let's get all the branches. + + # fix-security-vulnerabilities-additional-steps + + # In README.md, switch step 3 for step 4. + - name: Update to step 4 + uses: skills/action-update-step@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 3 + to_step: 4 + branch_name: tbd-branch-name diff --git a/.github/workflows/4-prevent-vulnerabilities-in-the-pull-request.yml b/.github/workflows/4-prevent-vulnerabilities-in-the-pull-request.yml new file mode 100644 index 0000000..5e8026d --- /dev/null +++ b/.github/workflows/4-prevent-vulnerabilities-in-the-pull-request.yml @@ -0,0 +1,66 @@ +name: Step 4, prevent-vulnerabilities-in-the-pull-request + +# This step triggers after prevent-vulnerabilities-in-the-pull-request-event-desc. +# This workflow updates from step 4 to step 5. + +# This will run every time we prevent-vulnerabilities-in-the-pull-request-event-desc. +# Reference: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + # Add events that trigger this workflow + pull_request: + types: opened + +# Reference: https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository. + # Need `contents: write` to update the step metadata. + contents: write + +jobs: + # Get the current step to only run the main job when the learner is on the same step. + get_current_step: + name: Check current step number + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - id: get_step + run: | + echo "current_step=$(cat ./.github/steps/-step.txt)" >> $GITHUB_OUTPUT + outputs: + current_step: ${{ steps.get_step.outputs.current_step }} + + on_prevent-vulnerabilities-in-the-pull-request-event: + name: On prevent vulnerabilities in the pull request + needs: get_current_step + + # We will only run this action when: + # 1. This repository isn't the template repository. + # 2. The step is currently 4. + # Reference: https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference: https://docs.github.com/en/actions/learn-github-actions/expressions + if: >- + ${{ !github.event.repository.is_template + && needs.get_current_step.outputs.current_step == 4 }} + + # We'll run Ubuntu for performance instead of Mac or Windows. + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README. + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Let's get all the branches. + + # prevent-vulnerabilities-in-the-pull-request-additional-steps + + # In README.md, switch step 4 for step 5. + - name: Update to step 5 + uses: skills/action-update-step@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 4 + to_step: X + branch_name: tbd-branch-name diff --git a/.github/workflows/5-tbd.yml b/.github/workflows/5-tbd.yml new file mode 100644 index 0000000..08e34ff --- /dev/null +++ b/.github/workflows/5-tbd.yml @@ -0,0 +1,65 @@ +name: Step 5, TBD-step-5-name + +# This step triggers after TBD-step-5-event-desc. +# This workflow updates from step 5 to step X. + +# This will run every time we TBD-step-5-event-desc. +# Reference: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + # Add events that trigger this workflow. + # TBD-step-5-event: + +# Reference: https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository. + # Need `contents: write` to update the step metadata. + contents: write + +jobs: + # Get the current step to only run the main job when the learner is on the same step. + get_current_step: + name: Check current step number + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - id: get_step + run: | + echo "current_step=$(cat ./.github/steps/-step.txt)" >> $GITHUB_OUTPUT + outputs: + current_step: ${{ steps.get_step.outputs.current_step }} + + on_TBD-step-5-event: + name: On TBD-step-5-event + needs: get_current_step + + # We will only run this action when: + # 1. This repository isn't the template repository. + # 2. The step is currently 5. + # Reference: https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference: https://docs.github.com/en/actions/learn-github-actions/expressions + if: >- + ${{ !github.event.repository.is_template + && needs.get_current_step.outputs.current_step == 5 }} + + # We'll run Ubuntu for performance instead of Mac or Windows. + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README. + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Let's get all the branches. + + # TBD-step-5-additional-steps + + # In README.md, switch step 5 for step X. + - name: Update to step X + uses: skills/action-update-step@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 5 + to_step: X + branch_name: tbd-branch-name diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..773bfd6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +# Compiled source # +################### +*.com +*.class +*.dll +*.exe +*.o +*.so + +# Packages # +############ +# it's better to unpack these files and commit the raw source +# git has its own built in compression methods +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Logs and databases # +###################### +*.log +*.sql +*.sqlite + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6c5bc3d --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) GitHub, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the β€œSoftware”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED β€œAS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..c30274b --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +flask = "*" +python-dotenv = "*" + +[dev-packages] + +[requires] +python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..286dc0a --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,156 @@ +{ + "_meta": { + "hash": { + "sha256": "069f33d2dc75b242fa5ee44daf090c80831812dc6cc59824e94c22a677eac958" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.8" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "click": { + "hashes": [ + "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28", + "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de" + ], + "markers": "python_version >= '3.7'", + "version": "==8.1.7" + }, + "flask": { + "hashes": [ + "sha256:58107ed83443e86067e41eff4631b058178191a355886f8e479e347fa1285fdf", + "sha256:edee9b0a7ff26621bd5a8c10ff484ae28737a2410d99b0bb9a6850c7fb977aa0" + ], + "index": "pypi", + "markers": "python_version >= '3.7'", + "version": "==2.2.5" + }, + "importlib-metadata": { + "hashes": [ + "sha256:3ebb78df84a805d7698245025b975d9d67053cd94c79245ba4b3eb694abe68bb", + "sha256:dbace7892d8c0c4ac1ad096662232f831d4e64f4c4545bd53016a3e9d4654743" + ], + "markers": "python_version < '3.10'", + "version": "==6.8.0" + }, + "itsdangerous": { + "hashes": [ + "sha256:2c2349112351b88699d8d4b6b075022c0808887cb7ad10069318a8b0bc88db44", + "sha256:5dbbc68b317e5e42f327f9021763545dc3fc3bfe22e6deb96aaf1fc38874156a" + ], + "markers": "python_version >= '3.7'", + "version": "==2.1.2" + }, + "jinja2": { + "hashes": [ + "sha256:4a3aee7acbbe7303aede8e9648d13b8bf88a429282aa6122a993f0ac800cb369", + "sha256:bc5dd2abb727a5319567b7a813e6a2e7318c39f4f487cfe6c89c6f9c7d25197d" + ], + "index": "pypi", + "markers": "python_version >= '3.7'", + "version": "==3.1.4" + }, + "markupsafe": { + "hashes": [ + "sha256:00e046b6dd71aa03a41079792f8473dc494d564611a8f89bbbd7cb93295ebdcf", + "sha256:075202fa5b72c86ad32dc7d0b56024ebdbcf2048c0ba09f1cde31bfdd57bcfff", + "sha256:0e397ac966fdf721b2c528cf028494e86172b4feba51d65f81ffd65c63798f3f", + "sha256:17b950fccb810b3293638215058e432159d2b71005c74371d784862b7e4683f3", + "sha256:1f3fbcb7ef1f16e48246f704ab79d79da8a46891e2da03f8783a5b6fa41a9532", + "sha256:2174c595a0d73a3080ca3257b40096db99799265e1c27cc5a610743acd86d62f", + "sha256:2b7c57a4dfc4f16f7142221afe5ba4e093e09e728ca65c51f5620c9aaeb9a617", + "sha256:2d2d793e36e230fd32babe143b04cec8a8b3eb8a3122d2aceb4a371e6b09b8df", + "sha256:30b600cf0a7ac9234b2638fbc0fb6158ba5bdcdf46aeb631ead21248b9affbc4", + "sha256:397081c1a0bfb5124355710fe79478cdbeb39626492b15d399526ae53422b906", + "sha256:3a57fdd7ce31c7ff06cdfbf31dafa96cc533c21e443d57f5b1ecc6cdc668ec7f", + "sha256:3c6b973f22eb18a789b1460b4b91bf04ae3f0c4234a0a6aa6b0a92f6f7b951d4", + "sha256:3e53af139f8579a6d5f7b76549125f0d94d7e630761a2111bc431fd820e163b8", + "sha256:4096e9de5c6fdf43fb4f04c26fb114f61ef0bf2e5604b6ee3019d51b69e8c371", + "sha256:4275d846e41ecefa46e2015117a9f491e57a71ddd59bbead77e904dc02b1bed2", + "sha256:4c31f53cdae6ecfa91a77820e8b151dba54ab528ba65dfd235c80b086d68a465", + "sha256:4f11aa001c540f62c6166c7726f71f7573b52c68c31f014c25cc7901deea0b52", + "sha256:5049256f536511ee3f7e1b3f87d1d1209d327e818e6ae1365e8653d7e3abb6a6", + "sha256:58c98fee265677f63a4385256a6d7683ab1832f3ddd1e66fe948d5880c21a169", + "sha256:598e3276b64aff0e7b3451b72e94fa3c238d452e7ddcd893c3ab324717456bad", + "sha256:5b7b716f97b52c5a14bffdf688f971b2d5ef4029127f1ad7a513973cfd818df2", + "sha256:5dedb4db619ba5a2787a94d877bc8ffc0566f92a01c0ef214865e54ecc9ee5e0", + "sha256:619bc166c4f2de5caa5a633b8b7326fbe98e0ccbfacabd87268a2b15ff73a029", + "sha256:629ddd2ca402ae6dbedfceeba9c46d5f7b2a61d9749597d4307f943ef198fc1f", + "sha256:656f7526c69fac7f600bd1f400991cc282b417d17539a1b228617081106feb4a", + "sha256:6ec585f69cec0aa07d945b20805be741395e28ac1627333b1c5b0105962ffced", + "sha256:72b6be590cc35924b02c78ef34b467da4ba07e4e0f0454a2c5907f473fc50ce5", + "sha256:7502934a33b54030eaf1194c21c692a534196063db72176b0c4028e140f8f32c", + "sha256:7a68b554d356a91cce1236aa7682dc01df0edba8d043fd1ce607c49dd3c1edcf", + "sha256:7b2e5a267c855eea6b4283940daa6e88a285f5f2a67f2220203786dfa59b37e9", + "sha256:823b65d8706e32ad2df51ed89496147a42a2a6e01c13cfb6ffb8b1e92bc910bb", + "sha256:8590b4ae07a35970728874632fed7bd57b26b0102df2d2b233b6d9d82f6c62ad", + "sha256:8dd717634f5a044f860435c1d8c16a270ddf0ef8588d4887037c5028b859b0c3", + "sha256:8dec4936e9c3100156f8a2dc89c4b88d5c435175ff03413b443469c7c8c5f4d1", + "sha256:97cafb1f3cbcd3fd2b6fbfb99ae11cdb14deea0736fc2b0952ee177f2b813a46", + "sha256:a17a92de5231666cfbe003f0e4b9b3a7ae3afb1ec2845aadc2bacc93ff85febc", + "sha256:a549b9c31bec33820e885335b451286e2969a2d9e24879f83fe904a5ce59d70a", + "sha256:ac07bad82163452a6884fe8fa0963fb98c2346ba78d779ec06bd7a6262132aee", + "sha256:ae2ad8ae6ebee9d2d94b17fb62763125f3f374c25618198f40cbb8b525411900", + "sha256:b91c037585eba9095565a3556f611e3cbfaa42ca1e865f7b8015fe5c7336d5a5", + "sha256:bc1667f8b83f48511b94671e0e441401371dfd0f0a795c7daa4a3cd1dde55bea", + "sha256:bec0a414d016ac1a18862a519e54b2fd0fc8bbfd6890376898a6c0891dd82e9f", + "sha256:bf50cd79a75d181c9181df03572cdce0fbb75cc353bc350712073108cba98de5", + "sha256:bff1b4290a66b490a2f4719358c0cdcd9bafb6b8f061e45c7a2460866bf50c2e", + "sha256:c061bb86a71b42465156a3ee7bd58c8c2ceacdbeb95d05a99893e08b8467359a", + "sha256:c8b29db45f8fe46ad280a7294f5c3ec36dbac9491f2d1c17345be8e69cc5928f", + "sha256:ce409136744f6521e39fd8e2a24c53fa18ad67aa5bc7c2cf83645cce5b5c4e50", + "sha256:d050b3361367a06d752db6ead6e7edeb0009be66bc3bae0ee9d97fb326badc2a", + "sha256:d283d37a890ba4c1ae73ffadf8046435c76e7bc2247bbb63c00bd1a709c6544b", + "sha256:d9fad5155d72433c921b782e58892377c44bd6252b5af2f67f16b194987338a4", + "sha256:daa4ee5a243f0f20d528d939d06670a298dd39b1ad5f8a72a4275124a7819eff", + "sha256:db0b55e0f3cc0be60c1f19efdde9a637c32740486004f20d1cff53c3c0ece4d2", + "sha256:e61659ba32cf2cf1481e575d0462554625196a1f2fc06a1c777d3f48e8865d46", + "sha256:ea3d8a3d18833cf4304cd2fc9cbb1efe188ca9b5efef2bdac7adc20594a0e46b", + "sha256:ec6a563cff360b50eed26f13adc43e61bc0c04d94b8be985e6fb24b81f6dcfdf", + "sha256:f5dfb42c4604dddc8e4305050aa6deb084540643ed5804d7455b5df8fe16f5e5", + "sha256:fa173ec60341d6bb97a89f5ea19c85c5643c1e7dedebc22f5181eb73573142c5", + "sha256:fa9db3f79de01457b03d4f01b34cf91bc0048eb2c3846ff26f66687c2f6d16ab", + "sha256:fce659a462a1be54d2ffcacea5e3ba2d74daa74f30f5f143fe0c58636e355fdd", + "sha256:ffee1f21e5ef0d712f9033568f8344d5da8cc2869dbd08d87c84656e6a2d2f68" + ], + "markers": "python_version >= '3.7'", + "version": "==2.1.5" + }, + "python-dotenv": { + "hashes": [ + "sha256:aae25dc1ebe97c420f50b81fb0e5c949659af713f31fdb63c749ca68748f34b1", + "sha256:f521bc2ac9a8e03c736f62911605c5d83970021e3fa95b37d769e2bbbe9b6172" + ], + "index": "pypi", + "markers": "python_version >= '3.5'", + "version": "==0.19.0" + }, + "werkzeug": { + "hashes": [ + "sha256:097e5bfda9f0aba8da6b8545146def481d06aa7d3266e7448e2cccf67dd8bd18", + "sha256:fc9645dc43e03e4d630d23143a04a7f947a9a3b5727cd535fdfe155a17cc48c8" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==3.0.3" + }, + "zipp": { + "hashes": [ + "sha256:2828e64edb5386ea6a52e7ba7cdb17bb30a73a858f5eb6eb93d8d36f5ea26091", + "sha256:35427f6d5594f4acf82d25541438348c26736fa9b3afa2754bcd63cdb99d8e8f" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==3.19.1" + } + }, + "develop": {} +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..f607bf5 --- /dev/null +++ b/README.md @@ -0,0 +1,76 @@ +
+ + + +# Enable CodeQL to secure your source code + +_Ensuring the security of application source code is a critical step in modern software development. In this GitHub Skills course, you will learn to use GitHub code scanning to identify, resolve, and prevent insecure coding patterns._ + +
+ + + +## Welcome + +_Welcome to "Introduction to CodeQL"! :wave:_ + +In this course, we will explore using GitHub code scanning, powered by [CodeQL](https://codeql.github.com/), to identify common coding practices that can lead to security vulnerabilities. During this course, we will enable code scanning on your repository to identify, remediate, and prevent vulnerabilities. + +Code scanning is part of the [GitHub Advanced Security (GHAS)](https://docs.github.com/en/get-started/learning-about-github/about-github-advanced-security) product suite. All of the features of Advanced Security are 100% free for open source, public repositories. + +- **Who is this for**: Developers, security engineers, open source maintainers. +- **What you'll learn**: We'll show you how to enable code scanning and identify SQL injection vulnerabilities with CodeQL. +- **What you'll build**: A secure software development pipeline that allows you to identify and prevent new security vulnerabilities from being introduced into your production code. +- **Prerequisites**: In this course, you'll need a baseline knowledge of GitHub concepts such as pull requests, GitHub Actions, and source code. You'll also need to be familiar with the concepts of Static Application Security Testing (SAST). Don't worry, we'll demistify the complex parts for you πŸ™‚. +- **How long**: This course is four steps long and takes less than 30 minutes to complete. + +## How to start this course + + + +[![start-course](https://user-images.githubusercontent.com/1221423/235727646-4a590299-ffe5-480d-8cd5-8194ea184546.svg)](https://github.com/new?template_owner=skills&template_name=introduction-to-codeql&owner=%40me&name=skills-introduction-to-codeql&description=GitHub+Skills:+Introduction+to+CodeQL&visibility=public) + +1. Right-click **Start course** and open the link in a new tab. +2. In the new tab, most of the prompts will automatically fill in for you. + - For owner, choose your personal account or an organization to host the repository. + - We recommend creating a public repository, as private repositories will [use Actions minutes](https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions). + - Scroll down and click the **Create repository** button at the bottom of the form. +3. After your new repository is created, wait about 20 seconds, then refresh the page. Follow the step-by-step instructions in the new repository's README. + + diff --git a/fixme b/fixme new file mode 100644 index 0000000..193477e --- /dev/null +++ b/fixme @@ -0,0 +1,30 @@ + +from flask import request, render_template, make_response + +from server.webapp import flaskapp, cursor +from server.models import Book + + +@flaskapp.route('/') +def index(): + name = request.args.get('name') + author = request.args.get('author') + read = bool(request.args.get('read')) + + if name: + cursor.execute( + "SELECT * FROM books WHERE name LIKE :name", {'name': f"%{name}%"} + ) + books = [Book(*row) for row in cursor] + + elif author: + cursor.execute( + "SELECT * FROM books WHERE author LIKE :author", {'author': f"%{author}%"} + ) + books = [Book(*row) for row in cursor] + + else: + cursor.execute("SELECT name, author, read FROM books") + books = [Book(*row) for row in cursor] + + return render_template('books.html', books=books) diff --git a/images/additiona-information.png b/images/additiona-information.png new file mode 100644 index 0000000..47034e7 Binary files /dev/null and b/images/additiona-information.png differ diff --git a/images/alert-status.png b/images/alert-status.png new file mode 100644 index 0000000..75d2006 Binary files /dev/null and b/images/alert-status.png differ diff --git a/images/audit-trail.png b/images/audit-trail.png new file mode 100644 index 0000000..732c00c Binary files /dev/null and b/images/audit-trail.png differ diff --git a/images/codeql-default-configuration-box.png b/images/codeql-default-configuration-box.png new file mode 100644 index 0000000..e438832 Binary files /dev/null and b/images/codeql-default-configuration-box.png differ diff --git a/images/codeql-setup.png b/images/codeql-setup.png new file mode 100644 index 0000000..487f73a Binary files /dev/null and b/images/codeql-setup.png differ diff --git a/images/edit-button.png b/images/edit-button.png new file mode 100644 index 0000000..9379968 Binary files /dev/null and b/images/edit-button.png differ diff --git a/images/enable-code-scanning-default.png b/images/enable-code-scanning-default.png new file mode 100644 index 0000000..d1ffd75 Binary files /dev/null and b/images/enable-code-scanning-default.png differ diff --git a/images/location-information.png b/images/location-information.png new file mode 100644 index 0000000..92f0d78 Binary files /dev/null and b/images/location-information.png differ diff --git a/images/one-closed-alert.png b/images/one-closed-alert.png new file mode 100644 index 0000000..f93b104 Binary files /dev/null and b/images/one-closed-alert.png differ diff --git a/images/pr-panel.png b/images/pr-panel.png new file mode 100644 index 0000000..3fca9fb Binary files /dev/null and b/images/pr-panel.png differ diff --git a/images/recommendations.png b/images/recommendations.png new file mode 100644 index 0000000..97b22a0 Binary files /dev/null and b/images/recommendations.png differ diff --git a/images/vulnerabilities-alert-code-scanning.png b/images/vulnerabilities-alert-code-scanning.png new file mode 100644 index 0000000..15a85e0 Binary files /dev/null and b/images/vulnerabilities-alert-code-scanning.png differ diff --git a/server/__init__.py b/server/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/__main__.py b/server/__main__.py new file mode 100644 index 0000000..a2aa005 --- /dev/null +++ b/server/__main__.py @@ -0,0 +1,36 @@ +import os +import sys +sys.path.append('.') + +from server.webapp import flaskapp, database, cursor, TEMPLATES +from server.models import * +from server.routes import * + +default_books = [ + ("The Hobbit", "JRR Tolkien", True), + ("The Fellowship of the Ring", "JRR Tolkien", True), + ("The Eye of the World", "Robert Jordan", False), + ("A Game of Thrones", "George R. R. Martin", True), + ("The Way of Kings", "Brandon Sanderson", False) +] + + +if __name__ == "__main__": + cursor.execute( + '''CREATE TABLE books (name text, author text, read text)''' + ) + + for bookname, bookauthor, hasread in default_books: + try: + cursor.execute( + 'INSERT INTO books values (?, ?, ?)', + (bookname, bookauthor, 'true' if hasread else 'false') + ) + + except Exception as err: + print(f'[!] Error Occurred: {err}') + + flaskapp.run('0.0.0.0', debug=bool(os.environ.get('DEBUG', False))) + + cursor.close() + database.close() diff --git a/server/models/__init__.py b/server/models/__init__.py new file mode 100644 index 0000000..4b08c41 --- /dev/null +++ b/server/models/__init__.py @@ -0,0 +1,2 @@ + +from server.models.books import Book diff --git a/server/models/books.py b/server/models/books.py new file mode 100644 index 0000000..ffc0a84 --- /dev/null +++ b/server/models/books.py @@ -0,0 +1,8 @@ + +from dataclasses import dataclass + +@dataclass +class Book(): + name: str + author: str + read: bool diff --git a/server/routes.py b/server/routes.py new file mode 100644 index 0000000..67a18af --- /dev/null +++ b/server/routes.py @@ -0,0 +1,30 @@ + +from flask import request, render_template, make_response + +from server.webapp import flaskapp, cursor +from server.models import Book + + +@flaskapp.route('/') +def index(): + name = request.args.get('name') + author = request.args.get('author') + read = bool(request.args.get('read')) + + if name: + cursor.execute( + "SELECT * FROM books WHERE name LIKE '%" + name + "%'" + ) + books = [Book(*row) for row in cursor] + + elif author: + cursor.execute( + "SELECT * FROM books WHERE author LIKE '%" + author + "%'" + ) + books = [Book(*row) for row in cursor] + + else: + cursor.execute("SELECT name, author, read FROM books") + books = [Book(*row) for row in cursor] + + return render_template('books.html', books=books) diff --git a/server/webapp.py b/server/webapp.py new file mode 100644 index 0000000..a7edb0b --- /dev/null +++ b/server/webapp.py @@ -0,0 +1,16 @@ +import os +import sqlite3 + +from flask import Flask + + +ROOT = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) +TEMPLATES = os.path.join(ROOT, 'templates') + +flaskapp = Flask("BookStore", template_folder=TEMPLATES) +flaskapp.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +database_uri = os.environ.get('SQLITE_URI', ':memory:') + +database = sqlite3.connect(database_uri, check_same_thread=False) +cursor = database.cursor() diff --git a/templates/books.html b/templates/books.html new file mode 100644 index 0000000..987779e --- /dev/null +++ b/templates/books.html @@ -0,0 +1,14 @@ + + + + Book Store + + +

Book Store

+ + + \ No newline at end of file