|
15 | 15 | logger = logging.getLogger(__name__) |
16 | 16 |
|
17 | 17 |
|
| 18 | +def normalize_repo_relative_path(path_value: str | None) -> str | None: |
| 19 | + """Normalize a repo-relative path to the POSIX form emitted by SAST alerts.""" |
| 20 | + if path_value is None: |
| 21 | + return None |
| 22 | + |
| 23 | + path_str = str(path_value).strip() |
| 24 | + if not path_str: |
| 25 | + return None |
| 26 | + |
| 27 | + # Accept common local input styles but keep the final format strict. |
| 28 | + path_str = path_str.replace('\\', '/') |
| 29 | + while path_str.startswith('./'): |
| 30 | + path_str = path_str[2:] |
| 31 | + path_str = path_str.lstrip('/') |
| 32 | + |
| 33 | + normalized_parts: List[str] = [] |
| 34 | + for part in path_str.split('/'): |
| 35 | + if not part or part == '.': |
| 36 | + continue |
| 37 | + if part == '..': |
| 38 | + return None |
| 39 | + normalized_parts.append(part) |
| 40 | + |
| 41 | + normalized = '/'.join(normalized_parts) |
| 42 | + return normalized or None |
| 43 | + |
| 44 | + |
| 45 | +def parse_sast_ignore_overrides(raw_value: str | None) -> List[Dict[str, str | None]]: |
| 46 | + """Parse `rule_id` and `rule_id:path` ignore override entries.""" |
| 47 | + overrides: List[Dict[str, str | None]] = [] |
| 48 | + seen: set[tuple[str, str | None]] = set() |
| 49 | + |
| 50 | + if not raw_value: |
| 51 | + return overrides |
| 52 | + |
| 53 | + for raw_entry in str(raw_value).split(','): |
| 54 | + entry = raw_entry.strip() |
| 55 | + if not entry: |
| 56 | + continue |
| 57 | + |
| 58 | + rule_id = entry |
| 59 | + path = None |
| 60 | + |
| 61 | + if ':' in entry: |
| 62 | + rule_id, path_part = entry.split(':', 1) |
| 63 | + rule_id = rule_id.strip() |
| 64 | + path_part = path_part.strip() |
| 65 | + |
| 66 | + if not rule_id or not path_part: |
| 67 | + logger.warning("Ignoring malformed SAST ignore override: %r", entry) |
| 68 | + continue |
| 69 | + |
| 70 | + if any(ch in path_part for ch in ('*', '?', '[')): |
| 71 | + logger.warning( |
| 72 | + "Ignoring unsupported SAST ignore override with glob syntax: %r", |
| 73 | + entry, |
| 74 | + ) |
| 75 | + continue |
| 76 | + |
| 77 | + path = normalize_repo_relative_path(path_part) |
| 78 | + if not path: |
| 79 | + logger.warning("Ignoring invalid repo-relative path in SAST override: %r", entry) |
| 80 | + continue |
| 81 | + else: |
| 82 | + rule_id = rule_id.strip() |
| 83 | + if not rule_id: |
| 84 | + logger.warning("Ignoring malformed SAST ignore override: %r", entry) |
| 85 | + continue |
| 86 | + |
| 87 | + key = (rule_id, path) |
| 88 | + if key in seen: |
| 89 | + continue |
| 90 | + seen.add(key) |
| 91 | + overrides.append({'rule_id': rule_id, 'path': path}) |
| 92 | + |
| 93 | + return overrides |
| 94 | + |
| 95 | + |
| 96 | +def alert_matches_sast_ignore_override( |
| 97 | + alert: Dict[str, Any], |
| 98 | + override: Dict[str, str | None], |
| 99 | +) -> bool: |
| 100 | + """Return True when an alert matches a parsed SAST ignore override.""" |
| 101 | + props = alert.get('props', {}) or {} |
| 102 | + rule_id = props.get('ruleId') or alert.get('title') or alert.get('ruleId') |
| 103 | + if not rule_id or rule_id != override.get('rule_id'): |
| 104 | + return False |
| 105 | + |
| 106 | + override_path = override.get('path') |
| 107 | + if not override_path: |
| 108 | + return True |
| 109 | + |
| 110 | + alert_path = ( |
| 111 | + props.get('filePath') |
| 112 | + or (alert.get('location') or {}).get('path') |
| 113 | + or '' |
| 114 | + ) |
| 115 | + normalized_alert_path = normalize_repo_relative_path(alert_path) |
| 116 | + return normalized_alert_path == override_path |
| 117 | + |
| 118 | + |
18 | 119 | class Config: |
19 | 120 | """Configuration object that provides unified access to all settings""" |
20 | 121 |
|
@@ -112,6 +213,13 @@ def get_action_for_severity(self, severity: str) -> str: |
112 | 213 | else: |
113 | 214 | # Default action for unknown severities |
114 | 215 | return 'monitor' |
| 216 | + |
| 217 | + def get_sast_ignore_overrides(self) -> List[Dict[str, str | None]]: |
| 218 | + """Return parsed SAST ignore overrides from config.""" |
| 219 | + if not hasattr(self, '_sast_ignore_overrides_cache'): |
| 220 | + raw_value = self.get('sast_ignore_overrides', '') |
| 221 | + self._sast_ignore_overrides_cache = parse_sast_ignore_overrides(raw_value) |
| 222 | + return self._sast_ignore_overrides_cache |
115 | 223 |
|
116 | 224 | @property |
117 | 225 | def repo(self) -> str: |
|
0 commit comments