Harden scan robustness and tests

This commit is contained in:
ZacharyZcR
2026-06-14 22:23:48 +08:00
parent 5ad914a1bb
commit c49c23c7f0
100 changed files with 4483 additions and 412 deletions
+75
View File
@@ -1,9 +1,11 @@
package WebScan
import (
"context"
"testing"
"github.com/shadow1ng/fscan/common"
"github.com/shadow1ng/fscan/common/config"
"github.com/shadow1ng/fscan/webscan/lib"
)
@@ -154,6 +156,41 @@ func TestBuildTargetURL(t *testing.T) {
expected: "http://[2001:db8::1]",
expectError: false,
},
{
name: "empty host is rejected",
hostInfo: &common.HostInfo{
Port: 80,
URL: "http://",
},
expectError: true,
},
{
name: "invalid port is rejected",
hostInfo: &common.HostInfo{
Host: "example.com",
Port: 80,
URL: "http://example.com:bad",
},
expectError: true,
},
{
name: "empty explicit port is rejected",
hostInfo: &common.HostInfo{
Host: "example.com",
Port: 80,
URL: "http://example.com:",
},
expectError: true,
},
{
name: "out of range port is rejected",
hostInfo: &common.HostInfo{
Host: "example.com",
Port: 80,
URL: "http://example.com:70000",
},
expectError: true,
},
}
for _, tt := range tests {
@@ -430,6 +467,44 @@ func TestFilterPocsNilSafety(t *testing.T) {
}
}
func TestCreateBaseRequestHeaders(t *testing.T) {
cfg := common.NewConfig()
cfg.HTTP.UserAgent = "fscan-test-agent"
cfg.HTTP.Accept = "application/json"
cfg.HTTP.Cookie = "sid=abc"
req, err := createBaseRequest(context.Background(), "http://example.com/path", cfg)
if err != nil {
t.Fatalf("createBaseRequest error = %v", err)
}
if req.Method != "GET" {
t.Fatalf("method = %q, want GET", req.Method)
}
if got := req.Header.Get("User-agent"); got != "fscan-test-agent" {
t.Fatalf("User-agent = %q", got)
}
if got := req.Header.Get("Accept"); got != "application/json" {
t.Fatalf("Accept = %q", got)
}
if got := req.Header.Get("Cookie"); got != "sid=abc" {
t.Fatalf("Cookie = %q", got)
}
if got := req.Header.Get("Accept-Language"); got == "" {
t.Fatal("Accept-Language should be set")
}
}
func TestExecutePOCsEarlyReturns(t *testing.T) {
cfg := common.NewConfig()
session := common.NewScanSession(cfg, common.NewState(), &common.FlagVars{})
previous := allPocs
allPocs = nil
t.Cleanup(func() { allPocs = previous })
executePOCs(context.Background(), config.PocInfo{}, cfg, session)
executePOCs(context.Background(), config.PocInfo{Target: "http://example.com", PocName: "missing"}, cfg, session)
}
func TestDirectoryExists(t *testing.T) {
tests := []struct {
name string