123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
using Forge.Features.Auth;
using Forge.Features.Browse;
using Forge.Features.Repos;
using Forge.Features.SmartHttp;
using Forge.Features.Webhooks;
using Forge.Git;
namespace Forge.Tests;
/// <summary>Pure logic that needs neither git nor the web app.</summary>
public class ParserTests
{
[Fact]
public void ParseTree_reads_modes_sizes_and_names_with_spaces()
{
const string output =
"100644 blob 1111111111111111111111111111111111111111 12\tfile with space.txt\0"
+ "040000 tree 2222222222222222222222222222222222222222 -\tsrc\0"
+ "160000 commit 3333333333333333333333333333333333333333 -\tvendor/lib\0";
var entries = GitParsers.ParseTree(output);
Assert.Equal(3, entries.Count);
Assert.Equal("file with space.txt", entries[0].Name);
Assert.Equal(12, entries[0].Size);
Assert.True(entries[0].IsBlob);
Assert.True(entries[1].IsTree);
Assert.Null(entries[1].Size);
Assert.Equal("lib", entries[2].Name);
Assert.True(entries[2].IsSubmodule);
}
[Fact]
public void ParseLog_splits_records_and_keeps_multiline_bodies()
{
var a = new string('a', 40);
var b = new string('b', 40);
var output =
$"{a}\0{b}\0Ann\0ann@example.com\01700000000\0Bob\0bob@example.com\01700000100\0Subject one\0Body line 1\nBody line 2\n\0\n"
+ $"{b}\0\0Ann\0ann@example.com\01600000000\0Ann\0ann@example.com\01600000000\0Root\0\0\n";
var commits = GitParsers.ParseLog(output);
Assert.Equal(2, commits.Count);
Assert.Equal(a, commits[0].Sha);
Assert.Equal([b], commits[0].Parents);
Assert.Equal("Body line 1\nBody line 2", commits[0].Body);
Assert.Equal("Bob", commits[0].CommitterName);
Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(1700000000), commits[0].AuthorDate);
Assert.Equal(b, commits[1].Sha);
Assert.Empty(commits[1].Parents);
Assert.Equal("", commits[1].Body);
}
[Fact]
public void ParseNumstat_handles_renames_and_binary_files()
{
const string output = "3\t1\tsrc/a.cs\0-\t-\tlogo.png\00\t0\t\0old name.txt\0new name.txt\0";
var stats = GitParsers.ParseNumstat(output);
Assert.Equal(3, stats.Count);
Assert.Equal(new FileStat("src/a.cs", null, 3, 1), stats[0]);
Assert.True(stats[1].IsBinary);
Assert.Equal("new name.txt", stats[2].Path);
Assert.Equal("old name.txt", stats[2].OldPath);
}
[Fact]
public void ParseDiff_builds_hunks_with_line_numbers()
{
const string patch = """
diff --git a/src/a.cs b/src/a.cs
index 1111111..2222222 100644
--- a/src/a.cs
+++ b/src/a.cs
@@ -1,3 +1,4 @@ class A
line one
-line two
+line 2
+line 2b
line three
\ No newline at end of file
diff --git a/logo.png b/logo.png
new file mode 100644
index 0000000..3333333
Binary files /dev/null and b/logo.png differ
diff --git a/old name.txt b/new name.txt
similarity index 100%
rename from old name.txt
rename to new name.txt
diff --git a/gone.txt b/gone.txt
deleted file mode 100644
index 4444444..0000000
--- a/gone.txt
+++ /dev/null
@@ -1 +0,0 @@
-bye
""";
var diff = GitParsers.ParseDiff(patch.ReplaceLineEndings("\n"));
Assert.False(diff.Truncated);
Assert.Equal(4, diff.Files.Count);
var modified = diff.Files[0];
Assert.Equal("src/a.cs", modified.NewPath);
Assert.Equal(FileChange.Modified, modified.Change);
Assert.Equal(2, modified.Added);
Assert.Equal(1, modified.Deleted);
var lines = Assert.Single(modified.Hunks).Lines;
Assert.Equal(new DiffLine(DiffLineKind.Context, 1, 1, "line one"), lines[0]);
Assert.Equal(new DiffLine(DiffLineKind.Deleted, 2, null, "line two"), lines[1]);
Assert.Equal(new DiffLine(DiffLineKind.Added, null, 2, "line 2"), lines[2]);
Assert.Equal(new DiffLine(DiffLineKind.Added, null, 3, "line 2b"), lines[3]);
Assert.Equal(new DiffLine(DiffLineKind.Context, 3, 4, "line three"), lines[4]);
Assert.Equal(DiffLineKind.NoNewline, lines[5].Kind);
var binary = diff.Files[1];
Assert.True(binary.IsBinary);
Assert.Equal(FileChange.Added, binary.Change);
Assert.Equal("logo.png", binary.NewPath);
var renamed = diff.Files[2];
Assert.Equal(FileChange.Renamed, renamed.Change);
Assert.Equal("old name.txt", renamed.OldPath);
Assert.Equal("new name.txt", renamed.NewPath);
var deleted = diff.Files[3];
Assert.Equal(FileChange.Deleted, deleted.Change);
Assert.Equal("gone.txt", deleted.DisplayPath);
}
[Fact]
public void ParseDiff_applies_file_and_line_limits()
{
var big = "diff --git a/big.txt b/big.txt\n--- a/big.txt\n+++ b/big.txt\n@@ -0,0 +1,5 @@\n+1\n+2\n+3\n+4\n+5\n";
var patch = big + "diff --git a/b.txt b/b.txt\n--- a/b.txt\n+++ b/b.txt\n@@ -1 +1 @@\n-x\n+y\n"
+ "diff --git a/c.txt b/c.txt\n--- a/c.txt\n+++ b/c.txt\n@@ -1 +1 @@\n-x\n+y\n";
var diff = GitParsers.ParseDiff(patch, maxFiles: 2, maxLinesPerFile: 3);
Assert.True(diff.Truncated);
Assert.Equal(2, diff.Files.Count);
Assert.True(diff.Files[0].TooLarge);
Assert.Empty(diff.Files[0].Hunks);
Assert.Equal(5, diff.Files[0].Added);
Assert.False(diff.Files[1].TooLarge);
}
[Theory]
[InlineData("plain.txt", "plain.txt")]
[InlineData("\"tab\\there.txt\"", "tab\there.txt")]
[InlineData("\"quote\\\"d.txt\"", "quote\"d.txt")]
[InlineData("\"\\303\\251t\\303\\251.txt\"", "été.txt")]
public void Unquote_reverses_git_path_quoting(string quoted, string expected) =>
Assert.Equal(expected, GitParsers.Unquote(quoted));
[Fact]
public void IsBinary_looks_for_nul_in_the_first_8000_bytes()
{
var late = new byte[9000];
Array.Fill(late, (byte)'a');
late[8500] = 0;
var early = (byte[])late.Clone();
early[100] = 0;
Assert.False(GitParsers.IsBinary(late));
Assert.True(GitParsers.IsBinary(early));
Assert.False(GitParsers.IsBinary("text"u8));
}
[Fact]
public void RefResolver_prefers_the_longest_matching_ref()
{
var sha1 = new string('1', 40);
var sha2 = new string('2', 40);
GitRef[] refs =
[
new("refs/heads/feature", "feature", RefKind.Branch, sha1, "commit", null, null, ""),
new("refs/heads/feature/x", "feature/x", RefKind.Branch, sha2, "commit", null, null, ""),
new("refs/tags/v1.0", "v1.0", RefKind.Tag, sha1, "tag", sha2, null, ""),
];
var deep = RefResolver.Match(refs, "feature/x/src/a.cs");
Assert.Equal(new ResolvedRef("feature/x", ResolvedKind.Branch, sha2, "src/a.cs"), deep);
var shallow = RefResolver.Match(refs, "feature/y/a.cs");
Assert.Equal(new ResolvedRef("feature", ResolvedKind.Branch, sha1, "y/a.cs"), shallow);
// An annotated tag resolves to the commit it points at.
Assert.Equal(new ResolvedRef("v1.0", ResolvedKind.Tag, sha2, ""), RefResolver.Match(refs, "v1.0"));
}
[Theory]
[InlineData("--upload-pack=touch /tmp/x")]
[InlineData("-n")]
[InlineData("feature/../x")]
[InlineData("nope")]
[InlineData("")]
public void RefResolver_rejects_anything_that_is_not_an_existing_ref(string input)
{
GitRef[] refs = [new("refs/heads/feature", "feature", RefKind.Branch, new string('1', 40), "commit", null, null, "")];
Assert.Null(RefResolver.Match(refs, input));
}
[Theory]
[InlineData("forge", true)]
[InlineData("my.repo_2-x", true)]
[InlineData("-leading-dash", true)]
[InlineData("", false)]
[InlineData(".", false)]
[InlineData("..", false)]
[InlineData("a..b", false)]
[InlineData("a/b", false)]
[InlineData("a\\b", false)]
[InlineData("repo.git", false)]
[InlineData("repo.GIT", false)]
[InlineData("has space", false)]
[InlineData("name\n", false)]
[InlineData("ž", false)]
public void Repository_names_are_validated(string name, bool valid) =>
Assert.Equal(valid, RepoService.ValidateName(name) is null);
[Fact]
public void Repository_name_length_is_limited_to_100()
{
Assert.Null(RepoService.ValidateName(new string('a', 100)));
Assert.NotNull(RepoService.ValidateName(new string('a', 101)));
}
[Theory]
[InlineData("jessie", true)]
[InlineData("settings", false)]
[InlineData("Login", false)]
[InlineData("-x", false)]
[InlineData("a.b", false)]
public void Usernames_are_validated(string name, bool valid) =>
Assert.Equal(valid, AccountRules.ValidateUsername(name) is null);
[Fact]
public void PktLine_prefixes_the_total_length_in_hex() =>
Assert.Equal("001e# service=git-upload-pack\n", System.Text.Encoding.ASCII.GetString(SmartHttpEndpoints.PktLine("# service=git-upload-pack\n")));
[Theory]
[InlineData("version=2", "version=2")]
[InlineData("version=2:object-format=sha1", "version=2:object-format=sha1")]
[InlineData("version=2; rm -rf /", null)]
[InlineData("a b", null)]
[InlineData("", null)]
public void Git_protocol_header_is_validated(string header, string? expected) =>
Assert.Equal(expected, SmartHttpEndpoints.ValidateProtocolHeader(header));
[Fact]
public void Basic_auth_password_is_extracted()
{
var header = "Basic " + Convert.ToBase64String("git:forge_abc:def"u8);
Assert.Equal("forge_abc:def", SmartHttpEndpoints.ParseBasicPassword(header));
Assert.Null(SmartHttpEndpoints.ParseBasicPassword(null));
Assert.Null(SmartHttpEndpoints.ParseBasicPassword("Bearer x"));
Assert.Equal("", SmartHttpEndpoints.ParseBasicPassword("Basic !!!"));
}
[Fact]
public void Push_diff_reports_created_updated_and_deleted_refs()
{
var a = new string('a', 40);
var b = new string('b', 40);
var before = new Dictionary<string, string> { ["refs/heads/main"] = a, ["refs/heads/old"] = a };
var after = new Dictionary<string, string> { ["refs/heads/main"] = b, ["refs/tags/v1"] = b };
var changes = PushRecorder.Diff(before, after);
Assert.Equal(
[
new RefChange("refs/heads/main", a, b),
new RefChange("refs/heads/old", a, GitParsers.ZeroSha),
new RefChange("refs/tags/v1", GitParsers.ZeroSha, b),
], changes);
}
[Fact]
public void Tokens_are_hashed_with_sha256()
{
Assert.Equal(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
TokenService.Hash("abc"));
}
[Fact]
public void Webhook_signature_is_hmac_sha256_of_the_body()
{
// RFC 4231 test case 2.
Assert.Equal(
"sha256=5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
WebhookSender.Sign("Jefe", "what do ya want for nothing?"u8));
}
[Fact]
public void Markdown_is_sanitized_and_relative_links_point_into_the_repository()
{
var links = new LinkContext("/o/r/raw/main", "/o/r/blob/main", "docs");
var html = new MarkdownRenderer().Render(
"""
# Title
 [guide](../GUIDE.md) [root](/LICENSE) [ext](https://example.com/x) [anchor](#top)
<script>alert(1)</script>
<img src="x.png" onerror="alert(1)">
<a href="javascript:alert(1)">bad</a>
""",
links);
Assert.Contains("src=\"/o/r/raw/main/docs/img/logo.png\"", html);
Assert.Contains("href=\"/o/r/blob/main/GUIDE.md\"", html);
Assert.Contains("href=\"/o/r/blob/main/LICENSE\"", html);
Assert.Contains("src=\"/o/r/raw/main/docs/x.png\"", html);
Assert.Contains("href=\"#top\"", html);
Assert.Matches("<a[^>]*href=\"https://example.com/x\"[^>]*rel=\"nofollow noopener\"", html);
Assert.DoesNotContain("<script", html);
Assert.DoesNotContain("onerror", html);
Assert.DoesNotContain("javascript:", html);
}
[Fact]
public void Auth_failure_limiter_blocks_after_repeated_failures()
{
var limiter = new AuthFailureLimiter(TimeProvider.System);
for (var i = 0; i < AuthFailureLimiter.MaxFailures; i++)
{
Assert.False(limiter.IsBlocked("1.2.3.4"));
limiter.RecordFailure("1.2.3.4");
}
Assert.True(limiter.IsBlocked("1.2.3.4"));
Assert.False(limiter.IsBlocked("5.6.7.8"));
}
}