Skip to content

Commit e5b8bf3

Browse files
committed
fix malware blacklist compatibility with SMAPI 4.5.2 and earlier
The revamped blacklist is more compact, which triggers the bug fixed in f090df0 (where syncing a smaller new version corrupts the file). This pads the file to the largest previous blacklist size to avoid the issue.
1 parent 5f519d6 commit e5b8bf3

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

‎src/SMAPI.Web/BackgroundJobs/MalwareBlacklistJob.cs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ internal class MalwareBlacklistJob : IBackgroundJob
2020
/*********
2121
** Fields
2222
*********/
23+
/// <summary>The minimum size in bytes for the generated malware blacklist file.</summary>
24+
/// <remarks>The generated file is padded to this length, to avoid a bug in SMAPI 4.5.2 and earlier where a smaller newer version corrupts the file (since it overwrites the previous file without truncating).</remarks>
25+
private const int MinBlacklistBytes = 8589;
26+
2327
/// <summary>The HTTP client for fetching data from GitHub.</summary>
2428
private readonly IGitHubClient GitHubClient;
2529

@@ -64,6 +68,7 @@ public async Task RunAsync(PerformContext? context)
6468
MalwareBlacklistConfig config = this.Config.Value;
6569
string repo = config.GitHubRepo ?? throw new InvalidOperationException("Can't update the malware blacklist: no GitHub repository is configured.");
6670
string outputPath = Path.Combine(this.WebRootPath, Startup.BlacklistFileName);
71+
UTF8Encoding encoding = new(encoderShouldEmitUTF8Identifier: false);
6772

6873
// fetch file
6974
GitFile file;
@@ -93,10 +98,15 @@ await this.GitHubClient.GetFileAsync(repo, config.FilePath, config.GitRef, previ
9398
string publicJson = MalwareBlacklistConverter.ToPublicJson(blacklist);
9499
context.WriteLine($"Updated malware blacklist ({blacklist.Blacklist.Length} mod entries and {blacklist.LooseFileBlacklist.Length} loose file entries).");
95100

101+
// pad to minimum size
102+
int byteCount = encoding.GetByteCount(publicJson);
103+
if (byteCount < MalwareBlacklistJob.MinBlacklistBytes)
104+
publicJson += new string(' ', MalwareBlacklistJob.MinBlacklistBytes - byteCount);
105+
96106
// save file
97107
{
98108
string tempPath = $"{outputPath}.tmp";
99-
await File.WriteAllTextAsync(tempPath, publicJson, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
109+
await File.WriteAllTextAsync(tempPath, publicJson, encoding);
100110
File.Move(tempPath, outputPath, overwrite: true);
101111

102112
context.WriteLine($"Saved to {Startup.BlacklistFileName}.");

0 commit comments

Comments
 (0)