Phase 1 foundations in the Anubis library: - Platform: startup configuration from environment variables (Docker) or config.toml (desktop, in-house TOML subset), data folder layout, secret key custody (ANUBIS_SECRET_KEY, DPAPI on Windows, restricted file elsewhere), installation identity and database fingerprint. - Db: ADO.NET with Npgsql and Microsoft.Data.Sqlite, money as NUMERIC(38,18) or canonical text rounded to 18 decimals, UTC moments, UUIDv7 ids; numbered SQL migrations with type tokens and a version table; engine-independent export/import with count and accounting-total checks; backups keeping 14. - Core: injectable clock, channel event bus, scheduler with fixed, interval and market-relative times, settings with change log, AES-GCM secrets, log redaction, Italian formats. - Access: PBKDF2-SHA512 hashes, RFC 6238 TOTP, hashed session tokens, sign-in attempt limit. SQLitePCLRaw (Apache-2.0) arrives with Microsoft.Data.Sqlite and is declared in DependencyPolicyTests and API-NOTES. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
91 lines
4.7 KiB
C#
91 lines
4.7 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace Anubis.Tests;
|
|
|
|
/// <summary>SPEC 2.2: no secret in the repository. Only .env.example with fake values.</summary>
|
|
[TestClass]
|
|
public sealed partial class SecretScanTests
|
|
{
|
|
private static readonly HashSet<string> _binaryExtensions = new(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
".png", ".jpg", ".jpeg", ".ico", ".gif", ".webp", ".ttf", ".woff", ".woff2", ".onnx", ".dll", ".exe", ".pdb", ".zip", ".gz",
|
|
};
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public void RepositoryContainsNoSecrets()
|
|
{
|
|
var findings = new List<string>();
|
|
foreach (var file in Repo.Files().Where(f => !_binaryExtensions.Contains(f.Extension)))
|
|
{
|
|
var lineNumber = 0;
|
|
foreach (var line in File.ReadLines(file.FullName))
|
|
{
|
|
lineNumber++;
|
|
if (LooksLikeSecret(line))
|
|
{
|
|
findings.Add($"{Repo.Relative(file)}:{lineNumber}");
|
|
}
|
|
}
|
|
}
|
|
|
|
Assert.IsEmpty(findings, "Possibili segreti: " + string.Join(", ", findings));
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public void DetectorFindsTypicalSecretsAndIgnoresPlaceholders()
|
|
{
|
|
Assert.IsTrue(LooksLikeSecret("-----BEGIN " + "PRIVATE KEY-----"));
|
|
Assert.IsTrue(LooksLikeSecret("APCA-API-SECRET-KEY: " + new string('a', 40)));
|
|
Assert.IsTrue(LooksLikeSecret("OK-ACCESS-PASSPHRASE=" + "Sup3rS3cretPass"));
|
|
Assert.IsTrue(LooksLikeSecret("TELEGRAM_BOT_TOKEN=123456789:" + new string('A', 35)));
|
|
Assert.IsTrue(LooksLikeSecret("api_key = \"" + "AKZXCVBNMASDFGHJKLQWERTY12" + "\""));
|
|
Assert.IsTrue(LooksLikeSecret("ANUBIS_SECRET_KEY=" + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822c"));
|
|
Assert.IsTrue(LooksLikeSecret("OKX_API_PASSPHRASE=" + "MyOkx2026Pass"));
|
|
Assert.IsTrue(LooksLikeSecret("FINNHUB_API_KEY: " + "c1a2b3c4d5e6f7g8h9i0"));
|
|
Assert.IsTrue(LooksLikeSecret("\"AlpacaSecret\": \"" + "Zx9Kq2Lm7Np4Rs8T" + "\""));
|
|
|
|
Assert.IsFalse(LooksLikeSecret("ANUBIS_SECRET_KEY=replace-with-random"));
|
|
Assert.IsFalse(LooksLikeSecret("APCA-API-SECRET-KEY: <your-secret>"));
|
|
Assert.IsFalse(LooksLikeSecret("POSTGRES_PASSWORD: anubis-dev"));
|
|
Assert.IsFalse(LooksLikeSecret("ANUBIS_DB_PASSWORD: ${ANUBIS_DB_PASSWORD:-anubis-dev}"));
|
|
Assert.IsFalse(LooksLikeSecret("REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}"));
|
|
Assert.IsFalse(LooksLikeSecret("private const string SecretKeyVariable = \"ANUBIS_SECRET_KEY\";"));
|
|
Assert.IsFalse(LooksLikeSecret("OKX_API_PASSPHRASE=<passphrase-123>"));
|
|
|
|
// Code that assigns a computed value is not a secret.
|
|
Assert.IsFalse(LooksLikeSecret("var key = RandomNumberGenerator.GetBytes(32);"));
|
|
Assert.IsFalse(LooksLikeSecret("builder.Password = Uri.UnescapeDataString(userInfo[1]);"));
|
|
Assert.IsFalse(LooksLikeSecret("?secret={Base32.Encode(secret)}&issuer=x"));
|
|
}
|
|
|
|
private static bool LooksLikeSecret(string line) =>
|
|
PrivateKey().IsMatch(line) || BrokerHeader().IsMatch(line) || TelegramToken().IsMatch(line) || GenericSecret().IsMatch(line)
|
|
|| NamedSecret().Matches(line).Any(m => !IsPlaceholder(m.Groups["value"].Value));
|
|
|
|
// Example values in .env.example, compose files and the CI are clearly fake.
|
|
private static bool IsPlaceholder(string value) =>
|
|
value.StartsWith('$') || value.StartsWith('<')
|
|
|| _placeholderWords.Any(word => value.Contains(word, StringComparison.OrdinalIgnoreCase));
|
|
|
|
private static readonly string[] _placeholderWords = ["replace", "change-me", "example", "your-", "dummy", "fake", "xxxx"];
|
|
|
|
[GeneratedRegex("-----BEGIN (RSA |EC |OPENSSH |DSA )?PRIVATE KEY-----")]
|
|
private static partial Regex PrivateKey();
|
|
|
|
[GeneratedRegex("""(APCA-API-(KEY-ID|SECRET-KEY)|OK-ACCESS-(KEY|SIGN|PASSPHRASE))\s*[:=]\s*["']?[A-Za-z0-9/+=_\-]{12,}""")]
|
|
private static partial Regex BrokerHeader();
|
|
|
|
[GeneratedRegex(@"\b\d{8,10}:[A-Za-z0-9_\-]{35}\b")]
|
|
private static partial Regex TelegramToken();
|
|
|
|
[GeneratedRegex("""(?i)(api[_-]?key|secret|token|passphrase|password)["']?\s*[:=]\s*["']?(?=[A-Za-z0-9/+=_\-]*\d)(?=[A-Za-z0-9/+=_\-]*[A-Za-z])[A-Za-z0-9/+=_\-]{24,}""")]
|
|
private static partial Regex GenericSecret();
|
|
|
|
// Variables named like the project's secrets (…KEY, …SECRET, …TOKEN, …PASSPHRASE, …PASSWORD) with a value
|
|
// of at least 8 characters that mixes letters and digits, as real keys and passwords do.
|
|
[GeneratedRegex("""(?i)[A-Za-z0-9_\-]*(key|secret|token|passphrase|password)["']?\s*[:=]\s*["']?(?<value>(?=[^\s"',;#]*\d)(?=[^\s"',;#]*[A-Za-z])[^\s"',;#(){}\[\]<>]{8,})(?=$|[\s"',;#])""")]
|
|
private static partial Regex NamedSecret();
|
|
}
|