164 lines
5.3 KiB
Go
164 lines
5.3 KiB
Go
// Package main implements the IntegraCI mobile-security-scan Dagger module.
|
|
//
|
|
// Mobile Application Security Testing (MAST): static security analysis of
|
|
// mobile source (Android / iOS / React Native) with mobsfscan — the MobSF
|
|
// project's static analyzer. Closes the gap where mobile apps went through
|
|
// build + test but skipped security scanning (web apps get SAST/DAST/SCA).
|
|
//
|
|
// Findings POST to /api/v1/scans/sast (scanner="mobsfscan"), so mobile
|
|
// static-security findings flow through the same deploy gate as web SAST.
|
|
// The tool runs as a pulled container image; no vendor SDK is bundled.
|
|
//
|
|
// Usage:
|
|
//
|
|
// dagger call scan --source .
|
|
// dagger call scan --source . --tool none
|
|
// dagger call scan --source . --api-url https://api.example --service-id S --version $SHA --token env://INTEGRACI_TOKEN
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"dagger/mobile-security-scan/internal/dagger"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// MobileSecurityScan is the IntegraCI mobile static-security module.
|
|
type MobileSecurityScan struct{}
|
|
|
|
// ScanResult is the normalised output of a mobile security scan.
|
|
type ScanResult struct {
|
|
Tool string `json:"tool"` // mobsfscan | none
|
|
Skipped bool `json:"skipped"` // true for the "none" no-op branch
|
|
Findings int `json:"findings"` // number of security findings
|
|
Report string `json:"report"` // raw scanner JSON
|
|
Pass bool `json:"pass"` // false when findings were detected
|
|
Error string `json:"error,omitempty"` // failure detail, if any
|
|
}
|
|
|
|
// Scan runs the mobile static-security scanner over the source tree and,
|
|
// when API coordinates are supplied, posts the result as a SAST scan.
|
|
func (m *MobileSecurityScan) Scan(
|
|
ctx context.Context,
|
|
// Source directory to scan (Android/iOS/React-Native project).
|
|
source *dagger.Directory,
|
|
// Tool: mobsfscan | none
|
|
// +optional
|
|
// +default="mobsfscan"
|
|
tool string,
|
|
// IntegraCI API URL for posting results.
|
|
// +optional
|
|
apiURL string,
|
|
// JWT token secret for API authentication.
|
|
// +optional
|
|
token *dagger.Secret,
|
|
// Service ID to link scan results.
|
|
// +optional
|
|
serviceID string,
|
|
// Version (commit SHA) the scan applies to.
|
|
// +optional
|
|
version string,
|
|
) (*ScanResult, error) {
|
|
var result *ScanResult
|
|
switch tool {
|
|
case "none":
|
|
result = &ScanResult{Tool: "none", Skipped: true, Pass: true}
|
|
case "mobsfscan", "":
|
|
result = runMobsfscan(ctx, source)
|
|
default:
|
|
return nil, fmt.Errorf("unsupported mobile-security tool: %q (want mobsfscan|none)", tool)
|
|
}
|
|
|
|
if apiURL != "" && token != nil {
|
|
if err := postResults(ctx, apiURL, token, serviceID, version, result); err != nil {
|
|
return result, err
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// runMobsfscan runs `mobsfscan --json .` over the source tree. mobsfscan
|
|
// exits non-zero when findings are present; we swallow that, capture the JSON
|
|
// report, and count findings ourselves so the caller decides how to gate.
|
|
func runMobsfscan(ctx context.Context, source *dagger.Directory) *ScanResult {
|
|
out, _ := dag.Container().
|
|
From("opensecurity/mobsfscan:latest").
|
|
WithMountedDirectory("/src", source).
|
|
WithWorkdir("/src").
|
|
WithExec([]string{"mobsfscan", "--json", "."}).
|
|
Stdout(ctx)
|
|
|
|
result := &ScanResult{Tool: "mobsfscan", Report: out}
|
|
// mobsfscan JSON emits one "metadata" block with a "severity" per matched
|
|
// rule; counting "severity" occurrences is a zero-dep proxy for findings.
|
|
result.Findings = strings.Count(out, `"severity"`)
|
|
result.Pass = result.Findings == 0
|
|
if !result.Pass {
|
|
result.Error = fmt.Sprintf("mobsfscan: %d mobile security finding(s)", result.Findings)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// postResults reports the mobile-security scan to the IntegraCI scans/sast
|
|
// endpoint. Shape matches api/app/schemas/scan.py ScanResultCreate: findings
|
|
// populate report_data.sast_errors (the field the deploy OPA gate reads), so
|
|
// mobile static-security findings gate exactly like web SAST.
|
|
func postResults(
|
|
ctx context.Context,
|
|
apiURL string,
|
|
token *dagger.Secret,
|
|
serviceID string,
|
|
version string,
|
|
result *ScanResult,
|
|
) error {
|
|
payload := map[string]any{
|
|
"service_id": serviceID,
|
|
"version": version,
|
|
"scan_type": "sast",
|
|
"scanner": result.Tool,
|
|
"report_format": "json",
|
|
"findings_count": result.Findings,
|
|
"high_count": result.Findings,
|
|
"report_data": map[string]any{
|
|
"sast_errors": result.Findings,
|
|
"secret_count": 0,
|
|
"dast_high": 0,
|
|
"dast_critical": 0,
|
|
"sbom_present": false,
|
|
"signed": false,
|
|
},
|
|
}
|
|
body, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal scan payload: %w", err)
|
|
}
|
|
|
|
endpoint := strings.TrimRight(apiURL, "/") + "/api/v1/scans/sast"
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
|
|
if err != nil {
|
|
return fmt.Errorf("build request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
tokenStr, err := token.Plaintext(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("read token secret: %w", err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+tokenStr)
|
|
|
|
client := &http.Client{Timeout: 30 * time.Second}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("API post to %s failed: %w", endpoint, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode >= 300 {
|
|
return fmt.Errorf("API post to %s returned %d", endpoint, resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|