-
-
Notifications
You must be signed in to change notification settings - Fork 22
security: require zero CodeQL findings #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0185ce3
0188464
664d452
9c316c0
e44d2ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,19 +21,28 @@ | |
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import heapq | ||
| import re | ||
| from dataclasses import dataclass, field | ||
| from typing import Any, Optional | ||
|
|
||
| from engraphis.core.interfaces import Edge, Node | ||
|
|
||
| # ── Regex NER (ported from engraphis/engines/ingest.py — the v1 heuristic path) ── | ||
| # Capitalized multi-word sequences, emails, URLs/hashtags, quoted names/mentions. | ||
| _ENTITY_RE = re.compile( | ||
| r"\b([A-Z][a-z]+(?:-[A-Za-z]+)*(?:\s+[A-Z][a-z]+(?:-[A-Za-z]+)*){0,3})\b" | ||
| r"|([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})" | ||
| r"|(#[a-zA-Z][a-zA-Z0-9_-]+)" | ||
| r"|(@[a-zA-Z][a-zA-Z0-9_-]+)" | ||
| # Capitalized multi-word sequences, emails, hashtags, and mentions. Keep the | ||
| # individual recognizers unambiguous: the old all-in-one expression could take | ||
| # polynomial time while backtracking over a long email-like local part. | ||
| _CAPITALIZED_WORD_RE = re.compile(r"\b[A-Z][a-z]+(?:-[A-Za-z]+)*\b") | ||
| _HASHTAG_RE = re.compile(r"#[a-zA-Z][a-zA-Z0-9_-]+") | ||
| _MENTION_RE = re.compile(r"@[a-zA-Z][a-zA-Z0-9_-]+") | ||
| _EMAIL_LOCAL_CHARS = frozenset( | ||
| "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._%+-" | ||
| ) | ||
| _EMAIL_DOMAIN_CHARS = frozenset( | ||
| "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-" | ||
| ) | ||
| _EMAIL_SUFFIX_CHARS = frozenset( | ||
| "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
| ) | ||
| _RELATION_RE = re.compile( | ||
| r"\b(?:is|are|was|were|has|have|had|owns|works at|lives in|prefers|likes|" | ||
|
|
@@ -79,6 +88,35 @@ | |
| "My", "Our", "Your", "Their", "His", "Her", "Its"} | ||
|
|
||
|
|
||
| def _iter_emails(text: str): | ||
| """Yield email-like spans in one pass without regex backtracking.""" | ||
|
|
||
| index = 0 | ||
| length = len(text) | ||
| while index < length: | ||
| if text[index] not in _EMAIL_LOCAL_CHARS: | ||
| index += 1 | ||
| continue | ||
| if index > 0 and text[index - 1] in _EMAIL_LOCAL_CHARS: | ||
| index += 1 | ||
| continue | ||
| start = index | ||
| while index < length and text[index] in _EMAIL_LOCAL_CHARS: | ||
| index += 1 | ||
| if index >= length or text[index] != "@": | ||
| continue | ||
| domain_start = index + 1 | ||
| end = domain_start | ||
| while end < length and text[end] in _EMAIL_DOMAIN_CHARS: | ||
| end += 1 | ||
| domain = text[domain_start:end] | ||
| dot = domain.rfind(".") | ||
| suffix = domain[dot + 1:] if dot > 0 else "" | ||
| if len(suffix) >= 2 and all(char in _EMAIL_SUFFIX_CHARS for char in suffix): | ||
| yield start, end, text[start:end] | ||
|
Comment on lines
+112
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an email is followed by a period, such as Useful? React with 👍 / 👎. |
||
| index = domain_start if end < length and text[end] == "@" else end | ||
|
|
||
|
|
||
| def _defang(value: str) -> str: | ||
| return _CONTROL_RE.sub("", value or "").strip()[:_MAX_NAME] | ||
|
|
||
|
|
@@ -105,24 +143,71 @@ class GraphExtraction: | |
|
|
||
|
|
||
| def _extract_entities(text: str) -> list[tuple[str, str]]: | ||
| text = text or "" | ||
|
|
||
| def capitalized_candidates(): | ||
| # Assemble at most four whitespace-separated capitalized words, matching the | ||
| # former heuristic without placing a nested repetition over untrusted text. | ||
| words = iter(_CAPITALIZED_WORD_RE.finditer(text)) | ||
| current = next(words, None) | ||
| while current is not None: | ||
| first = last = current | ||
| current = None | ||
| for _ in range(3): | ||
| following = next(words, None) | ||
| if following is None: | ||
| break | ||
| if text[last.end():following.start()].isspace(): | ||
| last = following | ||
| continue | ||
| current = following | ||
| break | ||
| else: | ||
| current = next(words, None) | ||
| yield ( | ||
| first.start(), | ||
| 0, | ||
| last.end(), | ||
| text[first.start():last.end()], | ||
| "person_or_concept", | ||
| ) | ||
|
|
||
| # Merge four already-position-ordered iterators. This keeps memory bounded even | ||
| # for very large untrusted input and lets the entity fanout cap stop collection. | ||
| candidates = heapq.merge( | ||
| capitalized_candidates(), | ||
| ( | ||
| (start, 1, end, value, "email") | ||
| for start, end, value in _iter_emails(text) | ||
| ), | ||
| ( | ||
| (match.start(), 2, match.end(), match.group(0), "hashtag") | ||
| for match in _HASHTAG_RE.finditer(text) | ||
| ), | ||
| ( | ||
| (match.start(), 3, match.end(), match.group(0), "mention") | ||
| for match in _MENTION_RE.finditer(text) | ||
| ), | ||
| ) | ||
|
|
||
| seen: set[str] = set() | ||
| out: list[tuple[str, str]] = [] | ||
| for m in _ENTITY_RE.finditer(text or ""): | ||
| raw = (m.group(0) or "").strip() | ||
| matched_through = 0 | ||
| for start, _priority, end, candidate, etype in candidates: | ||
| if start < matched_through: | ||
| continue | ||
| matched_through = end | ||
| raw = candidate.strip() | ||
| if not raw or raw.casefold() in _STOPWORD_KEYS or raw.casefold() in ( | ||
| "user", "the user" | ||
| ): | ||
| continue | ||
| if raw.startswith("#"): | ||
| ent, etype = raw, "hashtag" | ||
| elif raw.startswith("@"): | ||
| ent, etype = raw, "mention" | ||
| elif "@" in raw and "." in raw: | ||
| ent, etype = raw, "email" | ||
| else: | ||
| if etype == "person_or_concept": | ||
| ent, etype = _canon_concept(raw), "person_or_concept" | ||
| if len(ent) < 2 or ent.casefold() in _STOPWORD_KEYS: | ||
| continue | ||
| else: | ||
| ent = raw | ||
| key = ent.lower() | ||
| if key not in seen: | ||
| seen.add(key) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
analyzeexposessarif-outputas the generated SARIF file path, butcheck_codeql_sarif.pytreats its argument as a directory and callsrglob("*.sarif"). SincePath.rglobon that file returns no results, both matrix jobs fail with “no SARIF files found” even when CodeQL is clean, blocking every PR and main push. Pass the configuredcodeql-resultsdirectory instead, or make the checker accept an individual SARIF file.Useful? React with 👍 / 👎.