Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/semgrep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
on:
workflow_dispatch: {}
pull_request: {}
push:
branches:
- main
- master
paths:
- .github/workflows/semgrep.yml
schedule:
# random HH:MM to avoid a load spike on GitHub Actions at 00:00
- cron: 6 9 * * *
name: Semgrep
jobs:
semgrep:
name: semgrep/ci
runs-on: ubuntu-latest
permissions:
contents: read
env:
SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }}
container:
image: semgrep/semgrep
steps:
- uses: actions/checkout@v6
- run: semgrep ci
1 change: 1 addition & 0 deletions .semgrepignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.py
34 changes: 34 additions & 0 deletions path-traversal-chained-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os

UPLOAD_DIR = "/var/uploads"
SAFE_DIR = "/var/safe"

# ruleid: path-traversal-open-os-path-join
# SHOULD MATCH — user_filename flows directly into os.path.join then open()
def bad_file_read(user_filename):
path = os.path.join(UPLOAD_DIR, user_filename)
with open(path, 'r') as f:
return f.read()

# ruleid: path-traversal-open-os-path-join
# SHOULD MATCH — inline: os.path.join passed directly into open() in one expression
def bad_inline(user_filename):
with open(os.path.join(UPLOAD_DIR, user_filename), 'rb') as f:
return f.read()

# ok: path-traversal-open-os-path-join
# SHOULD NOT MATCH — hardcoded filename, no user input
def safe_hardcoded():
path = os.path.join(UPLOAD_DIR, "config.json")
with open(path, 'r') as f:
return f.read()

# ok: path-traversal-open-os-path-join
# SHOULD NOT MATCH — path is validated before open()
def safe_validated(user_filename):
joined = os.path.join(UPLOAD_DIR, user_filename)
safe = os.path.abspath(joined)
if not safe.startswith(os.path.abspath(UPLOAD_DIR)):
raise ValueError("Path traversal detected")
with open(safe, 'r') as f:
return f.read()
38 changes: 38 additions & 0 deletions path-traversal-chained.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
rules:
- id: path-traversal-open-os-path-join-inline
pattern: open(os.path.join($BASE, $USER_INPUT), ...)
pattern-not: open(os.path.join("...", "..."), ...)
message: >
Path traversal: os.path.join() result passed directly to open() without
path validation. An attacker controlling the second argument can escape
the base directory using '../' sequences. Fix: resolve the path with
os.path.abspath() and assert it starts with os.path.abspath($BASE).
languages: [python]
severity: ERROR
metadata:
cwe: "CWE-22: Improper Limitation of a Pathname to a Restricted Directory"
owasp: "A01:2021 - Broken Access Control"
confidence: HIGH
category: security

- id: path-traversal-open-os-path-join-two-step
patterns:
- pattern: |
$PATH = os.path.join($BASE, $USER_INPUT)
open($PATH, ...)
- pattern-not: |
$PATH = os.path.join("...", "...")
open($PATH, ...)
message: >
Path traversal: a path built with os.path.join() is passed to open()
without validation in between. An attacker controlling the second
argument of os.path.join() can escape the base directory. Fix: call
os.path.abspath() on $PATH and verify it starts with
os.path.abspath($BASE) before calling open().
languages: [python]
severity: ERROR
metadata:
cwe: "CWE-22: Improper Limitation of a Pathname to a Restricted Directory"
owasp: "A01:2021 - Broken Access Control"
confidence: HIGH
category: security
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ansible-core==2.16.0
certifi==2021.10.8
chardet==3.0.4
charset-normalizer==2.0.12
click==8.0.1
Flask==2.0.1
Expand Down
22 changes: 22 additions & 0 deletions test-vuln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sh

long = os.environ.get("LONG", "")
# ruleid: string-concat
sh.ls("-a" + long)

# ok: string-concat
sh.ls("-al")

# ok: string-concat
sh.semgrep("--config", "https://semgrep.dev/p/r2c-CI")

confurl = os.environ.get("SEMGREP_CONFIG_URL", "")
# ruleid: string-concat
sh.semgrep("--config {}".format(confurl))

# ruleid: string-concat
sh.semgrep(f"--config {confurl}")

# ok: string-concat
args = ["--config", confurl]
sh.semgrep(*args)