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
Fix secondary order XSS: Harden file type inputs #4635
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace BTCPayServer | ||
{ | ||
public class BufferizedFormFile : IFormFile | ||
{ | ||
private IFormFile _formFile; | ||
private MemoryStream _content; | ||
public byte[] Buffer { get; } | ||
BufferizedFormFile(IFormFile formFile, byte[] content) | ||
{ | ||
_formFile = formFile; | ||
Buffer = content; | ||
_content = new MemoryStream(content); | ||
} | ||
|
||
public string ContentType => _formFile.ContentType; | ||
|
||
public string ContentDisposition => _formFile.ContentDisposition; | ||
|
||
public IHeaderDictionary Headers => _formFile.Headers; | ||
|
||
public long Length => _formFile.Length; | ||
|
||
public string Name => _formFile.Name; | ||
|
||
public string FileName => _formFile.FileName; | ||
|
||
public static async Task<BufferizedFormFile> Bufferize(IFormFile formFile) | ||
{ | ||
if (formFile is BufferizedFormFile b) | ||
return b; | ||
var content = new byte[formFile.Length]; | ||
using var fs = formFile.OpenReadStream(); | ||
await fs.ReadAsync(content, 0, content.Length); | ||
return new BufferizedFormFile(formFile, content); | ||
} | ||
|
||
public void CopyTo(Stream target) | ||
{ | ||
_content.CopyTo(target); | ||
} | ||
|
||
public Task CopyToAsync(Stream target, CancellationToken cancellationToken = default) | ||
{ | ||
return _content.CopyToAsync(target, cancellationToken); | ||
} | ||
|
||
public Stream OpenReadStream() | ||
{ | ||
return _content; | ||
} | ||
|
||
public void Rewind() | ||
{ | ||
_content.Seek(0, SeekOrigin.Begin); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#nullable enable | ||
using System; | ||
using System.Linq; | ||
using Microsoft.EntityFrameworkCore; | ||
using NBitcoin.DataEncoders; | ||
|
||
namespace BTCPayServer | ||
{ | ||
public class FileTypeDetector | ||
{ | ||
// Thanks to https://www.garykessler.net/software/FileSigs_20220731.zip | ||
|
||
const string pictureSigs = | ||
"JPEG2000 image files,00 00 00 0C 6A 50 20 20,JP2,Picture,0,(null)\n" + | ||
"Bitmap image,42 4D,BMP|DIB,Picture,0,(null)\n" + | ||
"GIF file,47 49 46 38,GIF,Picture,0,00 3B\n" + | ||
"PNG image,89 50 4E 47 0D 0A 1A 0A,PNG|APNG,Picture,0,49 45 4E 44 AE 42 60 82\n" + | ||
"Generic JPEGimage fil,FF D8,JPE|JPEG|JPG,Picture,0,FF D9\n" + | ||
"JPEG-EXIF-SPIFF images,FF D8 FF,JFIF|JPE|JPEG|JPG,Picture,0,FF D9\n" + | ||
"SVG images, 3C 73 76 67,SVG,Picture,0,(null)\n" + | ||
"Google WebP image file, 52 49 46 46 XX XX XX XX 57 45 42 50,WEBP,Picture,0,(null)\n" + | ||
"AVIF image file, XX XX XX XX 66 74 79 70,AVIF,Picture,0,(null)\n"; | ||
|
||
readonly static (int[] Header, int[]? Trailer, string[] Extensions)[] headerTrailers; | ||
static FileTypeDetector() | ||
{ | ||
var lines = pictureSigs.Split('\n', StringSplitOptions.RemoveEmptyEntries); | ||
headerTrailers = new (int[] Header, int[]? Trailer, string[] Extensions)[lines.Length]; | ||
for (int i = 0; i < lines.Length; i++) | ||
{ | ||
var cells = lines[i].Split(','); | ||
headerTrailers[i] = ( | ||
DecodeData(cells[1]), | ||
cells[^1] == "(null)" ? null : DecodeData(cells[^1]), | ||
cells[2].Split('|').Select(p => $".{p}").ToArray() | ||
); | ||
} | ||
} | ||
|
||
private static int[] DecodeData(string pattern) | ||
{ | ||
pattern = pattern.Replace(" ", ""); | ||
int[] res = new int[pattern.Length / 2]; | ||
for (int i = 0; i < pattern.Length; i+=2) | ||
{ | ||
var b = pattern[i..(i + 2)]; | ||
if (b == "XX") | ||
res[i/2] = -1; | ||
else | ||
res[i/2] = byte.Parse(b, System.Globalization.NumberStyles.HexNumber); | ||
} | ||
return res; | ||
} | ||
|
||
public static bool IsPicture(byte[] bytes, string? filename) | ||
{ | ||
for (int i = 0; i < headerTrailers.Length; i++) | ||
{ | ||
if (headerTrailers[i].Header is int[] header) | ||
{ | ||
if (header.Length > bytes.Length) | ||
goto next; | ||
for (int x = 0; x < header.Length; x++) | ||
{ | ||
if (bytes[x] != header[x] && header[x] != -1) | ||
goto next; | ||
} | ||
} | ||
if (headerTrailers[i].Trailer is int[] trailer) | ||
{ | ||
if (trailer.Length > bytes.Length) | ||
goto next; | ||
for (int x = 0; x < trailer.Length; x++) | ||
{ | ||
if (bytes[^(trailer.Length - x)] != trailer[x] && trailer[x] != -1) | ||
goto next; | ||
} | ||
} | ||
|
||
if (filename is not null) | ||
{ | ||
if (!headerTrailers[i].Extensions.Any(ext => filename.Length > ext.Length && filename.EndsWith(ext, StringComparison.OrdinalIgnoreCase))) | ||
return false; | ||
} | ||
return true; | ||
next: | ||
; | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can lower this limit here as 100KB should already be more than enough. (Bootstrap as a whole is > 300KB)