Files
ucvl-zonghengjia-open-source/windows/GitReleasePolicySelfTest.cs
2026-10-11 23:23:20 +00:00

92 lines
5.8 KiB
C#

using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace UCVL.VPNPlus;
// In-memory adversarial responses. No real metadata is published or downloaded.
internal static class GitReleasePolicySelfTest
{
private const string Name = "UCVL-Zonghengjia-0.2.16-win-x64-setup.exe";
private const string Url = "https://pro.ucvl.cn/admin/ucvl-software-downloads/releases/download/windows-v0.2.16/" + Name;
private sealed class Handler(string mutation) : HttpMessageHandler
{
internal bool OnlyPublicUnauthenticated { get; private set; } = true;
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token)
{
token.ThrowIfCancellationRequested();
OnlyPublicUnauthenticated &= request.RequestUri?.Host == "pro.ucvl.cn"
&& request.RequestUri.Scheme == "https" && request.Headers.Authorization is null;
var api = request.RequestUri!.AbsolutePath.EndsWith("/releases", StringComparison.Ordinal);
var release = JsonNode.Parse(JsonSerializer.Serialize(new {
tag_name="windows-v0.2.16", draft=false, prerelease=false, body="fixture",
assets=new[] {
new { name=Name, size=300000, browser_download_url="http://172.16.0.21:3000/untrusted.exe" },
new { name=Name+".sha256", size=120, browser_download_url="https://untrusted.invalid/sha" },
},
}))!;
var assets = release["assets"]!.AsArray();
if (mutation == "duplicate_installer") assets.Add(assets[0]!.DeepClone());
if (mutation == "duplicate_checksum") assets.Add(assets[1]!.DeepClone());
if (mutation == "missing_installer") assets.RemoveAt(0);
if (mutation == "oversized_installer") assets[0]!["size"] = 200L * 1024 * 1024 + 1;
if (mutation == "four_part_version") release["tag_name"] = "windows-v9.0.0.1";
if (mutation == "missing_newest_no_fallback") {
release["tag_name"] = "windows-v0.2.17";
assets.Clear();
}
var text = api ? new JsonArray(release).ToJsonString() : new string('a',64) + " " + Name;
if (api && mutation == "missing_newest_no_fallback") {
var list=JsonNode.Parse(text)!.AsArray();
var old=release.DeepClone(); old["tag_name"]="windows-v0.2.15";
old["assets"]=new JsonArray(
new JsonObject { ["name"]="UCVL-Zonghengjia-0.2.15-win-x64-setup.exe", ["size"]=300000 },
new JsonObject { ["name"]="UCVL-Zonghengjia-0.2.15-win-x64-setup.exe.sha256", ["size"]=120 });
list.Add(old); text=list.ToJsonString();
}
if (!api && mutation == "wrong_checksum_filename") text=new string('a',64)+" other.exe";
if (!api && mutation == "missing_newest_no_fallback") text=new string('a',64)+" UCVL-Zonghengjia-0.2.15-win-x64-setup.exe";
if (!api && mutation == "checksum_extra_line") text+="\n"+text;
if (!api && mutation == "oversized_checksum") text=new string('a',4097);
if (api && mutation.StartsWith("oversized_metadata", StringComparison.Ordinal)) text=new string(' ',2*1024*1024+1);
var response=new HttpResponseMessage(mutation == "redirect" ? HttpStatusCode.Found : HttpStatusCode.OK) {
Content=new ByteArrayContent(Encoding.UTF8.GetBytes(text)),
};
if (mutation == "oversized_metadata_stream") response.Content.Headers.ContentLength = 1;
if (mutation == "redirect") response.Headers.Location=new Uri("https://untrusted.invalid/redirect");
return Task.FromResult(response);
}
}
internal static async Task RunAsync(Dictionary<string, object> report, string directory, IProgress<int> progress)
{
foreach (var mutation in new[] { "duplicate_installer", "duplicate_checksum", "missing_installer",
"oversized_installer", "four_part_version", "wrong_checksum_filename", "checksum_extra_line",
"oversized_checksum", "oversized_metadata", "oversized_metadata_stream", "redirect",
"missing_newest_no_fallback" })
{
var rejected=false;
try { await GitReleaseUpdater.GetLatestAsync(CancellationToken.None,new Handler(mutation),"0.2.15"); }
catch (Exception error) when (error is InvalidOperationException or HttpRequestException) { rejected=true; }
report["git_rejects_"+mutation]=rejected;
}
var validHandler=new Handler("normal");
var info=await GitReleaseUpdater.GetLatestAsync(CancellationToken.None,validHandler,"0.2.15");
report["git_ignores_attachment_urls_uses_public_unauthenticated_origin"]=info.DownloadUrl==Url && validHandler.OnlyPublicUnauthenticated;
foreach (var invalid in new[] { Url.Replace("https:","http:"), Url.Replace("pro.ucvl.cn","pro.ucvl.cn:444"),
Url.Replace("pro.ucvl.cn","user@pro.ucvl.cn"), Url+"?token=test", Url+"#fragment",
"https://pro.ucvl.cn/attachments/fixture.exe", Url.Replace("0.2.16/","0.2.15/") })
{
var rejected=false;
try {
await ClientUpdater.DownloadForTestAsync(new ClientUpdateInfo {
Version="0.2.16",Available=true,DownloadUrl=invalid,Size=300000,Sha256=new string('a',64),
},CancellationToken.None,progress,directory,new Handler("must_not_be_called"));
} catch (InvalidOperationException) { rejected=true; }
if (!rejected) throw new InvalidOperationException("Unexpected installer address accepted: "+invalid);
}
report["downloader_rejects_noncanonical_origins_and_paths"]=true;
}
}