From 5b32f3bcbeab534a0019f0977ce7857ccc0e3489 Mon Sep 17 00:00:00 2001 From: taiga Date: Sun, 29 Nov 2026 20:06:09 +0500 Subject: [PATCH 1/2] Add CodeQL Security Gate --- .github/workflows/codeql-security-gate.yml | 98 ++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 .github/workflows/codeql-security-gate.yml diff --git a/.github/workflows/codeql-security-gate.yml b/.github/workflows/codeql-security-gate.yml new file mode 100644 index 000000000..0dd73e2d2 --- /dev/null +++ b/.github/workflows/codeql-security-gate.yml @@ -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 From a54b4f2841307a4f09b35be7b91d36fff1faa09a Mon Sep 17 00:00:00 2001 From: taiga Date: Sun, 29 Nov 2026 20:18:58 +0500 Subject: [PATCH 2/2] Test Security Gate with CWE-404 --- src/RestSharp/SecurityGateDemo.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/RestSharp/SecurityGateDemo.cs diff --git a/src/RestSharp/SecurityGateDemo.cs b/src/RestSharp/SecurityGateDemo.cs new file mode 100644 index 000000000..9cc2951c9 --- /dev/null +++ b/src/RestSharp/SecurityGateDemo.cs @@ -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); + } +}