Skip to main content

Policy Engine

Enforce security policies with configurable severity thresholds and actions. Block, warn, or allow vulnerabilities based on your organization's requirements.


How It Works

The Policy Engine evaluates scan results against defined policies and returns:

  • Block - Fail the build/request
  • Warn - Log warning, continue
  • Allow - No action
builder.Services.AddPrimusSecurity(opts =>
{
opts.EnablePolicyValidation = true;
opts.FailOnCritical = true; // Block on critical findings
});

Policy Configuration

Basic Configuration

{
"PrimusSecurity": {
"EnablePolicyValidation": true,
"FailOnCritical": true,
"MaxCriticalFindings": 0,
"MaxHighFindings": 5,
"MinimumSeverity": "Medium"
}
}

Advanced Policy

var policy = new SecurityPolicy
{
Name = "Production Security Policy",
MinimumSeverity = SecuritySeverity.Medium,
MaxCriticalFindings = 0,
MaxHighFindings = 0,
BlockOnCategories = new[] { "Secrets", "Injection" },
AllowedCves = new[] { "CVE-2021-accepted-risk" }
};

var engine = new PolicyEngine();
var result = engine.Evaluate(scanResult.Findings, policy);

if (result.Action == PolicyAction.Block)
{
throw new SecurityPolicyViolationException(result.Violations);
}

API Usage

Validate Policy

[HttpPost("validate-policy")]
public IActionResult ValidatePolicy([FromBody] ValidatePolicyRequest request)
{
var findings = request.Findings.Select(f => new SecurityFinding
{
Severity = ParseSeverity(f.Severity),
Title = f.Title,
CvssScore = f.CvssScore
});

var result = _policyEngine.Evaluate(findings, new SecurityPolicy
{
MaxCriticalFindings = request.MaxCriticalFindings,
MaxHighFindings = request.MaxHighFindings
});

return Ok(new
{
Passed = result.Action != PolicyAction.Block,
Action = result.Action.ToString(),
Violations = result.Violations
});
}

Request

{
"findings": [
{ "severity": "critical", "title": "Hardcoded AWS Key", "cvssScore": 9.8 },
{ "severity": "high", "title": "SQL Injection", "cvssScore": 7.5 }
],
"maxCriticalFindings": 0,
"maxHighFindings": 5
}

Response

{
"passed": false,
"action": "Block",
"violations": [
{
"rule": "MaxCriticalFindings",
"description": "Critical findings exceed threshold",
"threshold": "0",
"actual": "1"
}
]
}

Policy Actions

ActionBehavior
BlockFail the build, return error
WarnLog warning, allow to continue
AllowNo action taken

Threshold Configuration

SettingDescriptionDefault
MaxCriticalFindingsMax allowed critical issues0
MaxHighFindingsMax allowed high issues5
MinimumSeverityIgnore findings below thisMedium
BlockOnCategoriesAlways block these categories[]
AllowedCvesAccepted risk CVEs[]

Integration with CI/CD

# In your CI pipeline
dotnet run --project SecurityScanner -- --policy strict

# Exit code 1 = policy violation (fail build)
# Exit code 0 = policy passed

Next Steps

Want to...See Guide
Generate compliance reportsEnterprise Reporting
Add to CI/CD pipelineCI/CD Integration
Configure secret patternsSecret Detection