From 9a5c1916e9d8410d70ffe41ea0bb90bae71a69fa Mon Sep 17 00:00:00 2001 From: Buck Evan Date: Wed, 22 Oct 2025 11:33:12 -0500 Subject: [PATCH] Fix #160: Anchor IsJSON regex to prevent false positives on CDATA MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The IsJSON() function was using an unanchored regex `\s*[{\[]` which would match a `[` character anywhere in the input. This caused XML documents containing CDATA sections like `` to be incorrectly detected as JSON. Changed the regex to `^\s*[{\[]` to anchor it to the start of the input string. Now only inputs that actually begin with `{` or `[` (after optional whitespace) will be detected as JSON. Added TestIsJSON with test cases including CDATA to verify the fix. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- cmd/root_test.go | 22 ++++++++++++++++++++++ internal/utils/utils.go | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index dccc2c7..1387568 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -211,3 +211,25 @@ Bye.`, }) } } + +func TestIsJSON(t *testing.T) { + tests := []struct { + input string + expected bool + }{ + {"{}", true}, + {"[]", true}, + {" {}", true}, + {"\t[", true}, + {"", false}, + {"", false}, // Bug #160: should not match [ in CDATA + {"plain text", false}, + } + + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + result := utils.IsJSON(tt.input) + assert.Equal(t, tt.expected, result) + }) + } +} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 2597b2a..7f74e06 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -519,7 +519,7 @@ func IsHTML(input string) bool { func IsJSON(input string) bool { input = strings.ToLower(input) - matched, _ := regexp.MatchString(`\s*[{\[]`, input) + matched, _ := regexp.MatchString(`^\s*[{\[]`, input) return matched }