From 514020063d65ab6b352b17fa00aa4856c51f9e07 Mon Sep 17 00:00:00 2001 From: govardhan kumar Date: Fri, 24 Apr 2026 11:18:56 +0530 Subject: [PATCH 1/2] Fix slice out of bound execption and Fix logic to handle scenerio like: Date: Fri, 24 Apr 2026 14:36:00 +0530 Subject: [PATCH 2/2] Added test methods --- .../standard/security/SecurityHelper_test.go | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/rules/standard/security/SecurityHelper_test.go b/rules/standard/security/SecurityHelper_test.go index a112b5c..fde35ff 100644 --- a/rules/standard/security/SecurityHelper_test.go +++ b/rules/standard/security/SecurityHelper_test.go @@ -84,3 +84,105 @@ func TestFindVulnerableLinesWithExtraVulnerableTags(t *testing.T) { t.Errorf("%s Actual: %+v, Expected: %+v", "Occurrences list should be equal!", actualResult, expectedResult) } } + +// TestFindVulnerableLinesOpenAndCloseTagOnSameLine verifies that the column range +// is correctly computed relative to the open tag end when both +// appear on the same line. +func TestFindVulnerableLinesOpenAndCloseTagOnSameLine(t *testing.T) { + // Given + mockLines := []files.Line{ + {LineNumber: 1, Text: "", IsCommentedLine: false}, + } + mockFile := files.File{ + Lines: mockLines, + FileName: "test.page", + IgnoresSelected: []files.IgnoreSelected{}, + } + // openScriptStyleTagRegexp matches "'), so openTagEnd = 7. + // "" starts at absolute index 48, closeTagMatch[0] = 48-7 = 41. + // ColumnRange = [7, 7+41] = [7, 48]. + expectedResult := []rules.Occurrence{ + { + LineContent: "", + LineNumber: 1, + ColumnRange: []int{7, 48}, + IsFalsePositive: false, + }, + } + + // When + actualResult := findVulnerableLinesBetweenTags(mockFile, "XSSCurrentPageParameters", []string{}, false) + + // Then + if !reflect.DeepEqual(actualResult, expectedResult) { + t.Errorf("%s Actual: %+v, Expected: %+v", "Occurrences list should be equal!", actualResult, expectedResult) + } +} + +// TestFindVulnerableLinesCloseTagFollowedByOpenTagKeepsState verifies that when a +// closing tag is immediately followed by a new opening tag on the same line, +// hasOpeningScriptOrStyleTagFound remains true and subsequent lines are still scanned. +func TestFindVulnerableLinesCloseTagFollowedByOpenTagKeepsState(t *testing.T) { + // Given - line 2 closes and re-opens a script block; line 3 should still be scanned + mockLines := []files.Line{ + {LineNumber: 1, Text: "", IsCommentedLine: false}, + } + mockFile := files.File{ + Lines: mockLines, + FileName: "test.page", + IgnoresSelected: []files.IgnoreSelected{}, + } + expectedResult := []rules.Occurrence{ + {LineContent: "", LineNumber: 4, IsFalsePositive: false}, + } + + // When + actualResult := findVulnerableLinesBetweenTags(mockFile, "XSSCurrentPageParameters", []string{}, false) + + // Then + if !reflect.DeepEqual(actualResult, expectedResult) { + t.Errorf("%s Actual: %+v, Expected: %+v", "Occurrences list should be equal!", actualResult, expectedResult) + } +} + +// TestFindVulnerableLinesColumnRangeLessThanGuard verifies the columnRange[0] < columnRange[1] +// guard introduced in the last commit. When closeTagMatch is nil (close tag not found after +// openTagEnd), columnRange stays [0,0] and the < condition correctly prevents appending a +// spurious ColumnRange. The line is still reported (as part of the opening-tag line) but +// without a ColumnRange field. +func TestFindVulnerableLinesColumnRangeLessThanGuard(t *testing.T) { + // Given - a self-closing-like line where openScriptStyleTagRegexp matches but + // closeScriptStyleTagRegexp also matches, yet the close tag appears BEFORE openTagEnd + // when searched from line.Text[openTagEnd:] → closeTagMatch is nil → columnRange = [0,0]. + // Construct: "", IsCommentedLine: false}, + } + mockFile := files.File{ + Lines: mockLines, + FileName: "test.page", + IgnoresSelected: []files.IgnoreSelected{}, + } + + // When + actualResult := findVulnerableLinesBetweenTags(mockFile, "XSSCurrentPageParameters", []string{}, false) + + // Then - ColumnRange [7, 14] is valid (7 < 14), so exactly one occurrence with a ColumnRange. + if len(actualResult) != 1 { + t.Fatalf("Expected 1 occurrence, got %d: %+v", len(actualResult), actualResult) + } + if len(actualResult[0].ColumnRange) != 2 { + t.Errorf("Expected a ColumnRange on the occurrence, got: %+v", actualResult[0]) + } + if actualResult[0].ColumnRange[0] >= actualResult[0].ColumnRange[1] { + t.Errorf("columnRange[0] must be < columnRange[1], got: %+v", actualResult[0].ColumnRange) + } +}