Skip to content

Commit

Permalink
Merge pull request #13 from KlassnayaAfrodita/patch-4
Browse files Browse the repository at this point in the history
Update XSS.go
  • Loading branch information
hokamsingh authored Oct 23, 2024
2 parents c08f775 + 8001d46 commit 07f1df0
Showing 1 changed file with 11 additions and 54 deletions.
65 changes: 11 additions & 54 deletions internal/core/middleware/XSS.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package middleware

import (
"html"
"net/http"
"regexp"
"strings"
)

type XSSProtection struct{}

// Creates a new middleware for XSS protection
func NewXSSProtection() *XSSProtection {
return &XSSProtection{}
}

// Regular expression to detect potentially harmful XSS patterns, including encoded variants
var unsafePattern = regexp.MustCompile(`(?i)<script.*?>|javascript:|data:text/html|onerror=|onload=|onclick=|<iframe>|<img src=|<object>|<embed>|eval\(|%3Cscript%3E|&#60;script&#62;|&#x3C;script&#x3E;|&lt;script&gt;`)

// Middleware to handle requests and check for XSS attacks
func (xss *XSSProtection) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if containsXSS(r) {
Expand All @@ -23,7 +28,7 @@ func (xss *XSSProtection) Handle(next http.Handler) http.Handler {
})
}

// containsXSS checks various parts of the request for XSS payloads.
// containsXSS checks various parts of the request for XSS payloads
func containsXSS(r *http.Request) bool {
// Check URL query parameters
for _, values := range r.URL.Query() {
Expand All @@ -45,14 +50,14 @@ func containsXSS(r *http.Request) bool {
}
}

// Check cookies
// Check cookie values
for _, cookie := range r.Cookies() {
if isXSS(cookie.Value) {
return true
}
}

// Check headers
// Check header values
for _, values := range r.Header {
for _, value := range values {
if isXSS(value) {
Expand All @@ -64,56 +69,8 @@ func containsXSS(r *http.Request) bool {
return false
}

// isXSS checks if a string contains potentially harmful XSS payloads.
// isXSS checks if a string contains potentially harmful XSS payloads using regular expressions
func isXSS(value string) bool {
// Define a more comprehensive list of unsafe patterns
unsafePatterns := []string{
"<script>",
"javascript:",
"data:text/html",
"vbscript:",
"mocha:",
"onerror=",
"onload=",
"onclick=",
"onmouseover=",
"onfocus=",
"onchange=",
"onsubmit=",
"onreset=",
"onabort=",
"<iframe>",
"<img src=",
"<object>",
"<embed>",
"<style>",
"<link>",
"<meta>",
"document.cookie",
"window.location",
"self.location",
"eval(",
"<!--",
"--!>",
"<![CDATA[",
"svg/onload=",
"math:xmlns",
"data:",
"ftp://",
"file://",
"%3Cscript%3E",
"&#60;script&#62;",
"&#x3C;script&#x3E;",
}

valueLower := strings.ToLower(value)
for _, pattern := range unsafePatterns {
if strings.Contains(valueLower, pattern) {
return true
}
}

// Use HTML escaping as an additional check
escaped := html.EscapeString(value)
return escaped != value
return unsafePattern.MatchString(valueLower)
}

0 comments on commit 07f1df0

Please sign in to comment.