Skip to content

Commit 4634a57

Browse files
committed
Add depth limit to recursive flattening for safety
- Added maxDepth parameter (default: 100) to prevent stack overflow - Returns string representation if depth limit reached - Maintains full functionality for normal use cases
1 parent f1e2c41 commit 4634a57

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/plugin/htmlReporter.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,12 +1542,16 @@ module.exports = function (config) {
15421542
if (typeof text !== 'string') {
15431543
// Handle arrays by recursively flattening and joining with commas
15441544
if (Array.isArray(text)) {
1545-
// Recursive helper to flatten deeply nested arrays
1546-
const flattenArray = arr => {
1545+
// Recursive helper to flatten deeply nested arrays with depth limit to prevent stack overflow
1546+
const flattenArray = (arr, depth = 0, maxDepth = 100) => {
1547+
if (depth >= maxDepth) {
1548+
// Safety limit reached, return string representation
1549+
return String(arr)
1550+
}
15471551
return arr
15481552
.map(item => {
15491553
if (Array.isArray(item)) {
1550-
return flattenArray(item)
1554+
return flattenArray(item, depth + 1, maxDepth)
15511555
}
15521556
return String(item)
15531557
})

test/unit/plugin/htmlReporter_test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ function escapeHtml(text) {
77
if (typeof text !== 'string') {
88
// Handle arrays by recursively flattening and joining with commas
99
if (Array.isArray(text)) {
10-
// Recursive helper to flatten deeply nested arrays
11-
const flattenArray = arr => {
10+
// Recursive helper to flatten deeply nested arrays with depth limit to prevent stack overflow
11+
const flattenArray = (arr, depth = 0, maxDepth = 100) => {
12+
if (depth >= maxDepth) {
13+
// Safety limit reached, return string representation
14+
return String(arr)
15+
}
1216
return arr
1317
.map(item => {
1418
if (Array.isArray(item)) {
15-
return flattenArray(item)
19+
return flattenArray(item, depth + 1, maxDepth)
1620
}
1721
return String(item)
1822
})

0 commit comments

Comments
 (0)