Skip to content
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
27 changes: 21 additions & 6 deletions src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,23 @@ await writer.WriteAsync($"<{prefix}sheetData>"
var rowInfo = _xRowInfos[rowNo];
var row = rowInfo.Row;

if (row.InnerText.Contains("@group"))
string specialCellType = "";
foreach (XmlNode c in row.GetElementsByTagName("c"))
{
specialCellType = c.InnerText switch
{
"@group" => "group",
"@endgroup" => "endgroup",
"@merge" or "@endmerge" => "merge",
var s when s.StartsWith("@header") => "header",
_ => ""
};

if (!string.IsNullOrEmpty(specialCellType))
break;
}
Comment on lines +380 to +394
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

This code block is responsible for identifying special template commands. The current implementation, however, processes these commands on cell content after user-provided data has been injected. This introduces a high-severity Template Injection vulnerability, as an attacker could provide data matching command strings (e.g., @group) to manipulate the template's execution logic or cause a denial of service. It is critical to identify commands from the original, unmodified template structure before any data replacement occurs. Furthermore, for improved readability, consider replacing the C# 11 list pattern ['@','h','e','a','d','e','r', ..] with a more explicit when clause using StartsWith().

                specialCellType = c.InnerText switch
                {
                    "@group" => "group",
                    "@endgroup" => "endgroup",
                    "@merge" or "@endmerge" => "merge",
                    var s when s.StartsWith("@header") => "header",
                    _ => ""
                };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's incorrect, data replacement already occurs after commands identification


if (specialCellType == "group")
{
groupingStarted = true;
hasEverGroupStarted = true;
Expand All @@ -386,7 +402,7 @@ await writer.WriteAsync($"<{prefix}sheetData>"
prevHeader = "";
continue;
}
else if (row.InnerText.Contains("@endgroup"))
else if (specialCellType == "endgroup")
{
if (cellIEnumerableValuesIndex >= cellIEnumerableValues.Count - 1)
{
Expand All @@ -403,13 +419,13 @@ await writer.WriteAsync($"<{prefix}sheetData>"
isFirstRound = false;
continue;
}
else if (row.InnerText.Contains("@header"))
else if (specialCellType == "header")
{
isHeaderRow = true;
}
else if (mergeCells)
{
if (row.InnerText.Contains("@merge") || row.InnerText.Contains("@endmerge"))
if (specialCellType == "merge")
{
mergeRowCount++;
continue;
Expand All @@ -418,8 +434,7 @@ await writer.WriteAsync($"<{prefix}sheetData>"

if (groupingStarted && !isCellIEnumerableValuesSet)
{
cellIEnumerableValues = rowInfo.CellIlListValues
?? rowInfo.CellIEnumerableValues.Cast<object>().ToList();
cellIEnumerableValues = rowInfo.CellIlListValues ?? rowInfo.CellIEnumerableValues.Cast<object>().ToList();
isCellIEnumerableValuesSet = true;
}

Expand Down
Loading