Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion src/MiniExcel.Core/Helpers/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

internal static class ThrowHelper
{
private static readonly byte[] ZipArchiveHeader = [0x50, 0x4B];
private const int ExcelMaxSheetNameLength = 31;

internal static void ThrowIfInvalidOpenXml(Stream stream)
{
var probe = new byte[8];
Expand All @@ -13,7 +16,16 @@
stream.Seek(0, SeekOrigin.Begin);

// OpenXml format can be any ZIP archive
if (probe is not [0x50, 0x4B, ..])
if (!probe.StartsWith(ZipArchiveHeader))
throw new InvalidDataException("The file is not a valid OpenXml document.");
}

internal static void ThrowIfInvalidSheetName(string? sheetName)
{
if (string.IsNullOrEmpty(sheetName))
throw new ArgumentException("Sheet names cannot be empty or null");

if (sheetName.Length > ExcelMaxSheetNameLength)

Check warning on line 28 in src/MiniExcel.Core/Helpers/ThrowHelper.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 28 in src/MiniExcel.Core/Helpers/ThrowHelper.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.
throw new ArgumentException("Sheet names must be less than 31 characters");
}
}
2 changes: 2 additions & 0 deletions src/MiniExcel.Core/OpenXml/OpenXmlWriter.DefaultOpenXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal partial class OpenXmlWriter : IMiniExcelWriter
{
foreach (var sheet in dictionary)
{
ThrowHelper.ThrowIfInvalidSheetName(sheet.Key);

sheetId++;
var sheetInfos = GetSheetInfos(sheet.Key);
yield return Tuple.Create(sheetInfos.ToDto(sheetId), sheet.Value);
Expand Down
5 changes: 1 addition & 4 deletions src/MiniExcel.Core/OpenXml/OpenXmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ internal OpenXmlWriter(Stream stream, object? value, string? sheetName, IMiniExc
[CreateSyncVersion]
internal static Task<OpenXmlWriter> CreateAsync(Stream stream, object? value, string? sheetName, bool printHeader, IMiniExcelConfiguration? configuration, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(sheetName))
throw new ArgumentException("Sheet names cannot be empty or null", nameof(sheetName));
if (sheetName?.Length > 31)
throw new ArgumentException("Sheet names must be less than 31 characters", nameof(sheetName));
ThrowHelper.ThrowIfInvalidSheetName(sheetName);

var writer = new OpenXmlWriter(stream, value, sheetName, configuration, printHeader);
return Task.FromResult(writer);
Expand Down
20 changes: 20 additions & 0 deletions tests/MiniExcel.Core.Tests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3690,4 +3690,24 @@ public void TestIssue869(string fileName, DateOnlyConversionMode mode, bool thro
}
}
}

[Fact]
public void TestIssue876()
{
var someTable = new[]
{
new { Name = "Jack", Age = 25 },
};

var sheets = new Dictionary<string, object>
{
["SomeVeryLongNameWithMoreThan31Characters"] = someTable
};

Assert.Throws<ArgumentException>(() =>
{
using var outputPath = AutoDeletingPath.Create();
_excelExporter.Export(outputPath.ToString(), sheets);
});
}
}
Loading