forge-guard 0.3.6

Pre-deployment smart contract auditing framework for Foundry
Documentation
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! CI/CD integration — generates pipeline configurations for various platforms.

pub mod vscode;

use crate::core::ForgeGuardError;

/// Generator for CI/CD pipeline configurations.
pub struct CiGenerator {
    platform: String,
}

impl CiGenerator {
    /// Create a new CI generator for the given platform.
    pub fn new(platform: &str) -> Self {
        Self {
            platform: platform.to_lowercase(),
        }
    }

    /// Get the output filename for this CI platform.
    pub fn filename(&self) -> &'static str {
        match self.platform.as_str() {
            "github" => "audit.yml",
            "gitlab" => ".gitlab-ci.yml",
            "bitbucket" => "bitbucket-pipelines.yml",
            "azure" => "azure-pipelines.yml",
            _ => "ci-config.yml",
        }
    }

    /// Generate the CI configuration content.
    pub fn generate(&self, include_deploy: bool) -> Result<String, ForgeGuardError> {
        match self.platform.as_str() {
            "github" => Ok(self.generate_github(include_deploy)),
            "gitlab" => Ok(self.generate_gitlab(include_deploy)),
            "bitbucket" => Ok(self.generate_bitbucket(include_deploy)),
            "azure" => Ok(self.generate_azure(include_deploy)),
            _ => Err(ForgeGuardError::Config(format!(
                "Unsupported CI platform: {}. Supported: github, gitlab, bitbucket, azure",
                self.platform
            ))),
        }
    }

    fn generate_github(&self, include_deploy: bool) -> String {
        let mut yaml = String::from(
            r#"name: Forge Guard Security Check

on:
  push:
    branches: [ main, master, develop ]
  pull_request:
    branches: [ main, master ]

env:
  FOUNDRY_PROFILE: ci

jobs:
  security-audit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install Foundry
        uses: foundry-rs/foundry-toolchain@v1
        with:
          version: nightly

      - name: Install Forge Guard
        run: |
          cargo install forge-guard
          forge audit --version

      - name: Run Security Audit
        run: forge audit --strict

      - name: Run Fuzzing Campaign
        run: forge fuzz --runs 10000

      - name: Run Invariant Tests
        run: forge invariant --runs 1000

      - name: Check Dependencies
        run: forge scan --depth 1

      - name: Generate Report
        run: forge audit --report --markdown
"#,
        );

        if include_deploy {
            yaml.push_str(
                r#"
  deploy:
    runs-on: ubuntu-latest
    needs: [security-audit]
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install Foundry
        uses: foundry-rs/foundry-toolchain@v1
        with:
          version: nightly

      - name: Final Security Check
        run: forge audit --strict --production

      - name: Safe Deploy
        run: forge deploy-safe
        env:
          ETH_RPC_URL: ${{ secrets.ETH_RPC_URL }}
          PRIVATE_KEY: ${{ secrets.DEPLOYER_PRIVATE_KEY }}
"#,
            );
        }

        yaml
    }

    /// Generate a GitHub Actions workflow that publishes an SBOM on every
    /// push to main/master. Used by `forge-guard sbom --ci` for supply-chain
    /// compliance (EO 14028 / NTIA minimum elements).
    pub fn generate_sbom_workflow(&self) -> String {
        String::from(
            r#"name: SBOM Generation

on:
  push:
    branches: [ main, master ]
  pull_request:
    branches: [ main, master ]

jobs:
  sbom:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install Forge Guard
        run: |
          cargo install forge-guard
          forge-guard sbom --version

      - name: Generate CycloneDX SBOM
        run: forge-guard sbom --format cyclonedx --output sbom.cyclonedx.json

      - name: Generate SPDX SBOM
        run: forge-guard sbom --format spdx --output sbom.spdx.json

      - name: Upload SBOM artifacts
        uses: actions/upload-artifact@v4
        with:
          name: sbom
          path: |
            sbom.cyclonedx.json
            sbom.spdx.json
"#,
        )
    }

    fn generate_gitlab(&self, include_deploy: bool) -> String {
        let mut yaml = String::from(
            r#"stages:
  - security-audit
  - fuzzing
  - deploy

variables:
  FOUNDRY_PROFILE: ci

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - target/

forge-guard:
  stage: security-audit
  image: ghcr.io/foundry-rs/foundry:latest
  before_script:
    - cargo install forge-guard || true
  script:
    - forge audit --strict
    - forge scan --depth 1
  artifacts:
    paths:
      - reports/
    when: always

fuzzing:
  stage: fuzzing
  image: ghcr.io/foundry-rs/foundry:latest
  script:
    - forge fuzz --runs 10000
    - forge invariant --runs 1000
"#,
        );

        if include_deploy {
            yaml.push_str(
                r#"
deploy:
  stage: deploy
  image: ghcr.io/foundry-rs/foundry:latest
  script:
    - forge audit --strict --production
    - forge deploy-safe
  only:
    - main
  environment: production
"#,
            );
        }

        yaml
    }

    fn generate_bitbucket(&self, _include_deploy: bool) -> String {
        String::from(
            r#"image: ghcr.io/foundry-rs/foundry:latest

pipelines:
  default:
    - step:
        name: Security Audit
        script:
          - cargo install forge-guard || true
          - forge audit --strict
          - forge scan --depth 1
          - forge fuzz --runs 10000
        artifacts:
          - reports/**

  branches:
    main:
      - step:
          name: Production Security Check
          script:
            - forge audit --strict --production
            - forge deploy-safe
          deployment: production
"#,
        )
    }

    fn generate_azure(&self, include_deploy: bool) -> String {
        let mut yaml = String::from(
            r#"trigger:
  - main
  - master

pool:
  vmImage: ubuntu-latest

steps:
  - checkout: self
    submodules: recursive

  - script: |
      wget -q https://github.com/foundry-rs/foundry/releases/latest/download/foundry_linux_amd64.tar.gz
      tar -xzf foundry_linux_amd64.tar.gz
      export PATH=$PATH:$(pwd)
      foundryup
    displayName: 'Install Foundry'

  - script: |
      cargo install forge-guard
    displayName: 'Install Forge Guard'

  - script: |
      forge audit --strict
    displayName: 'Run Security Audit'

  - script: |
      forge fuzz --runs 10000
    displayName: 'Run Fuzzing'

  - script: |
      forge invariant --runs 1000
    displayName: 'Run Invariant Tests'

  - script: |
      forge scan --depth 1
    displayName: 'Scan Dependencies'

  - task: PublishBuildArtifacts@1
    inputs:
      pathToPublish: reports/
      artifactName: 'audit-reports'
"#,
        );

        if include_deploy {
            yaml.push_str(
                r#"
  - script: |
      forge audit --strict --production
      forge deploy-safe
    displayName: 'Safe Deploy'                env:
      ETH_RPC_URL: $(ETH_RPC_URL)
"#,
            );
        }

        yaml
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ci_generator_github() {
        let gen = CiGenerator::new("github");
        assert_eq!(gen.filename(), "audit.yml");

        let config = gen.generate(false).unwrap();
        assert!(config.contains("name: Forge Guard Security Check"));
        assert!(config.contains("forge audit --strict"));
        assert!(config.contains("forge fuzz --runs 10000"));
    }

    #[test]
    fn test_ci_generator_github_with_deploy() {
        let gen = CiGenerator::new("github");
        let config = gen.generate(true).unwrap();
        assert!(config.contains("forge deploy-safe"));
        assert!(config.contains("needs: [security-audit]"));
    }

    #[test]
    fn test_ci_generator_gitlab() {
        let gen = CiGenerator::new("gitlab");
        assert_eq!(gen.filename(), ".gitlab-ci.yml");

        let config = gen.generate(false).unwrap();
        assert!(config.contains("forge-guard:"));
        assert!(config.contains("forge audit --strict"));
    }

    #[test]
    fn test_ci_generator_bitbucket() {
        let gen = CiGenerator::new("bitbucket");
        assert_eq!(gen.filename(), "bitbucket-pipelines.yml");

        let config = gen.generate(false).unwrap();
        assert!(config.contains("pipelines:"));
        assert!(config.contains("forge audit --strict"));
    }

    #[test]
    fn test_ci_generator_azure() {
        let gen = CiGenerator::new("azure");
        assert_eq!(gen.filename(), "azure-pipelines.yml");

        let config = gen.generate(false).unwrap();
        assert!(config.contains("vmImage: ubuntu-latest"));
        assert!(config.contains("forge audit --strict"));
    }

    #[test]
    fn test_ci_generator_invalid_platform() {
        let gen = CiGenerator::new("invalid");
        assert!(gen.generate(false).is_err());
    }

    #[test]
    fn test_ci_generator_filenames() {
        assert_eq!(CiGenerator::new("github").filename(), "audit.yml");
        assert_eq!(CiGenerator::new("gitlab").filename(), ".gitlab-ci.yml");
        assert_eq!(
            CiGenerator::new("bitbucket").filename(),
            "bitbucket-pipelines.yml"
        );
        assert_eq!(CiGenerator::new("azure").filename(), "azure-pipelines.yml");
        assert_eq!(CiGenerator::new("unknown").filename(), "ci-config.yml");
    }

    #[test]
    fn test_ci_generator_case_insensitivity() {
        assert_eq!(CiGenerator::new("GitHub").filename(), "audit.yml");
        assert_eq!(CiGenerator::new("GITLAB").filename(), ".gitlab-ci.yml");
        assert!(CiGenerator::new("GitHub").generate(false).is_ok());
        assert!(CiGenerator::new("GITLAB").generate(false).is_ok());
    }

    #[test]
    fn test_ci_generator_bitbucket_with_deploy() {
        let gen = CiGenerator::new("bitbucket");
        let config = gen.generate(true).unwrap();
        assert!(config.contains("forge deploy-safe"));
        assert!(config.contains("forge audit --strict --production"));
        assert!(config.contains("deployment: production"));
    }

    #[test]
    fn test_ci_generator_azure_with_deploy() {
        let gen = CiGenerator::new("azure");
        let config = gen.generate(true).unwrap();
        assert!(config.contains("forge deploy-safe"));
        assert!(config.contains("forge audit --strict --production"));
        assert!(config.contains("ETH_RPC_URL"));
    }

    #[test]
    fn test_ci_generator_gitlab_without_deploy_no_deploy_section() {
        let gen = CiGenerator::new("gitlab");
        let config = gen.generate(false).unwrap();
        assert!(
            !config.contains("deploy:"),
            "Should not contain deploy section"
        );
        assert!(
            !config.contains("forge deploy-safe"),
            "Should not contain deploy-safe"
        );
    }

    #[test]
    fn test_ci_generator_output_not_empty_for_all() {
        let platforms = ["github", "gitlab", "bitbucket", "azure"];
        for platform in platforms {
            let gen = CiGenerator::new(platform);
            let config = gen.generate(true).unwrap();
            assert!(!config.is_empty(), "{} should produce output", platform);
        }
    }

    #[test]
    fn test_ci_generator_unsupported_error_message() {
        let gen = CiGenerator::new("circle-ci");
        let err = gen.generate(false).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("Unsupported CI platform"));
        assert!(msg.contains("circle-ci"));
        assert!(msg.contains("github"));
        assert!(msg.contains("gitlab"));
        assert!(msg.contains("azure"));
    }

    #[test]
    fn test_ci_generator_github_contains_all_sections() {
        let gen = CiGenerator::new("github");
        let config = gen.generate(true).unwrap();
        // Security audit job
        assert!(config.contains("security-audit:"));
        // Deploy job when included
        assert!(config.contains("  deploy:"));
        // Env vars for deploy
        assert!(config.contains("ETH_RPC_URL"));
        assert!(config.contains("DEPLOYER_PRIVATE_KEY"));
    }

    #[test]
    fn test_ci_generator_sbom_workflow() {
        let gen = CiGenerator::new("github");
        let wf = gen.generate_sbom_workflow();
        assert!(wf.contains("name: SBOM Generation"));
        assert!(wf.contains("forge-guard sbom --format cyclonedx --output sbom.cyclonedx.json"));
        assert!(wf.contains("forge-guard sbom --format spdx --output sbom.spdx.json"));
        assert!(wf.contains("actions/upload-artifact@v4"));
        assert!(wf.contains("sbom.cyclonedx.json"));
    }

    #[test]
    fn test_ci_generator_sbom_workflow_schedule_trigger() {
        let gen = CiGenerator::new("github");
        let wf = gen.generate_sbom_workflow();
        // Triggered on push and PR to main/master
        assert!(wf.contains("branches: [ main, master ]"));
        assert!(wf.contains("pull_request:"));
    }

    #[test]
    fn test_ci_generator_platform_identity() {
        let github = CiGenerator::new("github");
        let gitlab = CiGenerator::new("gitlab");
        let gh_config = github.generate(false).unwrap();
        let gl_config = gitlab.generate(false).unwrap();
        // GitHub uses GitHub Actions syntax, GitLab uses GitLab CI syntax
        assert!(gh_config.contains("jobs:"));
        assert!(gl_config.contains("stages:"));
        assert!(gl_config.contains("forge-guard:"));
    }
}