// Package main implements the IntegraCI model-scan Dagger module. // // AI model scan-on-ingest — defends against OWASP LLM04 "Data and Model // Poisoning" by statically scanning model artifacts for unsafe deserialization // (pickle opcodes that execute code on load — a common model-supply-chain // backdoor) with picklescan before a model is adopted. The platform previously // synced model *metadata* (model_registry / huggingface) but had no tool that // scanned the *artifact*. // // Unsafe findings POST to /api/v1/scans (scan_type="sca", scanner="picklescan") // so model-poisoning findings flow through the supply-chain gate. Runs as a // pulled container image; no vendor SDK bundled. // // Usage: // // dagger call scan --source ./model // dagger call scan --source ./model --tool none // dagger call scan --source ./model --api-url https://api.example --service-id S --version v1 --token env://INTEGRACI_TOKEN package main import ( "bytes" "context" "dagger/model-scan/internal/dagger" "encoding/json" "fmt" "net/http" "strings" "time" ) // ModelScan is the IntegraCI AI-model artifact scanner. type ModelScan struct{} // ScanResult is the normalised output of a model scan. type ScanResult struct { Tool string `json:"tool"` // picklescan | none Skipped bool `json:"skipped"` // true for the "none" no-op branch Unsafe int `json:"unsafe"` // number of unsafe/dangerous globals Report string `json:"report"` // raw scanner output Pass bool `json:"pass"` // false when unsafe artifacts detected Error string `json:"error,omitempty"` // failure detail, if any } // Scan runs the model artifact scanner over the source and, when API // coordinates are supplied, posts unsafe findings as an SCA scan. func (m *ModelScan) Scan( ctx context.Context, // Directory containing the model artifact(s) to scan. source *dagger.Directory, // Tool: picklescan | none // +optional // +default="picklescan" 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 (model version / digest) 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 "picklescan", "": result = runPicklescan(ctx, source) default: return nil, fmt.Errorf("unsupported model-scan tool: %q (want picklescan|none)", tool) } if apiURL != "" && token != nil { if err := postResults(ctx, apiURL, token, serviceID, version, result); err != nil { return result, err } } return result, nil } // runPicklescan runs `picklescan -p /src` over the artifact tree. picklescan // flags globals that can execute code on unpickling; we swallow its non-zero // exit, capture the report, and count "Dangerous"/"unsafe" hits ourselves. func runPicklescan(ctx context.Context, source *dagger.Directory) *ScanResult { out, _ := dag.Container(). From("python:3.12-slim"). WithMountedDirectory("/src", source). WithExec([]string{"pip", "install", "--no-cache-dir", "picklescan==0.0.21"}). WithExec([]string{"sh", "-c", "picklescan -p /src || true"}). Stdout(ctx) result := &ScanResult{Tool: "picklescan", Report: out} // picklescan reports "Dangerous" / "Global" for unsafe imports and a // summary line; counting "Dangerous" hits is a zero-dep proxy. result.Unsafe = strings.Count(out, "Dangerous") + strings.Count(strings.ToLower(out), "unsafe imports") result.Pass = result.Unsafe == 0 if !result.Pass { result.Error = fmt.Sprintf("picklescan: %d unsafe global(s) detected", result.Unsafe) } return result } // postResults reports unsafe model artifacts to the IntegraCI scans endpoint as // an SCA (supply-chain) scan, scanner="picklescan". 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": "sca", "scanner": "picklescan", "report_format": "json", "findings_count": result.Unsafe, "critical_count": result.Unsafe, "report_data": map[string]any{ "model_unsafe": result.Unsafe, }, } body, err := json.Marshal(payload) if err != nil { return fmt.Errorf("marshal scan payload: %w", err) } endpoint := strings.TrimRight(apiURL, "/") + "/api/v1/scans" 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 }