mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 16:00:25 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			81 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
 | 
						|
 | 
						|
package injectionutils_test
 | 
						|
 | 
						|
import (
 | 
						|
	"github.com/TeaOSLab/EdgeNode/internal/waf/injectionutils"
 | 
						|
	"github.com/TeaOSLab/EdgeNode/internal/waf/utils"
 | 
						|
	"github.com/iwind/TeaGo/assert"
 | 
						|
	"runtime"
 | 
						|
	"testing"
 | 
						|
)
 | 
						|
 | 
						|
func TestDetectXSS(t *testing.T) {
 | 
						|
	var a = assert.NewAssertion(t)
 | 
						|
	a.IsFalse(injectionutils.DetectXSS(""))
 | 
						|
	a.IsFalse(injectionutils.DetectXSS("abc"))
 | 
						|
	a.IsTrue(injectionutils.DetectXSS("<script>"))
 | 
						|
	a.IsTrue(injectionutils.DetectXSS("<link>"))
 | 
						|
	a.IsFalse(injectionutils.DetectXSS("<html><span>"))
 | 
						|
	a.IsFalse(injectionutils.DetectXSS("<script>"))
 | 
						|
	a.IsTrue(injectionutils.DetectXSS("/path?onmousedown=a"))
 | 
						|
	a.IsTrue(injectionutils.DetectXSS("/path?onkeyup=a"))
 | 
						|
	a.IsTrue(injectionutils.DetectXSS("onkeyup=a"))
 | 
						|
	a.IsTrue(injectionutils.DetectXSS("<iframe scrolling='no'>"))
 | 
						|
	a.IsFalse(injectionutils.DetectXSS("<html><body><span>RequestId: 1234567890</span></body></html>"))
 | 
						|
	a.IsTrue(injectionutils.DetectXSS("name=s&description=%3Cscript+src%3D%22a.js%22%3Edddd%3C%2Fscript%3E"))
 | 
						|
	a.IsFalse(injectionutils.DetectXSS(`<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 6.0.0">
 | 
						|
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
 | 
						|
      <rdf:Description rdf:about=""
 | 
						|
            xmlns:tiff="http://ns.adobe.com/tiff/1.0/">
 | 
						|
         <tiff:Orientation>1</tiff:Orientation>
 | 
						|
      </rdf:Description>
 | 
						|
   </rdf:RDF>
 | 
						|
</x:xmpmeta>`)) // included in some photo files
 | 
						|
}
 | 
						|
 | 
						|
func BenchmarkDetectXSS_MISS(b *testing.B) {
 | 
						|
	var result = injectionutils.DetectXSS("<html><body><span>RequestId: 1234567890</span></body></html>")
 | 
						|
	if result {
 | 
						|
		b.Fatal("'result' should not be 'true'")
 | 
						|
	}
 | 
						|
 | 
						|
	runtime.GOMAXPROCS(4)
 | 
						|
 | 
						|
	b.RunParallel(func(pb *testing.PB) {
 | 
						|
		for pb.Next() {
 | 
						|
			_ = injectionutils.DetectXSS("<html><body><span>RequestId: 1234567890</span></body></html>")
 | 
						|
		}
 | 
						|
	})
 | 
						|
}
 | 
						|
 | 
						|
func BenchmarkDetectXSS_MISS_Cache(b *testing.B) {
 | 
						|
	var result = injectionutils.DetectXSS("<html><body><span>RequestId: 1234567890</span></body></html>")
 | 
						|
	if result {
 | 
						|
		b.Fatal("'result' should not be 'true'")
 | 
						|
	}
 | 
						|
 | 
						|
	runtime.GOMAXPROCS(4)
 | 
						|
 | 
						|
	b.RunParallel(func(pb *testing.PB) {
 | 
						|
		for pb.Next() {
 | 
						|
			_ = injectionutils.DetectXSSCache("<html><body><span>RequestId: 1234567890</span></body></html>", utils.CacheMiddleLife)
 | 
						|
		}
 | 
						|
	})
 | 
						|
}
 | 
						|
 | 
						|
func BenchmarkDetectXSS_HIT(b *testing.B) {
 | 
						|
	var result = injectionutils.DetectXSS("<html><body><span>RequestId: 1234567890</span><script src=\"\"></script></body></html>")
 | 
						|
	if !result {
 | 
						|
		b.Fatal("'result' should not be 'false'")
 | 
						|
	}
 | 
						|
 | 
						|
	runtime.GOMAXPROCS(4)
 | 
						|
 | 
						|
	b.RunParallel(func(pb *testing.PB) {
 | 
						|
		for pb.Next() {
 | 
						|
			_ = injectionutils.DetectXSS("<html><body><span>RequestId: 1234567890</span><script src=\"\"></script></body></html>")
 | 
						|
		}
 | 
						|
	})
 | 
						|
}
 |