Skip to content

GitHub Advanced Security Code Scanning demo using WebGoat, a deliberate insecure web application maintained by OWASP designed to teach web application security lessons.

License

Notifications You must be signed in to change notification settings

octo-faq/advanced-security-java-webgoat

 
 

Repository files navigation

Screenshot 2021-05-18 at 11 06 46

Code Scanning Java Tutorial

Introduction

Code scanning is a feature of GitHub Advanced Security that you use to analyze the code in a GitHub repository to find security vulnerabilities in your software. Any problems identified by the analysis are shown in GitHub.

For this demo we'll make use of the WebGoat, a deliberate insecure web application maintained by OWASP designed to teach web application security lessons.

Instructions

Fork this repo

Begin by forking this repo.

Enable Code Scanning

Security tab

Click on the Security tab.

Screenshot 2021-05-18 at 16 44 28

Set up code scanning

Setup Workflow

Click the Setup this workflow button by CodeQL Analysis.

This will create a GitHub Actions Workflow file with CodeQL already set up. Since Javascript is an interpreted language there is no need to configure any builds. See the documentation if you would like to configure CodeQL Analysis with a 3rd party CI system instead of using GitHub Actions.

Actions Workflow file

Actions Workflow

The Actions Workflow file contains a number of different sections including:

  1. Checking out the repository
  2. Initializing the CodeQL Action
  3. Running a build (or use autobuilder)
  4. Running the CodeQL Analysis

For this demo we'll manually run the build steps of this application. Comment out the autobuild step and copy paste the following snippet to build this application:

    - name: Set up JDK 15
      uses: actions/setup-java@v2
      with:
        distribution: 'zulu'
        java-version: 15
        architecture: x64
    - name: Build with Maven
      run: mvn clean install
Screenshot 2021-05-18 at 16 49 46

Click Start Commit -> Commit this file to commit the changes to develop branch.

Workflow triggers

Workflow triggers

There are a number of events that can trigger a GitHub Actions workflow. In this example, the workflow will be triggered on

  • push to main branch
  • pull request to merge to main branch
  • on schedule, at 6:33 every Thursday

Setting up the new CodeQL workflow and committing it to main branch in the step above will trigger the scan.

GitHub Actions Progress

GitHub Actions Progress

Click Actions tab -> CodeQL

Click the specific workflow run. You can view the progress of the Workflow run until the analysis completes.

Security Issues

Once the Workflow has completed (this will take about 5 minutes), click the Security tab -> Code Scanning Alerts. An security alert "Resolving XML external entity in user-controlled data)" should be visible.

Security Alert View

Clicking on the security alert will provide details about the security alert including: A description of the issue A tag to the CWE that it is connected to as well as the type of alert (Error, Warning, Note) The line of code that triggered the security alert The ability to dismiss the alert depending on certain conditions (false positive? won't fix? used in tests?)

Screenshot 2021-05-18 at 16 55 27

Security Alert Description

Click Show more to view a full desciption of the alert including examples and links to additional information.

Screenshot 2021-05-18 at 16 56 29

Security Full Description

Screenshot 2021-05-18 at 16 57 02

Show Paths

Show Paths Button

CodeQL Analysis is able to trace the dataflow path from source to sink and gives you the ability to view the path traversal within the alert.

Click show paths in order to see the dataflow path that resulted in this alert.

Screenshot 2021-05-18 at 16 57 25

Show Paths View

Screenshot 2021-05-18 at 16 57 50

Fix the Security Alert

In order to fix this specific alert, we will need to ensure disable the parsing of this user-controlled data.

Click on the Code tab and Edit the webgoat-lessons/xxe/src/main/java/org/owasp/webgoat/xxe/Comments.java file. Navigate to Line 97 of the `Comments.java file and modify the line:

var xsr = xif.createXMLStreamReader(new StringReader(xml));

to

xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
xif.setProperty("javax.xml.stream.isSupportingExternalEntities", false);
var xsr = xif.createXMLStreamReader(new StringReader(xml));

Click Create a new branch for this commit and start a pull request, name the branch fix-xml-reader, and create the Pull Request.

Pull Request Status Check

In the Pull Request, you will notice that the CodeQL Analysis has started as a status check. Wait until it completes.

Screenshot 2021-05-19 at 09 41 53

Security Alert Details

After the Workflow has completed click on Details by the Code Scanning Results / CodeQL status check.

Screenshot 2021-05-19 at 09 46 18

Fixed Alert

Notice that Code Scanning has detected that this Pull Request will fix the vulnerability that was detected before.

Screenshot 2021-05-19 at 09 46 53

Merge the Pull Request. After the Pull Request has been merged, another Workflow will kick off to scan the repository for any vulnerabilties.

Closed Security Alerts

After the final Workflow has completed, navigate back to the Security tab and click Closed. Notice that the XML external identity alert now shows up as a closed issue.

Screenshot 2021-05-19 at 09 53 21

Traceability

Click on the security alert and notice that it details when the fix was made, by whom, and the specific commit. This provides full traceability to detail when and how a security alert was fixed and exactly what was changed to remediate the issue.

Screenshot 2021-05-19 at 09 54 01

Next Steps

Ready to talk about advanced security features for GitHub Enterprise? Contact Sales for more information!

Check out GitHub's Security feature page for more security features embedded into GitHub.

Check out the Code Scanning documentation for additional configuration options and technical details.

About

GitHub Advanced Security Code Scanning demo using WebGoat, a deliberate insecure web application maintained by OWASP designed to teach web application security lessons.

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • JavaScript 49.1%
  • Java 36.4%
  • HTML 10.8%
  • CSS 3.6%
  • Other 0.1%