Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 18 additions & 35 deletions actions/repo-hygiene/check-repo-hygiene.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,30 @@

# --- public-repository leaks (SK.9.1) -----------------------------------------------------
#
# Matched by the tracker's key shape rather than one project's prefix, so a second project's
# keys are caught too. Letters only in the prefix, and at least two: allowing digits made an
# earlier version match `Z0-9` inside a regex character class, and a false positive is what
# gets a guard switched off wholesale.
TRACKER_KEY = re.compile(r"\b[A-Z]{2,10}-\d+\b(?!\.\d)")

# Same shape, not tracker keys. Each is a real external standard, so this is a list of
# evidenced exceptions rather than a way to quiet a genuine hit.
NOT_A_TRACKER_KEY = re.compile(r"\b(?:ISO-\d+|RFC-\d+|SHA-\d+|UTF-\d+|AES-\d+|PEP-\d+|CVE-\d+|SMETS-\d+|FLEX-\d+|GPL-\d+|LGPL-\d+|AGPL-\d+)\b")

# Same shape at the end of a longer standard's name, matched against the line up to the key's
# end, since the key alone cannot see what comes before it: Unicode-DFS-2016, the SPDX name of
# Unicode's data files licence, which a Debian copyright file must spell exactly. Only that name
# is let through, so a project whose keys start DFS-, or a key that follows "Unicode-", is still
# reported.
PART_OF_A_STANDARD = re.compile(r"\bUnicode-DFS-\d+$")

# Any link into the internal Atlassian site, not only a wiki one. Review asked whether this
# should require a /wiki/ path, since the name said "wiki link" while the pattern also matched a
# Jira /browse/ URL. Widened the name rather than narrowing the pattern: a Jira board URL
# carrying no issue key - .../jira/software/projects/X/boards/1 - would otherwise slip past both
# this and the tracker-key check, and it is exactly as much of a leak. A /browse/ link may now
# be reported twice, once here and once as its issue key. That redundancy is the cheaper error.
# Tracker keys are not searched for by shape. `ABC-123` is too common a shape outside any
# tracker - standards, licences, product codes, vendors' error codes - and each new one needed
# an exception, so the search cost more than it caught. Links into the internal site are what
# is searched for: a URL shape nothing else has.
#
# Any link into the internal site is a leak, whether a wiki page, an issue or a board, so the
# pattern matches the host rather than one path under it.
#
# The URL alternative consumes the rest of the URL, so a full wiki link is reported once rather
# than twice - the host branch and the /wiki/spaces/ branch would otherwise both match adjacent
# non-overlapping parts of the same link. The bare /wiki/spaces/ branch then only fires on a
# path with no host before it.
#
# A link written without its scheme is matched by its path instead: the host followed by one of
# the site's own top-level paths and a slash.
#
# A URL shape is required, not the bare hostname. Matching the substring flagged a routine
# prompt whose whole purpose is forbidding such links - the guard firing on the instruction not
# to do the thing - and so did requiring only a slash after the host, because the same prompt
# says "an atlassian.net/wiki link".
INTERNAL_LINK = re.compile(
r"(?:https?://(?:[^\s)\"'/?#]*\.)?atlassian\.net(?![A-Za-z0-9-]|\.[A-Za-z])[^\s)\"\']*)|/wiki/spaces/"
r"(?:https?://(?:[^\s)\"'/?#]*\.)?atlassian\.net(?![A-Za-z0-9-]|\.[A-Za-z])[^\s)\"\']*)"
r"|(?:[^\s)\"'/?#]*\.)?atlassian\.net/(?:wiki|browse|jira)/[^\s)\"\']*"
r"|/wiki/spaces/"
)

# --- CI job bounds (GB.3.5) ---------------------------------------------------------------
Expand Down Expand Up @@ -183,7 +172,7 @@ def tracked_paths(root):
Deliberately not a filesystem walk: that would sweep in build output and a developer's own
gitignored configuration, which may hold real credentials and is none of this check's
business. Text is decided by sniffing rather than by a suffix allowlist, because the files
most likely to carry a leaked tracker key - maintainer scripts, CODEOWNERS - have no
most likely to carry a leaked internal link - maintainer scripts, CODEOWNERS - have no
extension at all.

Args:
Expand Down Expand Up @@ -383,11 +372,11 @@ def check_the_scan_is_real(root, tracked, files):


def check_no_internal_references(root, tracked, files):
"""No tracker key or internal Atlassian link in a public repository (SK.9.1).
"""No internal Atlassian link in a public repository (SK.9.1).

Both are leaks and both are dead references to anyone reading the repository, including
whoever reads generated release notes. Describe the work instead; the issue carries the
link in the other direction.
A leak, and a dead reference to anyone reading the repository, including whoever reads
generated release notes. Describe the work instead; the issue carries the link in the other
direction. A bare tracker key is not searched for: see the note above ``INTERNAL_LINK``.

Args:
root (pathlib.Path): repository root.
Expand All @@ -411,12 +400,6 @@ def check_no_internal_references(root, tracked, files):
except OSError as exc:
raise CannotEvaluate(f"{relative} could not be read: {exc}") from exc
for number, line in enumerate(text.splitlines(), 1):
for match in TRACKER_KEY.finditer(line):
span = match.group(0)
if not NOT_A_TRACKER_KEY.search(span) and not PART_OF_A_STANDARD.search(
line[: match.end()]
):
findings.append(f"{relative}:{number}: tracker key {span}")
for match in INTERNAL_LINK.finditer(line):
findings.append(f"{relative}:{number}: internal Atlassian link ({match.group(0)[:48]})")
return findings
Expand Down
49 changes: 31 additions & 18 deletions tests/test_internal_references.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
"""The hygiene check's search for tracker keys: what it reports, and the standards it does not.
"""The hygiene check's search for internal Atlassian links: what it reports, and what it does not.

The check is loaded from the action's script, so what is tested is the script that ships. The
keys it should report are assembled at run time, so that this file holds none for the check to
links it should report are assembled at run time, so that this file holds none for the check to
find.
"""

import pytest
from conftest import load_hygiene

HOST = "example" + "." + "atlassian" + ".net"


def findings(tmp_path, line):
path = tmp_path / "copyright"
path = tmp_path / "notes.md"
path.write_text(line + "\n", encoding="utf-8")
return load_hygiene().check_no_internal_references(tmp_path, [path], [path])


@pytest.mark.parametrize("line", [
"License: Unicode-DFS-2016",
"License: GPL-3+ and Expat and Unicode-DFS-2016 and Unicode-3.0",
"Encoded as UTF-8, per RFC-3629 and ISO-8859",
@pytest.mark.parametrize("link", [
f"https://{HOST}/wiki/spaces/XY/pages/123",
f"https://{HOST}/browse/" + "XY" + "-22",
f"https://{HOST}/jira/software/projects/XY/boards/1",
# Written without its scheme, as a link pasted from an address bar often is.
f"{HOST}/browse/" + "XY" + "-22",
f"{HOST}/wiki/spaces/XY/pages/123",
])
def test_an_external_standard_is_not_a_tracker_key(tmp_path, line):
assert findings(tmp_path, line) == []
def test_an_internal_link_is_reported(tmp_path, link):
(finding,) = findings(tmp_path, f"See {link} for the decision.")
assert finding.startswith("notes.md:1: internal Atlassian link (")


def test_a_wiki_path_with_no_host_is_reported(tmp_path):
assert findings(tmp_path, "Moved to " + "/wiki" + "/spaces/XY") == [
"notes.md:1: internal Atlassian link (/wiki/spaces/)"
]

@pytest.mark.parametrize("before,key", [
("Fixes ", "XY" + "-22"),
("See ", "ABC" + "-1"),
# Only the Unicode licence's name is let through, not every key ending its shape.
("Fixes ", "DFS" + "-2016"),
("MyUnicode-", "DFS" + "-2016"),
("Unicode-", "XY" + "-22"),

@pytest.mark.parametrize("line", [
# A bare tracker-key shape is not searched for: standards, licences, product codes and
# vendors' error codes all share it.
"Fixes " + "XY" + "-22",
"License: GPL-3+ and Unicode-DFS-2016",
"Tariff E-1R-AGILE-24-10-01-C, error KT-CT-1139",
# The hostname named as text, not as a link: an instruction forbidding such links says it.
"Never put an " + "atlassian" + ".net" + "/wiki link in a commit message",
])
def test_a_tracker_key_is_reported(tmp_path, before, key):
assert findings(tmp_path, before + key) == [f"copyright:1: tracker key {key}"]
def test_what_is_not_a_link_is_not_reported(tmp_path, line):
assert findings(tmp_path, line) == []