-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.go
More file actions
187 lines (163 loc) · 4.66 KB
/
scanner.go
File metadata and controls
187 lines (163 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package inspect
import (
"context"
"fmt"
"sort"
"sync"
"time"
"github.com/GrayCodeAI/inspect/internal/check"
"github.com/GrayCodeAI/inspect/internal/crawler"
)
// Scanner is a reusable site auditor. Create one with NewScanner and call
// Scan multiple times. It is safe for concurrent use.
type Scanner struct {
cfg *config
mu sync.Mutex
}
// NewScanner creates a configured Scanner. Apply presets and options:
//
// s := inspect.NewScanner(inspect.Standard, inspect.WithDepth(3))
func NewScanner(opts ...Option) *Scanner {
return &Scanner{cfg: buildConfig(opts)}
}
// Scan crawls the target URL and runs all configured checks against the
// discovered pages. Returns a complete Report with findings and stats.
func (s *Scanner) Scan(ctx context.Context, target string) (*Report, error) {
if s.cfg.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, s.cfg.timeout)
defer cancel()
}
start := time.Now()
crawlCfg := crawler.Config{
MaxDepth: s.cfg.depth,
Concurrency: s.cfg.concurrency,
Timeout: s.cfg.timeout,
PageTimeout: s.cfg.pageTimeout,
RateLimit: s.cfg.rateLimit,
UserAgent: s.cfg.userAgent,
FollowRedirects: s.cfg.followRedirects,
RespectRobots: s.cfg.respectRobots,
Exclude: s.cfg.exclude,
AuthHeader: s.cfg.authHeader,
AuthValue: s.cfg.authValue,
CookieJar: s.cfg.cookieJar,
AllowPrivateIPs: !s.cfg.blockPrivateIPs,
}
if s.cfg.logger != nil {
s.cfg.logger.Info("inspect: starting crawl", "target", target, "depth", s.cfg.depth)
}
c := crawler.New(crawlCfg)
pages, err := c.Crawl(ctx, target)
if err != nil {
return nil, fmt.Errorf("inspect: crawl failed: %w", err)
}
if s.cfg.logger != nil {
s.cfg.logger.Info("inspect: crawl complete", "pages", len(pages))
}
registry := check.DefaultRegistry()
// Apply accepted status codes to the links checker
if len(s.cfg.acceptedStatusCodes) > 0 {
registry.Register(&check.LinksCheck{AcceptedStatusCodes: s.cfg.acceptedStatusCodes})
}
for _, custom := range getCustomInternalChecks() {
registry.Register(custom)
}
enabledChecks := registry.Filter(s.cfg.checks)
var (
mu sync.Mutex
allFindings []Finding
durations = make(map[string]time.Duration)
)
const perCheckTimeout = 30 * time.Second
var wg sync.WaitGroup
for _, chk := range enabledChecks {
wg.Add(1)
go func(chk check.Checker) {
defer wg.Done()
checkStart := time.Now()
checkCtx, checkCancel := context.WithTimeout(ctx, perCheckTimeout)
defer checkCancel()
type checkResult struct {
findings []check.Finding
}
done := make(chan checkResult, 1)
go func() {
done <- checkResult{findings: chk.Run(checkCtx, pages)}
}()
var findings []check.Finding
var timedOut bool
select {
case res := <-done:
findings = res.findings
case <-checkCtx.Done():
timedOut = true
}
elapsed := time.Since(checkStart)
var converted []Finding
if timedOut {
converted = []Finding{{
Check: chk.Name(),
Severity: SeverityLow,
URL: target,
Message: fmt.Sprintf("check %q timed out after %s", chk.Name(), perCheckTimeout),
}}
} else {
converted = make([]Finding, len(findings))
for i, f := range findings {
converted[i] = Finding{
Check: chk.Name(),
Severity: Severity(f.Severity),
URL: f.URL,
Element: f.Element,
Message: f.Message,
Fix: f.Fix,
Evidence: f.Evidence,
}
}
}
mu.Lock()
allFindings = append(allFindings, converted...)
durations[chk.Name()] = elapsed
mu.Unlock()
}(chk)
}
wg.Wait()
sort.Slice(allFindings, func(i, j int) bool {
if allFindings[i].Severity != allFindings[j].Severity {
return allFindings[i].Severity > allFindings[j].Severity
}
return allFindings[i].URL < allFindings[j].URL
})
bySev := make(map[Severity]int)
byCheck := make(map[string]int)
for _, f := range allFindings {
bySev[f.Severity]++
byCheck[f.Check]++
}
report := &Report{
Target: target,
Findings: allFindings,
CrawledURLs: len(pages),
Duration: time.Since(start),
FailOn: s.cfg.failOn,
Stats: Stats{
PagesScanned: len(pages),
FindingsTotal: len(allFindings),
BySeverity: bySev,
ByCheck: byCheck,
DurationPerCheck: durations,
},
}
return report, nil
}
// ScanDir scans a local directory by starting a temporary file server.
// Useful for auditing build output before deployment.
func (s *Scanner) ScanDir(ctx context.Context, dir string) (*Report, error) {
srv, addr, err := crawler.ServeDir(ctx, dir)
if err != nil {
return nil, err
}
defer srv.Close()
return s.Scan(ctx, "http://"+addr)
}