@@ -2096,6 +2096,84 @@ func Test_Ctx_AutoFormat_Struct(t *testing.T) {
20962096 )
20972097}
20982098
2099+ // go test -run Test_Ctx_AutoFormat_XSS_Prevention
2100+ func Test_Ctx_AutoFormat_XSS_Prevention (t * testing.T ) {
2101+ t .Parallel ()
2102+ app := New ()
2103+ c := app .AcquireCtx (& fasthttp.RequestCtx {})
2104+
2105+ // Test basic XSS with script tag
2106+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2107+ err := c .AutoFormat ("<script>alert('XSS')</script>" )
2108+ require .NoError (t , err )
2109+ require .Equal (t , MIMETextHTMLCharsetUTF8 , c .GetRespHeader (HeaderContentType ))
2110+ require .Equal (t , "<p><script>alert('XSS')</script></p>" , string (c .Response ().Body ()))
2111+ require .NotContains (t , string (c .Response ().Body ()), "<script>" , "Script tags should be escaped" )
2112+
2113+ // Test XSS with img onerror
2114+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2115+ err = c .AutoFormat ("<img src=x onerror=alert('XSS')>" )
2116+ require .NoError (t , err )
2117+ require .Equal (t , "<p><img src=x onerror=alert('XSS')></p>" , string (c .Response ().Body ()))
2118+ require .NotContains (t , string (c .Response ().Body ()), "<img" , "Img tags should be escaped" )
2119+
2120+ // Test XSS with iframe
2121+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2122+ err = c .AutoFormat ("<iframe src='javascript:alert(\" XSS\" )'></iframe>" )
2123+ require .NoError (t , err )
2124+ require .Equal (t , "<p><iframe src='javascript:alert("XSS")'></iframe></p>" , string (c .Response ().Body ()))
2125+ require .NotContains (t , string (c .Response ().Body ()), "<iframe" , "Iframe tags should be escaped" )
2126+
2127+ // Test XSS with event handler attributes
2128+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2129+ err = c .AutoFormat ("<div onload=alert('XSS')>test</div>" )
2130+ require .NoError (t , err )
2131+ require .Equal (t , "<p><div onload=alert('XSS')>test</div></p>" , string (c .Response ().Body ()))
2132+ require .NotContains (t , string (c .Response ().Body ()), "<div" , "Div tags should be escaped" )
2133+
2134+ // Test XSS with link javascript protocol
2135+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2136+ err = c .AutoFormat ("<a href='javascript:alert(\" XSS\" )'>Click me</a>" )
2137+ require .NoError (t , err )
2138+ require .Equal (t , "<p><a href='javascript:alert("XSS")'>Click me</a></p>" , string (c .Response ().Body ()))
2139+ require .NotContains (t , string (c .Response ().Body ()), "<a href" , "Anchor tags should be escaped" )
2140+
2141+ // Test XSS with mixed quotes
2142+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2143+ err = c .AutoFormat (`"><script>alert('XSS')</script><"` )
2144+ require .NoError (t , err )
2145+ require .Equal (t , `<p>"><script>alert('XSS')</script><"</p>` , string (c .Response ().Body ()))
2146+ require .NotContains (t , string (c .Response ().Body ()), "<script>" , "Script tags with quotes should be escaped" )
2147+
2148+ // Test legitimate HTML special characters are escaped
2149+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2150+ err = c .AutoFormat ("Price: 5 < 10 & 20 > 15" )
2151+ require .NoError (t , err )
2152+ require .Equal (t , "<p>Price: 5 < 10 & 20 > 15</p>" , string (c .Response ().Body ()))
2153+
2154+ // Test that normal text without special chars works fine
2155+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2156+ err = c .AutoFormat ("Hello, World!" )
2157+ require .NoError (t , err )
2158+ require .Equal (t , "<p>Hello, World!</p>" , string (c .Response ().Body ()))
2159+
2160+ // Test XSS prevention with byte slice
2161+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2162+ err = c .AutoFormat ([]byte ("<script>alert('XSS')</script>" ))
2163+ require .NoError (t , err )
2164+ require .Equal (t , "<p><script>alert('XSS')</script></p>" , string (c .Response ().Body ()))
2165+
2166+ // Test XSS prevention with complex struct formatting
2167+ c .Request ().Header .Set (HeaderAccept , MIMETextHTML )
2168+ err = c .AutoFormat (struct {
2169+ Value string
2170+ }{Value : "<script>alert('XSS')</script>" })
2171+ require .NoError (t , err )
2172+ // When formatted as string via fmt.Sprintf, the struct becomes something like {Value:<script>...}
2173+ require .NotContains (t , string (c .Response ().Body ()), "<script>" , "Script tags in struct should be escaped" )
2174+ require .Contains (t , string (c .Response ().Body ()), "<script>" , "Escaped script tags should be present" )
2175+ }
2176+
20992177// go test -v -run=^$ -bench=Benchmark_Ctx_AutoFormat -benchmem -count=4
21002178func Benchmark_Ctx_AutoFormat (b * testing.B ) {
21012179 app := New ()
0 commit comments