Skip to content

Commit 729a894

Browse files
committed
check for executable files in some archives
1 parent 79d9ad6 commit 729a894

14 files changed

Lines changed: 75 additions & 15 deletions

CompatBot/EventHandlers/LogParsing/ArchiveHandlers/PlainText.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ internal sealed class PlainTextHandler: IArchiveHandler
1414
public Result CanHandle(string fileName, int fileSize, ReadOnlySpan<byte> header)
1515
{
1616
LogSize = fileSize;
17+
if (fileName.HasExecutableExtension())
18+
return Result.Failure().WithCode("executable");
19+
1720
if (fileName.Contains("tty.log", StringComparison.InvariantCultureIgnoreCase))
1821
return Result.Failure();
1922

CompatBot/EventHandlers/LogParsing/ArchiveHandlers/RarHandler.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ internal sealed class RarHandler: IArchiveHandler
1717
public Result CanHandle(string fileName, int fileSize, ReadOnlySpan<byte> header)
1818
{
1919
if (header.Length >= Header.Length && header[..Header.Length].SequenceEqual(Header)
20-
|| header.Length == 0 && fileName.EndsWith(".rar", StringComparison.InvariantCultureIgnoreCase))
20+
|| header is [] && fileName.EndsWith(".rar", StringComparison.InvariantCultureIgnoreCase))
2121
{
2222
var firstEntry = Encoding.ASCII.GetString(header);
2323
if (!firstEntry.Contains(".log", StringComparison.InvariantCultureIgnoreCase))
24+
{
25+
var idx = -1;
26+
while ((idx = firstEntry.IndexOf('.', idx + 1)) > -1 && idx < firstEntry.Length - 4)
27+
{
28+
if (firstEntry[idx..(idx + 4)].ToLower().HasExecutableExtension())
29+
return Result.Failure().WithCode("executable");
30+
}
2431
return Result.Failure().WithMessage("Archive doesn't contain any logs.");
32+
}
2533
return Result.Success();
2634
}
2735
return Result.Failure();

CompatBot/EventHandlers/LogParsing/ArchiveHandlers/ZipHandler.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ public Result CanHandle(string fileName, int fileSize, ReadOnlySpan<byte> header
2222
{
2323
var firstEntry = Encoding.ASCII.GetString(header);
2424
if (!firstEntry.Contains(".log", StringComparison.InvariantCultureIgnoreCase))
25+
{
26+
var idx = -1;
27+
while ((idx = firstEntry.IndexOf('.', idx + 1)) > -1 && idx < firstEntry.Length - 4)
28+
{
29+
if (firstEntry[idx..(idx + 4)].ToLower().HasExecutableExtension())
30+
return Result.Failure().WithCode("executable");
31+
}
2532
return Result.Failure().WithMessage("Archive doesn't contain any logs.");
33+
}
2634
return Result.Success();
2735
}
2836
return Result.Failure();

CompatBot/EventHandlers/LogParsing/SourceHandlers/DiscordAttachmentHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override async Task<Result<ISource>> FindHandlerAsync(DiscordMessage mess
2828
var result = handler.CanHandle(attachment.FileName, attachment.FileSize, buf.AsSpan(0, read));
2929
if (result.IsSuccess())
3030
return Result.Success<ISource>(new DiscordAttachmentSource(attachment, handler, attachment.FileName, attachment.FileSize));
31-
else if (result.Message is {Length: >0})
31+
else if (result is {Code.Length: >0} or {Message.Length: >0} )
3232
return result.Cast<ISource>();
3333
}
3434
}

CompatBot/EventHandlers/LogParsing/SourceHandlers/DropboxHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public override async Task<Result<ISource>> FindHandlerAsync(DiscordMessage mess
5656
var result = handler.CanHandle(filename, filesize, buf.AsSpan(0, read));
5757
if (result.IsSuccess())
5858
return Result.Success<ISource>(new DropboxSource(uri, handler, filename, filesize));
59-
else if (result.Message is {Length: >0})
59+
else if (result is {Code.Length: >0} or {Message.Length: >0} )
6060
return result.Cast<ISource>();
6161
}
6262
}

CompatBot/EventHandlers/LogParsing/SourceHandlers/GenericLinkHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public override async Task<Result<ISource>> FindHandlerAsync(DiscordMessage mess
5555
var result = handler.CanHandle(filename, filesize, buf.AsSpan(0, read));
5656
if (result.IsSuccess())
5757
return Result.Success<ISource>(new GenericSource(uri, handler, host, filename, filesize));
58-
else if (result.Message is {Length: > 0})
58+
else if (result is {Code.Length: >0} or {Message.Length: >0} )
5959
return result.Cast<ISource>();
6060
}
6161
}

CompatBot/EventHandlers/LogParsing/SourceHandlers/GoogleDriveHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public override async Task<Result<ISource>> FindHandlerAsync(DiscordMessage mess
5757
var result = handler.CanHandle(fileMeta.Name, (int)fileMeta.Size, buf.AsSpan(0, read));
5858
if (result.IsSuccess())
5959
return Result.Success<ISource>(new GoogleDriveSource(client, fileInfoRequest, fileMeta, handler));
60-
else if (result.Message is {Length: >0})
60+
else if (result is {Code.Length: >0} or {Message.Length: >0} )
6161
return result.Cast<ISource>();
6262
}
6363
}

CompatBot/EventHandlers/LogParsing/SourceHandlers/MediafireHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public override async Task<Result<ISource>> FindHandlerAsync(DiscordMessage mess
6161
var result = handler.CanHandle(filename, filesize, buf.AsSpan(0, read));
6262
if (result.IsSuccess())
6363
return Result.Success<ISource>(new MediafireSource(directLink, handler, filename, filesize));
64-
else if (result.Message is {Length: >0})
64+
else if (result is {Code.Length: >0} or {Message.Length: >0} )
6565
return result.Cast<ISource>();
6666
}
6767
Config.Log.Debug("MediaFire Response:\n" + Encoding.UTF8.GetString(buf, 0, read));

CompatBot/EventHandlers/LogParsing/SourceHandlers/MegaHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override async Task<Result<ISource>> FindHandlerAsync(DiscordMessage mess
4747
var result = handler.CanHandle(node.Name, (int)node.Size, buf.AsSpan(0, read));
4848
if (result.IsSuccess())
4949
return Result.Success<ISource>(new MegaSource(client, uri, node, handler));
50-
else if (result.Message is {Length: >0})
50+
else if (result is {Code.Length: >0} or {Message.Length: >0} )
5151
return result.Cast<ISource>();
5252
}
5353
}

CompatBot/EventHandlers/LogParsing/SourceHandlers/OneDriveSourceHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override async Task<Result<ISource>> FindHandlerAsync(DiscordMessage mess
4747
var result = handler.CanHandle(filename, filesize, buf.AsSpan(0, read));
4848
if (result.IsSuccess())
4949
return Result.Success<ISource>(new OneDriveSource(uri, handler, filename, filesize));
50-
else if (result.Message is {Length: >0})
50+
else if (result is {Code.Length: >0} or {Message.Length: >0} )
5151
return result.Cast<ISource>();
5252
}
5353
}

0 commit comments

Comments
 (0)