-
Notifications
You must be signed in to change notification settings - Fork 92
300 lines (249 loc) · 10.5 KB
/
security-echidna.yml
File metadata and controls
300 lines (249 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
name: Security - Echidna Fuzzing
on:
pull_request:
branches:
- master
paths:
- 'packages/smart-contracts/src/contracts/**/*.sol'
- 'packages/smart-contracts/echidna.config.yml'
- '.github/workflows/security-echidna.yml'
schedule:
# Run thorough fuzzing nightly at 2 AM UTC
- cron: '0 2 * * *'
workflow_dispatch:
inputs:
mode:
description: 'Testing mode'
required: true
default: 'ci'
type: choice
options:
- ci
- quick
- thorough
permissions:
contents: read
pull-requests: write
jobs:
echidna-fuzzing:
name: Echidna Property-Based Fuzzing
runs-on: ubuntu-latest
timeout-minutes: 90
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'yarn'
- name: Install dependencies
working-directory: packages/smart-contracts
run: |
yarn install --frozen-lockfile
- name: Compile contracts
working-directory: packages/smart-contracts
run: |
yarn build:sol
- name: Setup Echidna
run: |
# Pull Echidna Docker image
docker pull trailofbits/echidna:latest
# Create a wrapper script to run echidna via docker
# Mount the entire monorepo to ensure node_modules is accessible
cat > /tmp/echidna << 'EOF'
#!/bin/bash
# Find monorepo root (contains package.json with workspaces)
REPO_ROOT="$PWD"
while [ ! -f "$REPO_ROOT/lerna.json" ] && [ "$REPO_ROOT" != "/" ]; do
REPO_ROOT="$(dirname "$REPO_ROOT")"
done
# Calculate relative path from repo root to current dir
REL_PATH="${PWD#$REPO_ROOT/}"
# Run echidna with repo root mounted
docker run --rm -v "$REPO_ROOT":/src -w "/src/$REL_PATH" trailofbits/echidna:latest echidna "$@"
EOF
sudo mv /tmp/echidna /usr/local/bin/echidna
sudo chmod +x /usr/local/bin/echidna
echidna --version
- name: Restore corpus cache
uses: actions/cache@v4
with:
path: packages/smart-contracts/corpus
key: echidna-corpus-${{ github.ref_name }}-${{ github.sha }}
restore-keys: |
echidna-corpus-${{ github.ref_name }}-
echidna-corpus-master-
- name: Determine test mode
id: mode
run: |
if [ "${{ github.event_name }}" = "schedule" ]; then
echo "MODE=thorough" >> $GITHUB_OUTPUT
echo "TEST_LIMIT=500000" >> $GITHUB_OUTPUT
echo "TIMEOUT=3600" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
MODE="${{ github.event.inputs.mode }}"
echo "MODE=$MODE" >> $GITHUB_OUTPUT
if [ "$MODE" = "thorough" ]; then
echo "TEST_LIMIT=500000" >> $GITHUB_OUTPUT
echo "TIMEOUT=3600" >> $GITHUB_OUTPUT
elif [ "$MODE" = "quick" ]; then
echo "TEST_LIMIT=100000" >> $GITHUB_OUTPUT
echo "TIMEOUT=300" >> $GITHUB_OUTPUT
else
echo "TEST_LIMIT=50000" >> $GITHUB_OUTPUT
echo "TIMEOUT=180" >> $GITHUB_OUTPUT
fi
else
# Default CI mode
echo "MODE=ci" >> $GITHUB_OUTPUT
echo "TEST_LIMIT=50000" >> $GITHUB_OUTPUT
echo "TIMEOUT=180" >> $GITHUB_OUTPUT
fi
- name: Run Echidna Fuzzing
id: echidna
working-directory: packages/smart-contracts
continue-on-error: true
run: |
mkdir -p reports/security
echo "Running Echidna in ${{ steps.mode.outputs.MODE }} mode..."
echo "Test limit: ${{ steps.mode.outputs.TEST_LIMIT }}"
echo "Timeout: ${{ steps.mode.outputs.TIMEOUT }}s"
# Use relative path from smart-contracts directory to OpenZeppelin
# Docker now mounts the entire monorepo, so ../../node_modules is accessible
echidna src/contracts/test/EchidnaERC20CommerceEscrowWrapper.sol \
--contract EchidnaERC20CommerceEscrowWrapper \
--config echidna.config.yml \
--test-limit ${{ steps.mode.outputs.TEST_LIMIT }} \
--timeout ${{ steps.mode.outputs.TIMEOUT }} \
--format text \
--crytic-args="--solc-remaps @openzeppelin/=../../node_modules/@openzeppelin/" \
| tee reports/security/echidna-report.txt
ECHIDNA_EXIT=${PIPESTATUS[0]}
# Save coverage if available
if [ -f coverage.txt ]; then
mv coverage.txt reports/security/echidna-coverage.txt
fi
exit $ECHIDNA_EXIT
- name: Parse Echidna results
if: always()
id: parse
working-directory: packages/smart-contracts
run: |
# Count passed and failed properties
# Note: Echidna 2.x outputs "passing" not "passed"
PASSED=$(grep -c "echidna.*: passing" reports/security/echidna-report.txt 2>/dev/null || echo "0")
FAILED=$(grep -c "echidna.*: failed" reports/security/echidna-report.txt 2>/dev/null || echo "0")
# Ensure variables are single line and numeric
PASSED=${PASSED##*$'\n'}
FAILED=${FAILED##*$'\n'}
TOTAL=$((PASSED + FAILED))
echo "PASSED=$PASSED" >> $GITHUB_OUTPUT
echo "FAILED=$FAILED" >> $GITHUB_OUTPUT
echo "TOTAL=$TOTAL" >> $GITHUB_OUTPUT
# Extract any counterexamples
if [ "$FAILED" -gt 0 ]; then
grep -A 10 "failed" reports/security/echidna-report.txt > reports/security/counterexamples.txt 2>/dev/null || true
fi
- name: Upload Echidna reports
if: always()
uses: actions/upload-artifact@v4
with:
name: echidna-reports-${{ steps.mode.outputs.MODE }}
path: |
packages/smart-contracts/reports/security/
packages/smart-contracts/corpus/
retention-days: 90
- name: Comment on PR
if: github.event_name == 'pull_request' && always()
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const passed = '${{ steps.parse.outputs.PASSED }}';
const failed = '${{ steps.parse.outputs.FAILED }}';
const total = '${{ steps.parse.outputs.TOTAL }}';
const mode = '${{ steps.mode.outputs.MODE }}';
const testLimit = '${{ steps.mode.outputs.TEST_LIMIT }}';
const status = '${{ steps.echidna.outcome }}';
const statusEmoji = status === 'success' ? '✅' : '❌';
const passRate = total > 0 ? ((passed / total) * 100).toFixed(1) : '0';
let body = `## ${statusEmoji} Echidna Fuzzing Results
**Mode:** ${mode} (${testLimit} test sequences)
**Status:** ${status === 'success' ? 'All Properties Passed' : 'Property Violations Found'}
### Property Test Results
| Status | Count |
|--------|-------|
| ✅ Passed | ${passed} |
| ❌ Failed | ${failed} |
| **Total** | **${total}** |
| **Pass Rate** | **${passRate}%** |
`;
if (failed > 0) {
body += `### ⚠️ Invariant Violations Detected
Echidna found sequences of transactions that violate defined invariants.
This indicates potential security issues or logic errors.
**Action Required:**
1. Download the artifacts to see counterexamples
2. Review the failing properties
3. Fix the contract or adjust the properties
4. Re-run the fuzzing campaign
`;
}
body += `📄 Full report and corpus available in workflow artifacts.
<details>
<summary>ℹ️ About Echidna Fuzzing</summary>
Echidna is a property-based fuzzer that generates random sequences of transactions
to test invariants (properties that should always hold true).
**Properties tested:**
- Fee calculation bounds
- Access control enforcement
- Amount constraints
- No duplicate payments
- Zero address validation
- Integer overflow protection
</details>`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
- name: Create issue for nightly failures
if: github.event_name == 'schedule' && steps.echidna.outcome == 'failure'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const passed = '${{ steps.parse.outputs.PASSED }}';
const failed = '${{ steps.parse.outputs.FAILED }}';
github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `🔴 Echidna Nightly Fuzzing Failed - ${new Date().toISOString().split('T')[0]}`,
body: `## Echidna Nightly Fuzzing Campaign Failed
**Date:** ${new Date().toISOString()}
**Branch:** ${context.ref}
**Commit:** ${context.sha}
### Results
- ✅ Passed: ${passed}
- ❌ Failed: ${failed}
### Details
The thorough nightly fuzzing campaign found property violations.
**Action Items:**
1. Review the [workflow run](${context.payload.repository.html_url}/actions/runs/${context.runId})
2. Download artifacts to examine counterexamples
3. Investigate and fix violations
4. Re-run fuzzing to verify fix
/cc @RequestNetwork/security-team`,
labels: ['security', 'fuzzing', 'high-priority']
});
- name: Fail on property violations
if: steps.parse.outputs.FAILED != '0' && steps.parse.outputs.FAILED != ''
run: |
echo "::error::Echidna found property violations. Check the reports for counterexamples."
echo "::error::Failed properties: ${{ steps.parse.outputs.FAILED }}"
echo "::error::Passed properties: ${{ steps.parse.outputs.PASSED }}"
exit 1