Skip to content
Closed
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
98 changes: 98 additions & 0 deletions .github/workflows/codeql-security-gate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

name: CodeQL Security Gate

on:
push:
branches: [dev]
pull_request:
branches: [dev]
workflow_dispatch:

permissions:
contents: read
security-events: write

jobs:
security:
name: CodeQL SAST
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: csharp
build-mode: none
queries: security-and-quality

- name: Analyze
id: codeql
uses: github/codeql-action/analyze@v4
with:
output: codeql-results

- name: Security Gate - check new CWE findings
if: github.event_name == 'pull_request'
shell: bash
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
SARIF_DIR: ${{ steps.codeql.outputs.sarif-output }}
run: |
python3 - <<'PY'
import json
import os
import subprocess
import sys
from pathlib import Path

changed = set(subprocess.check_output(
["git", "diff", "--name-only",
os.environ["BASE_SHA"], "HEAD"],
text=True
).splitlines())

findings = []

for file in Path(os.environ["SARIF_DIR"]).rglob("*.sarif"):
data = json.loads(file.read_text(encoding="utf-8"))

for run in data.get("runs", []):
rules = {
rule["id"]: rule
for rule in run["tool"]["driver"].get("rules", [])
}

for result in run.get("results", []):
rule_id = result["ruleId"]
rule = rules.get(rule_id, {})
props = rule.get("properties", {})
tags = props.get("tags", [])

severity = props.get("problem.severity", "")
is_cwe = any("cwe-" in tag for tag in tags)

if not is_cwe or severity not in ("warning", "error"):
continue

for loc in result.get("locations", []):
physical = loc.get("physicalLocation", {})
path = physical.get("artifactLocation", {}).get("uri", "")
line = physical.get("region", {}).get("startLine", "?")

if path in changed:
findings.append(f"{path}:{line} — {rule_id}")
break

if findings:
print("SECURITY GATE FAILED")
for finding in findings:
print(finding)
sys.exit(1)

print("SECURITY GATE PASSED")
PY
11 changes: 11 additions & 0 deletions src/RestSharp/SecurityGateDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

using System.Security.Cryptography;

namespace RestSharp;

internal static class SecurityGateDemo {
internal static byte[] Sign(byte[] data) {
var hmac = new HMACSHA256();
return hmac.ComputeHash(data);
}
}