Semgrep not detecting variant of SQL injection and Insecure Random

Summary

Semgrep SAST rule doesn't detect variants of SQL injection and Insecure Randomness vulnerability.

Steps to reproduce

  1. Create an empty project
  2. Use the example files in the project to run pipeline
  3. Wait for pipeline to finish and see that there's no vulnerabilities detected
.gitlab-ci.yml
stages:
- test

include:
- template: Security/SAST.gitlab-ci.yml

sast:
  stage: test
  artifacts:
    paths: [ "gl-sast-report.json" ]
    reports:
      sast: gl-sast-report.json
code.java
package org.example;

import javax.crypto.spec.SecretKeySpec;
import javax.sql.DataSource;
import java.security.Key;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

public class Issue {

    private DataSource dataSource;

    protected String injectableQuery(String accountName) {
        try (Connection connection = dataSource.getConnection()) {
            String query = "SELECT * FROM user_data WHERE first_name = 'John' and last_name = '" + accountName + "'";
            try (Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
                ResultSet results = statement.executeQuery(query);
                return results.getCursorName() + getRandomKey(results.getCursorName(), 42);
            } catch (SQLException sqle) {
                return "very bad";
            }
        } catch (Exception e) {
            return "bad";
        }
    }

    private static Key getRandomKey(String cipher, int keySize) {
        byte[] randomKeyBytes = new byte[keySize / 8];
        Random random = new Random();
        random.nextBytes(randomKeyBytes);
        return new SecretKeySpec(randomKeyBytes, cipher);
    }
}

Example Project

What is the current bug behavior?

The semgrep scan didn't flag the following code as vulnerable

  • String query = "SELECT * FROM user_data WHERE first_name = 'John' and last_name = '" + accountName + "'";
  • Random random = new Random();

What is the expected correct behavior?

Relevant logs and/or screenshots

Output of checks

Results of GitLab environment info

Expand for output related to GitLab environment info
(For installations with omnibus-gitlab package run and paste the output of:
`sudo gitlab-rake gitlab:env:info`)

(For installations from source run and paste the output of:
`sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production`)

Results of GitLab application Check

Expand for output related to the GitLab application check

(For installations with omnibus-gitlab package run and paste the output of: sudo gitlab-rake gitlab:check SANITIZE=true)

(For installations from source run and paste the output of: sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true)

(we will only investigate if the tests are passing)

Possible fixes