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
190 changes: 190 additions & 0 deletions .github/scripts/check_crypto_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

"""Reject direct cryptographic implementation dependencies outside crypto/*.

The check walks every manifest so it sees declarations that Cargo metadata
would omit, including unused workspace entries, standalone workspace
inheritance, renamed packages, target-specific tables, and both Cargo
dependency table spellings.
"""

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
import sys
import tomllib

FORBIDDEN_PACKAGES = frozenset(
{
"sha1",
"sha2",
"hmac",
"rsa",
"p256",
"aes-gcm",
"getrandom",
"boring",
"openssl",
"ring",
"aws-lc-rs",
}
)
SKIPPED_DIRECTORIES = frozenset({"target", ".git"})
# Cargo 1.87 accepts both hyphenated and underscored table names.
DEPENDENCY_TABLES = (
"dependencies",
"dev-dependencies",
"dev_dependencies",
"build-dependencies",
"build_dependencies",
)


@dataclass(frozen=True)
class Violation:
manifest: str
table: str
key: str
package: str

def __str__(self) -> str:
return (
f"{self.manifest}: [{self.table}] {self.key} resolves to "
f"cryptographic implementation crate {self.package!r}"
)


def dependency_package(
key: str,
value: object,
workspace_packages: dict[str, str],
) -> str:
if isinstance(value, str):
return key
if isinstance(value, dict):
if value.get("workspace") is True:
return workspace_packages.get(key, key)
package = value.get("package")
if isinstance(package, str):
return package
return key


def dependency_tables(manifest: dict[str, object]) -> list[tuple[str, object]]:
paths: list[tuple[str, object]] = []
for table in DEPENDENCY_TABLES:
if table in manifest:
paths.append((table, manifest[table]))
target = manifest.get("target")
if isinstance(target, dict):
for target_name, target_table in target.items():
if not isinstance(target_table, dict):
continue
for table in DEPENDENCY_TABLES:
if table in target_table:
paths.append((f"target.{target_name}.{table}", target_table[table]))
return paths


def workspace_dependencies(manifest: dict[str, object]) -> dict[str, object]:
workspace = manifest.get("workspace")
if not isinstance(workspace, dict):
return {}
dependencies = workspace.get("dependencies", {})
return dependencies if isinstance(dependencies, dict) else {}


def parse_manifests(root: Path) -> dict[Path, tuple[Path, dict[str, object]]]:
manifests = {}
for manifest_path in sorted(root.rglob("Cargo.toml")):
relative = manifest_path.relative_to(root)
if any(part in SKIPPED_DIRECTORIES for part in relative.parts):
continue
with manifest_path.open("rb") as file:
manifests[manifest_path] = (relative, tomllib.load(file))
return manifests


def owning_workspace(
manifest_path: Path,
root: Path,
manifests: dict[Path, tuple[Path, dict[str, object]]],
) -> Path | None:
directory = manifest_path.parent
while True:
candidate = directory / "Cargo.toml"
if candidate in manifests and isinstance(manifests[candidate][1].get("workspace"), dict):
return candidate
if directory == root:
return None
directory = directory.parent


def check_repository(root: Path) -> list[Violation]:
root = root.resolve()
manifests = parse_manifests(root)
workspace_packages = {
path: {
key: dependency_package(key, value, {})
for key, value in workspace_dependencies(manifest).items()
}
for path, (_, manifest) in manifests.items()
if isinstance(manifest.get("workspace"), dict)
}
violations: list[Violation] = []

for manifest_path, (relative, manifest) in manifests.items():
if relative.parts[0] == "crypto":
continue
workspace_path = owning_workspace(manifest_path, root, manifests)
inherited_packages = workspace_packages.get(workspace_path, {})

for key, value in workspace_dependencies(manifest).items():
package = dependency_package(key, value, {})
if package in FORBIDDEN_PACKAGES:
violations.append(
Violation(relative.as_posix(), "workspace.dependencies", key, package)
)

for table, dependencies in dependency_tables(manifest):
if not isinstance(dependencies, dict):
continue
for key, value in dependencies.items():
package = dependency_package(key, value, inherited_packages)
if package in FORBIDDEN_PACKAGES:
violations.append(
Violation(relative.as_posix(), table, key, package)
)

return violations


def main(argv: list[str]) -> int:
root = Path(argv[1]) if len(argv) > 1 else Path(__file__).resolve().parents[2]
violations = check_repository(root)
if not violations:
print("crypto dependency boundary is clean")
return 0
for violation in violations:
print(violation, file=sys.stderr)
return 1


if __name__ == "__main__":
raise SystemExit(main(sys.argv))
191 changes: 191 additions & 0 deletions .github/scripts/test_check_crypto_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from pathlib import Path
import tempfile
import textwrap
import unittest

from check_crypto_dependencies import check_repository


class CryptoDependencyBoundaryTest(unittest.TestCase):
def check(self, files: dict[str, str]):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
for relative, content in files.items():
path = root / relative
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(textwrap.dedent(content))
return check_repository(root)

def test_normal_dependency_outside_crypto_is_rejected(self):
violations = self.check(
{
"Cargo.toml": """\
[workspace]
members = ["service"]
""",
"service/Cargo.toml": """\
[dependencies]
sha2 = "0.11"
""",
}
)
self.assertEqual(
[(item.table, item.key, item.package) for item in violations],
[("dependencies", "sha2", "sha2")],
)

def test_renamed_dependency_is_checked_by_package_name(self):
violations = self.check(
{
"Cargo.toml": """\
[workspace]
members = ["service"]
""",
"service/Cargo.toml": """\
[dependencies]
crypto-hash = { package = "sha2", version = "0.11" }
""",
}
)
self.assertEqual(violations[0].package, "sha2")
self.assertEqual(violations[0].key, "crypto-hash")

def test_legacy_dependency_table_names_are_rejected(self):
for declared, expected in [
("build_dependencies", "build_dependencies"),
("target.'cfg(unix)'.dev_dependencies", "target.cfg(unix).dev_dependencies"),
]:
with self.subTest(table=declared):
violations = self.check(
{
"Cargo.toml": """\
[workspace]
members = ["service"]
""",
"service/Cargo.toml": f"""\
[{declared}]
backend = {{ package = "sha2", version = "0.11" }}
""",
}
)
self.assertEqual(
[(item.table, item.key, item.package) for item in violations],
[(expected, "backend", "sha2")],
)

def test_unused_workspace_alias_is_rejected(self):
violations = self.check(
{
"Cargo.toml": """\
[workspace]
members = ["service"]

[workspace.dependencies]
crypto-random = { package = "getrandom", version = "0.3" }
""",
"service/Cargo.toml": """\
[dependencies]
anyhow = "1"
""",
}
)
self.assertEqual(
[(item.table, item.key, item.package) for item in violations],
[("workspace.dependencies", "crypto-random", "getrandom")],
)

def test_standalone_workspace_inherited_alias_is_rejected_in_both_places(self):
violations = self.check(
{
"Cargo.toml": """\
[workspace]
members = []
exclude = ["tool"]
""",
"tool/Cargo.toml": """\
[workspace]
resolver = "2"

[workspace.dependencies]
crypto-hash = { package = "sha2", version = "0.11" }

[dev-dependencies]
crypto-hash = { workspace = true }
""",
}
)
self.assertEqual(
{(item.manifest, item.table) for item in violations},
{
("tool/Cargo.toml", "workspace.dependencies"),
("tool/Cargo.toml", "dev-dependencies"),
},
)

def test_crypto_members_and_allowed_dependencies_are_ignored(self):
violations = self.check(
{
"Cargo.toml": """\
[workspace]
members = ["crypto/backend", "service"]
""",
"crypto/backend/Cargo.toml": """\
[dependencies]
sha2 = "0.11"
rsa = "0.10"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
getrandom = "0.3"
""",
"service/Cargo.toml": """\
[dependencies]
reqsign-crypto-rustcrypto = { path = "../crypto/backend" }
base64 = "0.23"
zeroize = "1"

[dev-dependencies]
aws-sigv4 = "1.2"
""",
}
)
self.assertEqual(violations, [])

def test_target_directory_is_not_scanned(self):
violations = self.check(
{
"Cargo.toml": """\
[workspace]
members = ["service"]
""",
"service/Cargo.toml": """\
[dependencies]
anyhow = "1"
""",
"target/package/example/Cargo.toml": """\
[dependencies]
sha1 = "0.11"
""",
}
)
self.assertEqual(violations, [])


if __name__ == "__main__":
unittest.main()
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ jobs:
fi
done

- name: Check direct crypto dependencies
run: |
python3 .github/scripts/check_crypto_dependencies.py
python3 -m unittest discover -s .github/scripts -p 'test_check_crypto_dependencies.py'

licenses:
runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down
Loading
Loading