Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fully sanitized archive.org bucket names #4043

Merged
merged 1 commit into from
Feb 28, 2024
Merged
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
39 changes: 28 additions & 11 deletions Core/Types/CkanModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,17 +755,34 @@ public string DescribeInstallStanzas(IGame game)
/// Here it's the first 8 characters of the SHA1 of the DOWNLOADED FILE, not the URL!
/// </summary>
public Uri InternetArchiveDownload
{
get
{
string verStr = version.ToString().Replace(' ', '_').Replace(':', '-');
// Some alternate registry repositories don't set download_hash
return (download_hash?.sha1 != null && license.All(l => l.Redistributable))
? new Uri(
$"https://archive.org/download/{identifier}-{verStr}/{download_hash.sha1.Substring(0, 8)}-{identifier}-{verStr}.zip")
: null;
}
}
=> !license.Any(l => l.Redistributable)
? null
: InternetArchiveURL(
Truncate(bucketExcludePattern.Replace(identifier + "-"
+ version.ToString()
.Replace(' ', '_')
.Replace(':', '-'),
""),
100),
// Some alternate registry repositories don't set download_hash
download_hash?.sha1);

private static string Truncate(string s, int len)
=> s.Length <= len ? s
: s.Substring(0, len);

private static Uri InternetArchiveURL(string bucket, string sha1)
=> string.IsNullOrEmpty(sha1)
? null
: new Uri($"https://archive.org/download/{bucket}/{sha1.Substring(0, 8)}-{bucket}.zip");

// InternetArchive says:
// Bucket names should be valid archive identifiers;
// try someting matching this regular expression:
// ^[a-zA-Z0-9][a-zA-Z0-9_.-]{4,100}$
// (We enforce everything except the minimum of 4 characters)
private static readonly Regex bucketExcludePattern = new Regex(@"^[^a-zA-Z0-9]+|[^a-zA-Z0-9._-]",
RegexOptions.Compiled);

private const double K = 1024;

Expand Down
Loading