diff --git a/.github/workflows/near-tools.yml b/.github/workflows/near-tools.yml new file mode 100644 index 0000000..8fd69c4 --- /dev/null +++ b/.github/workflows/near-tools.yml @@ -0,0 +1,24 @@ +name: near-tools + +on: + push: + branches: [main] + paths: + - "scripts/near/**" + - ".github/workflows/near-tools.yml" + pull_request: + paths: + - "scripts/near/**" + - ".github/workflows/near-tools.yml" + +permissions: + contents: read + +jobs: + credential-output: + name: credential output regression tests + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - run: python3 -m unittest discover -s scripts/near -p 'test_*.py' -v diff --git a/contracts/approval/Cargo.lock b/contracts/approval/Cargo.lock index 718a0a4..5fbd9a9 100644 --- a/contracts/approval/Cargo.lock +++ b/contracts/approval/Cargo.lock @@ -298,12 +298,6 @@ dependencies = [ "shlex", ] -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - [[package]] name = "cfg-if" version = "1.0.5" @@ -333,7 +327,7 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "cpufeatures 0.3.1", "rand_core 0.10.1", ] @@ -457,7 +451,7 @@ version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", ] [[package]] @@ -528,7 +522,7 @@ version = "4.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "cpufeatures 0.2.17", "curve25519-dalek-derive", "digest 0.10.7", @@ -624,7 +618,7 @@ version = "6.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6361d5c062261c78a176addb82d4c821ae42bed6089de0e12603cd25de2059c" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "crossbeam-utils", "hashbrown 0.14.5", "lock_api", @@ -911,7 +905,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -942,7 +936,7 @@ version = "0.2.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c287a33c7f0a620c38e641e7f60827713987b3c0f26e8ddc9462cc69cf75759" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "libc", ] @@ -1098,7 +1092,7 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "libc", "wasi", ] @@ -1109,7 +1103,7 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "libc", "r-efi", "rand_core 0.10.1", @@ -1552,7 +1546,7 @@ version = "0.3.106" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7883d941dae510fb2d978fc3fe018c71c9e2892fd38854de3e8b92c2e5ad9cc5" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "futures-util", "wasm-bindgen", ] @@ -1623,7 +1617,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8f198d1db720e4940b5a493201d199d9f24f568f8f746bd13706243a2f71598" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "cpufeatures 0.3.1", ] @@ -1691,12 +1685,6 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" -[[package]] -name = "memory_units" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" - [[package]] name = "miniz_oxide" version = "0.9.1" @@ -2101,7 +2089,6 @@ dependencies = [ "serde_json", "serde_with", "unicode-segmentation", - "wee_alloc", ] [[package]] @@ -2358,7 +2345,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77823a27f0babb03091cb9ed9ef80af3b39dbc82f97e8fa530374b7dafd87a45" dependencies = [ "bitflags 2.13.2", - "cfg-if 1.0.5", + "cfg-if", "foreign-types", "libc", "openssl-macros", @@ -2434,7 +2421,7 @@ version = "0.9.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "libc", "redox_syscall", "smallvec", @@ -2787,7 +2774,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" dependencies = [ "cc", - "cfg-if 1.0.5", + "cfg-if", "getrandom 0.2.17", "libc", "untrusted 0.9.0", @@ -2828,7 +2815,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -3133,7 +3120,7 @@ version = "0.10.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "cpufeatures 0.2.17", "digest 0.10.7", ] @@ -3144,7 +3131,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "446ba717509524cb3f22f17ecc096f10f4822d76ab5c0b9822c5f9c284e825f4" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "cpufeatures 0.3.1", "digest 0.11.3", ] @@ -3394,7 +3381,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -3806,7 +3793,7 @@ version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409" dependencies = [ - "cfg-if 1.0.5", + "cfg-if", "once_cell", "rustversion", "wasm-bindgen-macro", @@ -3875,18 +3862,6 @@ dependencies = [ "rustls-pki-types", ] -[[package]] -name = "wee_alloc" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" -dependencies = [ - "cfg-if 0.1.10", - "libc", - "memory_units", - "winapi", -] - [[package]] name = "winapi" version = "0.3.9" diff --git a/contracts/approval/contract/Cargo.toml b/contracts/approval/contract/Cargo.toml index 25eb84d..8c2265a 100644 --- a/contracts/approval/contract/Cargo.toml +++ b/contracts/approval/contract/Cargo.toml @@ -12,10 +12,11 @@ crate-type = ["cdylib", "rlib"] [dependencies] approval-core = { path = "../core", features = ["borsh"] } -near-sdk = "5.29" +# Use Rust's standard allocator instead of the SDK's unmaintained wee_alloc default. +near-sdk = { version = "5.29", default-features = false } [dev-dependencies] -near-sdk = { version = "5.29", features = ["unit-testing"] } +near-sdk = { version = "5.29", default-features = false, features = ["unit-testing"] } # `cargo near build reproducible-wasm` builds inside this pinned image, so the # wasm's code hash can be reproduced from the source at a given commit. diff --git a/scripts/near/README.md b/scripts/near/README.md index c02836c..a2256cf 100644 --- a/scripts/near/README.md +++ b/scripts/near/README.md @@ -377,6 +377,23 @@ for commit `081a3a6`). | `.sandbox/`, `.cache/` | Local sandbox state and the cargo-near download cache (gitignored). | | `../../.near-credentials/` | Principal key files (gitignored). | +## Credential output regression tests + +From the repository root, run: + +```bash +python3 -m unittest discover -s scripts/near -p 'test_*.py' -v +``` + +The tests use a public RFC 8032 test vector to check that key-management +commands print only the public key, never its private seed or full secret. +They also check error output, CLI redaction, and mode 0600 on generated +credential files. NEAR ed25519 secret keys encode `seed || public_key`; +`parse_secret_key` exposes only the last 32 bytes as its public-key result. +CodeQL can carry the input's sensitive-data label onto that public result, +so its clear-text logging alerts on these public-key outputs are false +positives. Secret values still belong only in private files. + ## Limitations - Only ed25519 keys are generated and accepted for `NEAR_PRIVATE_KEY`. diff --git a/scripts/near/near_util.py b/scripts/near/near_util.py index 0e80bc0..4ee406d 100755 --- a/scripts/near/near_util.py +++ b/scripts/near/near_util.py @@ -76,7 +76,12 @@ def b58decode(text): def parse_secret_key(secret): - """ed25519: -> (secret_str, public_key_str).""" + """ed25519: -> (secret_str, public_key_str). + + Only the final 32 bytes form the public key returned in the second slot. + Printing that value is intentional: no seed bytes are included. The full + secret in the first slot must only be written to private credential files. + """ secret = secret.strip() if not secret.startswith("ed25519:"): if secret.startswith("secp256k1:"): diff --git a/scripts/near/test_near_util.py b/scripts/near/test_near_util.py new file mode 100644 index 0000000..7372eea --- /dev/null +++ b/scripts/near/test_near_util.py @@ -0,0 +1,120 @@ +"""CLI regression checks for public-key output and private credential storage.""" + +import json +from pathlib import Path +import stat +import subprocess +import sys +import tempfile +import unittest + +import near_util + + +class CredentialOutputTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + # Public RFC 8032 section 7.1 test vector, never a deployed credential. + self.seed = bytes.fromhex( + "9d61b19deffd5a60ba844af492ec2cc4" + "4449c5697b326919703bac031cae7f60" + ) + public = bytes.fromhex( + "d75a980182b10ab7d54bfed3c964073a" + "0ee172f3daa62325af021a68f707511a" + ) + self.key = "ed25519:" + near_util.b58encode(self.seed + public) + self.public = "ed25519:" + near_util.b58encode(public) + + def run_cli(self, *args, stdin=None, success=True): + result = subprocess.run( + [sys.executable, str(Path(near_util.__file__).resolve()), *map(str, args)], + input=stdin, capture_output=True, text=True, check=False, + ) + output = result.stdout + result.stderr + for value in (self.key, self.key.split(":", 1)[1], near_util.b58encode(self.seed)): + self.assertNotIn(value, output) + if success: + self.assertEqual(result.returncode, 0, result.stderr) + else: + self.assertNotEqual(result.returncode, 0) + return result + + def keyfile(self, **fields): + path = self.root / "validator.json" + fields.setdefault("account_id", "example.testnet") + fields.setdefault("public_key", self.public) + fields.setdefault("secret_key", self.key) + path.write_text(json.dumps(fields)) + return path + + def assert_private(self, path): + self.assertEqual(stat.S_IMODE(path.stat().st_mode), 0o600) + + def test_pubkey_prints_only_public_half(self): + result = self.run_cli("pubkey", stdin=self.key + "\n") + self.assertEqual(result.stdout, self.public + "\n") + + def test_write_keyfile_keeps_secret_in_private_file(self): + path = self.root / "key.json" + result = self.run_cli("write-keyfile", "--account", "example.testnet", + "--out", path, stdin=self.key) + self.assertEqual(result.stdout, self.public + "\n") + self.assertEqual(json.loads(path.read_text())["private_key"], self.key) + self.assert_private(path) + + def test_convert_keyfile_prints_only_public_key(self): + path = self.root / "converted.json" + result = self.run_cli("convert-keyfile", "--in", self.keyfile(), + "--account", "example.testnet", "--out", path) + self.assertEqual(result.stdout, self.public + "\n") + self.assertEqual(json.loads(path.read_text())["private_key"], self.key) + self.assert_private(path) + + def test_keyfile_info_does_not_include_private_fields(self): + result = self.run_cli("keyfile-info", self.keyfile()) + info = json.loads(result.stdout) + self.assertEqual(info["public_key"], self.public) + self.assertEqual(info["account_id"], "example.testnet") + self.assertNotIn("private_key", info) + self.assertNotIn("secret_key", info) + + def test_sandbox_env_prints_public_key_and_stores_private_key(self): + path = self.root / ".env" + result = self.run_cli( + "sandbox-env", "--validator-key", self.keyfile(), + "--account", "example.testnet", "--rpc-url", "http://127.0.0.1:3030", + "--credentials-dir", self.root / "credentials", + "--deployments-dir", self.root / "deployments", "--out", path, + ) + self.assertEqual(result.stdout, self.public + "\n") + self.assertIn("NEAR_PRIVATE_KEY=" + self.key, path.read_text()) + self.assert_private(path) + + def test_invalid_key_error_does_not_echo_input(self): + result = self.run_cli("pubkey", stdin=self.key + "!", success=False) + self.assertEqual(result.stdout, "") + + def test_mismatched_public_key_error_does_not_echo_secret(self): + other = "ed25519:" + near_util.b58encode(bytes(32)) + result = self.run_cli("keyfile-info", self.keyfile(public_key=other), success=False) + self.assertEqual(result.stdout, "") + + def test_cli_filter_redacts_secret_and_preserves_public_key(self): + result = self.run_cli("filter-cli", stdin=self.key + "\n" + self.public + "\n") + self.assertEqual(result.stdout, "ed25519:\n" + self.public + "\n") + + def test_env_append_status_does_not_echo_secret(self): + path = self.root / ".env" + args = ("env-append", "--keyfile", self.keyfile(), "--env-file", path, + "--prefix", "APPROVAL_AGENT", "--comment", "test principal") + self.assertEqual(self.run_cli(*args).stdout, "appended\n") + self.assertEqual(self.run_cli(*args).stdout, "unchanged\n") + self.assertIn("APPROVAL_AGENT_PRIVATE_KEY=" + self.key, path.read_text()) + self.assert_private(path) + + +if __name__ == "__main__": + unittest.main()