scala.lang.security.audit.dangerous-seq-run.dangerous-seq-run

profile photo of semgrepsemgrep
Author
unknown
Download Count*

Found dynamic content used for the external process. This is dangerous if arbitrary data can reach this function call because it allows a malicious actor to execute commands. Ensure your variables are not controlled by users or sufficiently sanitized.

Run Locally

Run in CI

Defintion

rules:
  - id: dangerous-seq-run
    patterns:
      - pattern: Seq($CMD, ...)
      - pattern-not: Seq("...", ...)
      - pattern-inside: |
          import sys.process
          ...
      - pattern-not-inside: |
          $CMD = "..."
          ...
      - pattern-either:
          - pattern-inside: Seq(...).!
          - pattern-inside: Seq(...).!!
          - pattern-inside: Seq(...).lazyLines
    message: Found dynamic content used for the external process. This is dangerous
      if arbitrary data can reach this function call because it allows a
      malicious actor to execute commands. Ensure your variables are not
      controlled by users or sufficiently sanitized.
    languages:
      - scala
    severity: ERROR
    metadata:
      category: security
      cwe:
        - "CWE-78: Improper Neutralization of Special Elements used in an OS
          Command ('OS Command Injection')"
      owasp:
        - A01:2017 - Injection
        - A03:2021 - Injection
      technology:
        - scala
      confidence: LOW
      references:
        - https://owasp.org/Top10/A03_2021-Injection
      cwe2022-top25: true
      cwe2021-top25: true
      subcategory:
        - audit
      likelihood: LOW
      impact: HIGH
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]
      vulnerability_class:
        - Command Injection

Examples

dangerous-seq-run.scala

class Foo {
  def run1(command: String, arg1: String) = {
    import sys.process._
    // ruleid: dangerous-seq-run
    Seq(command, arg1).!
  }

  def run2(command: String) = {
    import sys.process._
    // ruleid: dangerous-seq-run
    val result = Seq(command, "--some-arg").!!
    return result
  }

  def run3(message: String) = {
    import sys.process._
    // ok: dangerous-seq-run
    Seq("ls", "-la").!!
  }

  def run4(message: String) = {
    import sys.process._
    // ok: dangerous-seq-run
    Seq("sh", "-c", "ls").!!
  }

  def run5(message: String) = {
    import sys.process._
    // ok: dangerous-seq-run
    Seq(message, "123")
  }

  def run6(command: String) = {
    // ok: dangerous-seq-run
    val result = Seq(command, "--some-arg").!!
    return result
  }
}