Record dependency firewall policy bypasses as audit events

What does this MR do and why?

A request that bypassed an enforced dependency firewall policy wrote no audit event when another linked policy warned about or blocked the package. EnforcementService#enforce_policy audits one outcome per request, and a warn or block result wins over a bypass result, so the bypass was dropped. A Beta customer hit this with a warn baseline policy and an enforced subgroup policy.

  • Each bypassed policy now writes its own audit event, whatever the request's outcome: dependency_firewall_access_token_bypass or dependency_firewall_user_bypass.
  • The events follow the merge request approval policy bypass pattern: they reuse Security::ScanResultPolicies::PolicyBypassAuditor with a new BypassContexts::DependencyFirewallContext. Scope is the security policy project, target is the policy, and details include the project ID, policy, bypass type, token or user IDs, PURL, operation, and session ID.
  • AuditBypassesService maps each bypass reason to an auditor call. Supporting another bypass kind needs one when branch and one event type YAML.
  • PolicyEvaluator results now carry the Security::Policy record. BypassSettings#bypassing_access_token_ids returns the IDs that matched.
  • Existing dependency_firewall_allowed, _warned, and _blocked events, activity stats, and the response are unchanged. A pure bypass-allowed request now writes both the existing allowed event in the project and the new bypass event in the security policy project.
  • A failure while writing a bypass event is tracked and does not block the package, same as CreateAuditEventsService.

Resolves https://gitlab.com/gitlab-org/gitlab/-/work_items/639638

image

How to set up and validate locally

  1. Link two dependency firewall policies to a project: a warn policy and an enforced policy, both matching the same package. Put a project access token in the enforced policy's bypass_settings.access_tokens.
  2. Request the package with that token.
  3. In the security policy project, select Secure > Audit events. A dependency_firewall_access_token_bypass event names the enforced policy and the package.
Tests
  • ee/spec/services/security/dependency_firewall/enforcement_service_spec.rb: new end-to-end example with real policies (warn baseline plus enforced policy bypassed by a token). It failed on master (received: 0 times) and passes with the fix.
  • ee/spec/services/security/dependency_firewall/audit_bypasses_service_spec.rb: token and user bypass, several bypasses mixed with other outcomes, no bypass, one failing audit not skipping the rest, and an unstubbed auditor persisting the event in the security policy project.
  • ee/spec/lib/security/scan_result_policies/bypass_contexts/dependency_firewall_context_spec.rb, bypass_settings_spec.rb, policy_evaluator_spec.rb.
  • Mutation checks: removing the audit_bypasses call or the rescue fails a spec.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

Edited by Hannah Baker

Merge request reports

Loading
Loading