Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

feat: Add Semgrep Python security scanning for comprehensive code analysis

LearningCircuit2547fd0e34a1
diff --git a/.github/workflows/semgrep.yml b/.github/workflows/semgrep.yml
new file mode 100644
index 0000000000..f82efdbb5a
--- /dev/null
+++ b/.github/workflows/semgrep.yml
@@ -0,0 +1,211 @@
+name: Semgrep Security Scan
+
+on:
+ push:
+ branches: [ main, dev ]
+ pull_request:
+ branches: [ main, dev ]
+ workflow_dispatch:
+ schedule:
+ # Run security scan daily at 5 AM UTC
+ - cron: '0 5 * * *'
+
+jobs:
+ semgrep-scan:
+ runs-on: ubuntu-latest
+ permissions:
+ contents: read
+ security-events: write
+ actions: read
+
+ steps:
+ - name: Checkout code
+ uses: actions/checkout@v5
+
+ - name: Set up Python
+ uses: actions/setup-python@v5
+ with:
+ python-version: '3.12'
+
+ - name: Install Semgrep
+ run: |
+ pip install semgrep==1.87.0
+
+ - name: Run Semgrep security scan
+ run: |
+ semgrep \
+ --config=auto \
+ --severity=INFO \
+ --json \
+ --output=semgrep-results.json \
+ --metrics=off \
+ src/
+
+ - name: Run Semgrep custom rules for LDR
+ run: |
+ semgrep \
+ --config=.semgrep/rules/ \
+ --severity=INFO \
+ --json \
+ --output=semgrep-custom-results.json \
+ --metrics=off \
+ src/ || true
+
+ - name: Merge Semgrep results
+ run: |
+ python3 -c "
+ import json
+ import glob
+
+ results = []
+ for file in glob.glob('semgrep-*.json'):
+ try:
+ with open(file) as f:
+ data = json.load(f)
+ if isinstance(data, dict) and 'results' in data:
+ results.extend(data['results'])
+ elif isinstance(data, list):
+ results.extend(data)
+ except Exception as e:
+ print(f'Error reading {file}: {e}')
+
+ output = {'results': results, 'version': '1.0.0'}
+ with open('semgrep-combined-results.json', 'w') as f:
+ json.dump(output, f, indent=2)
+
+ print(f'Combined {len(results)} findings from Semgrep scans')
+ "
+
+ - name: Convert to SARIF format
+ run: |
+ python3 -c "
+ import json
+ import uuid
+ from datetime import datetime
+
+ # Load combined results
+ with open('semgrep-combined-results.json') as f:
+ semgrep_data = json.load(f)
+
+ # Convert to SARIF
+ sarif = {
+ '\$schema': 'https://json.schemastore.org/sarif-2.1.0',
+ 'version': '2.1.0',
+ 'runs': [{
+ 'tool': {
+ 'driver': {
+ 'name': 'Semgrep',
+ 'version': '1.87.0',
+ 'informationUri': 'https://semgrep.dev'
+ }
+ },
+ 'results': []
+ }]
+ }
+
+ for result in semgrep_data.get('results', []):
+ sarif_result = {
+ 'ruleId': result.get('check_id', 'unknown'),
+ 'message': {
+ 'text': result.get('message', 'Security issue detected')
+ },
+ 'level': 'warning' if result.get('metadata', {}).get('severity', 'INFO') in ['ERROR', 'WARNING'] else 'note',
+ 'locations': [{
+ 'physicalLocation': {
+ 'artifactLocation': {
+ 'uri': result.get('path', 'unknown')
+ },
+ 'region': {
+ 'startLine': result.get('start', {}).get('line', 1),
+ 'startColumn': result.get('start', {}).get('col', 1),
+ 'endLine': result.get('end', {}).get('line', result.get('start', {}).get('line', 1)),
+ 'endColumn': result.get('end', {}).get('col', result.get('start', {}).get('col', 1) + 1)
+ }
+ }
+ }]
+ }
+
+ # Add rule information
+ metadata = result.get('metadata', {})
+ sarif_result['rule'] = {
+ 'id': result.get('check_id', 'unknown'),
+ 'name': metadata.get('name', 'Security Issue'),
+ 'shortDescription': {
+ 'text': metadata.get('name', 'Security Issue')
+ },
+ 'fullDescription': {
+ 'text': metadata.get('description', 'Security vulnerability detected')
+ },
+ 'help': {
+ 'text': metadata.get('remediation', 'Review and fix the security issue')
+ },
+ 'properties': {
+ 'precision': 'medium',
+ 'tags': ['security', 'semgrep']
+ }
+ }
+
+ if 'security-severity' in metadata:
+ sarif_result['rule']['properties']['security-severity'] = metadata['security-severity']
+
+ sarif['runs'][0]['results'].append(sarif_result)
+
+ # Write SARIF file
+ with open('semgrep-results.sarif', 'w') as f:
+ json.dump(sarif, f, indent=2)
+
+ print(f'Converted {len(sarif[\"runs\"][0][\"results\"])} findings to SARIF format')
+ "
+
+ - name: Upload Semgrep results to GitHub Security tab
+ uses: github/codeql-action/upload-sarif@v3
+ if: always()
+ with:
+ sarif_file: 'semgrep-results.sarif'
+ category: semgrep-security
+
+ - name: Upload Semgrep results as artifact
+ uses: actions/upload-artifact@v4
+ if: always()
+ with:
+ name: semgrep-scan-results
+ path: |
+ semgrep-results.json
+ semgrep-custom-results.json
+ semgrep-combined-results.json
+ semgrep-results.sarif
+ retention-days: 30
+
+ - name: Display Semgrep summary
+ if: always()
+ run: |
+ echo "## Semgrep Security Scan Summary" >> $GITHUB_STEP_SUMMARY
+ echo "" >> $GITHUB_STEP_SUMMARY
+
+ if [ -f semgrep-combined-results.json ]; then
+ # Count results by severity
+ CRITICAL=$(python3 -c "import json; data=json.load(open('semgrep-combined-results.json')); print(len([r for r in data['results'] if r.get('metadata', {}).get('severity') == 'ERROR']))" 2>/dev/null || echo "0")
+ HIGH=$(python3 -c "import json; data=json.load(open('semgrep-combined-results.json')); print(len([r for r in data['results'] if r.get('metadata', {}).get('severity') == 'WARNING']))" 2>/dev/null || echo "0")
+ MEDIUM=$(python3 -c "import json; data=json.load(open('semgrep-combined-results.json')); print(len([r for r in data['results'] if r.get('metadata', {}).get('severity') == 'INFO']))" 2>/dev/null || echo "0")
+ TOTAL=$(python3 -c "import json; data=json.load(open('semgrep-combined-results.json')); print(len(data['results']))" 2>/dev/null || echo "0")
+
+ echo "📊 **Scan Results:**" >> $GITHUB_STEP_SUMMARY
+ echo "- **Critical:** $CRITICAL" >> $GITHUB_STEP_SUMMARY
+ echo "- **High:** $HIGH" >> $GITHUB_STEP_SUMMARY
+ echo "- **Medium:** $MEDIUM" >> $GITHUB_STEP_SUMMARY
+ echo "- **Total:** $TOTAL" >> $GITHUB_STEP_SUMMARY
+ echo "" >> $GITHUB_STEP_SUMMARY
+
+ if [ "$CRITICAL" -gt 0 ] || [ "$HIGH" -gt 0 ]; then
+ echo "⚠️ **Action Required:** Critical or High severity issues found" >> $GITHUB_STEP_SUMMARY
+ else
+ echo "✅ **No Critical or High severity issues found**" >> $GITHUB_STEP_SUMMARY
+ fi
+
+ echo "" >> $GITHUB_STEP_SUMMARY
+ echo "📋 **Detailed Results:**" >> $GITHUB_STEP_SUMMARY
+ echo "- Security tab: Results uploaded to GitHub Security tab" >> $GITHUB_STEP_SUMMARY
+ echo "- Artifacts: Full JSON and SARIF reports available" >> $GITHUB_STEP_SUMMARY
+ else
+ echo "❌ Semgrep scan failed or no results generated" >> $GITHUB_STEP_SUMMARY
+ fi
diff --git a/.gitignore b/.gitignore
index 5cee180932..43bc0917e0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -51,6 +51,13 @@
!.pre-commit-hooks/*.py
!.pre-commit-hooks/*.sh
+# Allow security configuration files
+!.gitleaks.toml
+!.semgrepignore
+!.trivyignore
+!.semgrep/
+!.semgrep/**
+
# Block all other dot files/folders
.*
diff --git a/.pre-commit-hooks/file-whitelist-check.sh b/.pre-commit-hooks/file-whitelist-check.sh
index be49997613..2bc6147020 100755
--- a/.pre-commit-hooks/file-whitelist-check.sh
+++ b/.pre-commit-hooks/file-whitelist-check.sh
@@ -25,6 +25,9 @@ ALLOWED_PATTERNS=(
"^\.isort\.cfg$"
"^\.coveragerc$"
"^\.secrets\.baseline$"
+ "^\.gitleaks\.toml$"
+ "^\.semgrepignore$"
+ "^\.trivyignore$"
"^pytest\.ini$"
"^LICENSE$"
"^README$"