Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat: add rule-based security findings to audit runs

Luigi Agosti009fe3ce7bcd
diff --git a/internal/audit/enrich.go b/internal/audit/enrich.go
index f843b011..d73f0636 100644
--- a/internal/audit/enrich.go
+++ b/internal/audit/enrich.go
@@ -16,11 +16,12 @@ type PageOptions struct {
A11y bool `json:"a11y"`
Timing bool `json:"timing"`
Elements bool `json:"elements"`
+ Security bool `json:"security"`
}
// DefaultPageOptions enables every collector.
func DefaultPageOptions() PageOptions {
- return PageOptions{Screenshot: true, Network: true, Console: true, A11y: true, Timing: true, Elements: true}
+ return PageOptions{Screenshot: true, Network: true, Console: true, A11y: true, Timing: true, Elements: true, Security: true}
}
// Collectors are the browser-backed data sources EnrichPage composes. Hooks
@@ -33,6 +34,7 @@ type Collectors struct {
Snapshot func() ([]observe.A11yNode, error)
PageFacts func() (PageFacts, error)
Timing func() (*observe.TimingMetrics, error)
+ Forms func() ([]FormFact, error)
}
// PageAudit is the audit result for one page. Collector failures are data,
@@ -48,6 +50,8 @@ type PageAudit struct {
Screenshot string `json:"screenshot,omitempty"`
// A11yFindings are the accessibility rule violations behind AccessibilityScore.
A11yFindings []A11yFinding `json:"a11yFindings,omitempty"`
+ // SecurityFindings are the page's rule-based security-surface findings.
+ SecurityFindings []SecurityFinding `json:"securityFindings,omitempty"`
BrowserPageData
}
@@ -125,6 +129,18 @@ func EnrichPage(url string, opts PageOptions, c Collectors) PageAudit {
}
}
+ if opts.Security {
+ var forms []FormFact
+ if c.Forms != nil {
+ var err error
+ if forms, err = c.Forms(); err != nil {
+ fail("security", err)
+ forms = nil
+ }
+ }
+ pa.SecurityFindings = EvaluateSecurity(url, pa.Title, pa.NetworkRequests, forms)
+ }
+
pa.Error = strings.Join(errs, "; ")
return pa
}
diff --git a/internal/audit/run.go b/internal/audit/run.go
index d5e6f9f4..57cb4926 100644
--- a/internal/audit/run.go
+++ b/internal/audit/run.go
@@ -168,17 +168,21 @@ func RunAudit(input AuditInput, seaportalPages []SeaportalPage, opts RunOptions,
}
report.Pages = results
report.SummaryScore = summaryScore(plans, results)
+ for _, pr := range results {
+ report.SecurityFindings = append(report.SecurityFindings, pr.SecurityFindings...)
+ }
return report, nil
}
// ToPageResult converts a single-page audit into the report page shape.
func (pa PageAudit) ToPageResult() PageResult {
return PageResult{
- URL: pa.URL,
- Title: pa.Title,
- Error: pa.Error,
- Screenshot: pa.Screenshot,
- Browser: pa.BrowserPageData,
+ URL: pa.URL,
+ Title: pa.Title,
+ Error: pa.Error,
+ Screenshot: pa.Screenshot,
+ SecurityFindings: pa.SecurityFindings,
+ Browser: pa.BrowserPageData,
}
}
diff --git a/internal/audit/security.go b/internal/audit/security.go
new file mode 100644
index 00000000..416cc23c
--- /dev/null
+++ b/internal/audit/security.go
@@ -0,0 +1,133 @@
+// Security rules (no external scanner; all computed from data the audit
+// already collects, deterministically and offline):
+//
+// - mixed-content (high): an https page loading http:// subresources,
+// from the observed network requests.
+// - insecure-form-action (medium): an https page with a form whose action
+// posts to plain http://.
+// - insecure-password-form (high): a form containing a password input
+// whose action is plain http://, regardless of the page's own scheme.
+// - exposed-endpoint (medium): a successful (non-4xx/5xx) request to a
+// sensitive path (.env, .git, .htaccess, .htpasswd, wp-config.php,
+// id_rsa) observed in the network log.
+// - directory-listing (low): a page title that is a server directory
+// index ("Index of /...").
+package audit
+
+import (
+ "fmt"
+ "net/url"
+ "strings"
+)
+
+// FormFact is the DOM shape of one form, gathered by FormFactsScript.
+type FormFact struct {
+ // Action is the form's resolved action URL.
+ Action string `json:"action"`
+ // Method is the lowercased form method.
+ Method string `json:"method"`
+ // HasPassword reports whether the form contains a password input.
+ HasPassword bool `json:"hasPassword"`
+}
+
+// FormFactsScript evaluates to the []FormFact JSON shape in the page.
+const FormFactsScript = `(() => Array.from(document.forms).map((f) => ({
+ action: f.action || '',
+ method: (f.method || 'get').toLowerCase(),
+ hasPassword: !!f.querySelector('input[type="password"]'),
+})))()`
+
+// exposedPathMarkers are sensitive path fragments that should never answer
+// successfully on a production site.
+var exposedPathMarkers = []string{".env", ".git", ".htaccess", ".htpasswd", "wp-config.php", "id_rsa"}
+
+func isHTTPS(raw string) bool {
+ return strings.HasPrefix(strings.ToLower(raw), "https://")
+}
+
+func isHTTP(raw string) bool {
+ return strings.HasPrefix(strings.ToLower(raw), "http://")
+}
+
+// EvaluateSecurity derives the rule-based security findings for one page
+// from its URL, title, observed network requests, and form facts. Output
+// order is deterministic: rules in documentation order, items in input
+// order.
+func EvaluateSecurity(pageURL, title string, requests []NetworkRequest, forms []FormFact) []SecurityFinding {
+ var findings []SecurityFinding
+ pageSecure := isHTTPS(pageURL)
+
+ if pageSecure {
+ for _, req := range requests {
+ if req.URL == pageURL || !isHTTP(req.URL) {
+ continue
+ }
+ findings = append(findings, SecurityFinding{
+ RuleID: "mixed-content",
+ Severity: "high",
+ Detail: fmt.Sprintf("https page loads insecure subresource %s", req.URL),
+ URL: pageURL,
+ })
+ }
+ }
+
+ for _, form := range forms {
+ if pageSecure && isHTTP(form.Action) && !form.HasPassword {
+ findings = append(findings, SecurityFinding{
+ RuleID: "insecure-form-action",
+ Severity: "medium",
+ Detail: fmt.Sprintf("form on https page submits to insecure %s", form.Action),
+ URL: pageURL,
+ })
+ }
+ if form.HasPassword && isHTTP(form.Action) {
+ findings = append(findings, SecurityFinding{
+ RuleID: "insecure-password-form",
+ Severity: "high",
+ Detail: fmt.Sprintf("password form submits over plain http to %s", form.Action),
+ URL: pageURL,
+ })
+ }
+ }
+
+ for _, req := range requests {
+ if req.Status >= 400 || req.Status == 0 {
+ continue
+ }
+ if marker := exposedPathMarker(req.URL); marker != "" {
+ findings = append(findings, SecurityFinding{
+ RuleID: "exposed-endpoint",
+ Severity: "medium",
+ Detail: fmt.Sprintf("sensitive path %q answered %d at %s", marker, req.Status, req.URL),
+ URL: pageURL,
+ })
+ }
+ }
+
+ if strings.HasPrefix(strings.TrimSpace(title), "Index of /") {
+ findings = append(findings, SecurityFinding{
+ RuleID: "directory-listing",
+ Severity: "low",
+ Detail: fmt.Sprintf("server directory listing exposed (%q)", strings.TrimSpace(title)),
+ URL: pageURL,
+ })
+ }
+
+ return findings
+}
+
+// exposedPathMarker returns the matched sensitive fragment in the URL path,
+// or "" when none matches. Only the path is inspected, not the query.
+func exposedPathMarker(raw string) string {
+ u, err := url.Parse(raw)
+ if err != nil {
+ return ""
+ }
+ path := strings.ToLower(u.Path)
+ for _, marker := range exposedPathMarkers {
+ if strings.Contains(path, marker) {
+ return marker
+ }
+ }
+ return ""
+}
diff --git a/internal/audit/security_test.go b/internal/audit/security_test.go
new file mode 100644
index 00000000..05a0a91c
--- /dev/null
+++ b/internal/audit/security_test.go
@@ -0,0 +1,115 @@
+package audit
+
+import (
+ "testing"
+)
+
+func rulesOf(findings []SecurityFinding) []string {
+ var rules []string
+ for _, f := range findings {
+ rules = append(rules, f.RuleID)
+ }
+ return rules
+}
+
+func hasRule(findings []SecurityFinding, rule string) bool {
+ for _, f := range findings {
+ if f.RuleID == rule {
+ return true
+ }
+ }
+ return false
+}
+
+func TestMixedContentRule(t *testing.T) {
+ requests := []NetworkRequest{
+ {URL: "https://site.example/page", Status: 200},
+ {URL: "http://site.example/legacy.js", Status: 200},
+ {URL: "https://cdn.example/app.css", Status: 200},
+ }
+ findings := EvaluateSecurity("https://site.example/page", "Page", requests, nil)
+ if !hasRule(findings, "mixed-content") {
+ t.Fatalf("findings = %v, want mixed-content", rulesOf(findings))
+ }
+ for _, f := range findings {
+ if f.RuleID == "mixed-content" {
+ if f.Severity != "high" || f.URL != "https://site.example/page" {
+ t.Errorf("mixed-content finding = %+v", f)
+ }
+ }
+ }
+
+ httpPage := EvaluateSecurity("http://site.example/page", "Page", requests, nil)
+ if hasRule(httpPage, "mixed-content") {
+ t.Error("mixed-content must not fire for http pages")
+ }
+}
+
+func TestInsecureFormRules(t *testing.T) {
+ forms := []FormFact{
+ {Action: "http://site.example/login", Method: "post", HasPassword: true},
+ {Action: "http://site.example/search", Method: "get", HasPassword: false},
+ {Action: "https://site.example/safe", Method: "post", HasPassword: true},
+ }
+
+ fromHTTPS := EvaluateSecurity("https://site.example/", "Page", nil, forms)
+ if !hasRule(fromHTTPS, "insecure-password-form") || !hasRule(fromHTTPS, "insecure-form-action") {
+ t.Errorf("https page findings = %v", rulesOf(fromHTTPS))
+ }
+
+ fromHTTP := EvaluateSecurity("http://site.example/", "Page", nil, forms)
+ if !hasRule(fromHTTP, "insecure-password-form") {
+ t.Errorf("password-over-http must fire regardless of page scheme, got %v", rulesOf(fromHTTP))
+ }
+ if hasRule(fromHTTP, "insecure-form-action") {
+ t.Error("insecure-form-action only applies to https pages")
+ }
+
+ clean := EvaluateSecurity("https://site.example/", "Page", nil, []FormFact{
+ {Action: "https://site.example/login", Method: "post", HasPassword: true},
+ })
+ if len(clean) != 0 {
+ t.Errorf("https form should yield no findings, got %v", rulesOf(clean))
+ }
+}
+
+func TestExposedEndpointRule(t *testing.T) {
+ requests := []NetworkRequest{
+ {URL: "https://site.example/.env", Status: 200},
+ {URL: "https://site.example/.git/config", Status: 301},
+ {URL: "https://site.example/missing/.env", Status: 404},
+ {URL: "https://site.example/page?file=.env", Status: 200},
+ {URL: "https://site.example/app.js", Status: 200},
+ }
+ findings := EvaluateSecurity("https://site.example/", "Page", requests, nil)
+ count := 0
+ for _, f := range findings {
+ if f.RuleID == "exposed-endpoint" {
+ count++
+ if f.Severity != "medium" {
+ t.Errorf("exposed-endpoint severity = %q", f.Severity)
+ }
+ }
+ }
+ if count != 2 {
+ t.Errorf("exposed-endpoint count = %d, want 2 (.env 200 and .git 301; 404 and query-only excluded)", count)
+ }
+}
+
+func TestDirectoryListingRule(t *testing.T) {
+ findings := EvaluateSecurity("https://site.example/files/", "Index of /files", nil, nil)
+ if !hasRule(findings, "directory-listing") {
+ t.Errorf("findings = %v, want directory-listing", rulesOf(findings))
+ }
+ if f := EvaluateSecurity("https://site.example/", "Indexing 101", nil, nil); len(f) != 0 {
+ t.Errorf("title mentioning indexing should not fire, got %v", rulesOf(f))
+ }
+}
+
+func TestCleanPageYieldsNoFindings(t *testing.T) {
+ requests := []NetworkRequest{{URL: "https://site.example/app.css", Status: 200}}
+ forms := []FormFact{{Action: "https://site.example/search", Method: "get"}}
+ if f := EvaluateSecurity("https://site.example/", "Clean Page", requests, forms); len(f) != 0 {
+ t.Errorf("clean page findings = %v, want none", rulesOf(f))
+ }
+}
diff --git a/internal/audit/types.go b/internal/audit/types.go
index 25115bd0..061c9093 100644
--- a/internal/audit/types.go
+++ b/internal/audit/types.go
@@ -170,6 +170,8 @@ type PageResult struct {
// Seaportal holds SeaPortal per-page summary fields passed through
// verbatim when the input was a SeaPortal results file.
Seaportal map[string]any `json:"seaportal,omitempty"`
+ // SecurityFindings are the page-level security-surface findings.
+ SecurityFindings []SecurityFinding `json:"securityFindings,omitempty"`
// Browser is the browser-enriched data for the page.
Browser BrowserPageData `json:"browser"`
}
diff --git a/internal/handlers/audit_page.go b/internal/handlers/audit_page.go
index 085886ae..e7f23a5f 100644
--- a/internal/handlers/audit_page.go
+++ b/internal/handlers/audit_page.go
@@ -27,6 +27,7 @@ type auditPageOptionsBody struct {
A11y *bool `json:"a11y"`
Timing *bool `json:"timing"`
Elements *bool `json:"elements"`
+ Security *bool `json:"security"`
}
func (o *auditPageOptionsBody) pageOptions() audit.PageOptions {
@@ -45,6 +46,7 @@ func (o *auditPageOptionsBody) pageOptions() audit.PageOptions {
apply(&opts.A11y, o.A11y)
apply(&opts.Timing, o.Timing)
apply(&opts.Elements, o.Elements)
+ apply(&opts.Security, o.Security)
return opts
}
@@ -211,5 +213,10 @@ func (h *Handlers) auditCollectors(tCtx context.Context, tabID string) audit.Col
return h.Bridge.Evaluate(tCtx, expression, result, bridge.EvalOpts{AwaitPromise: true})
})
},
+ Forms: func() ([]audit.FormFact, error) {
+ var forms []audit.FormFact
+ err := h.Bridge.Evaluate(tCtx, audit.FormFactsScript, &forms, bridge.EvalOpts{})
+ return forms, err
+ },
}
}
diff --git a/tests/e2e/scenarios/api/audit-security-extended.sh b/tests/e2e/scenarios/api/audit-security-extended.sh
new file mode 100755
index 00000000..970aada8
--- /dev/null
+++ b/tests/e2e/scenarios/api/audit-security-extended.sh
@@ -0,0 +1,54 @@
+#!/bin/bash
+# audit-security-extended.sh — rule-based security findings in audit runs.
+
+GROUP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+source "${GROUP_DIR}/../../helpers/api.sh"
+
+# ─────────────────────────────────────────────────────────────────
+start_test "forms.html yields a password-form-over-http finding"
+
+pt_post /audit -d "{\"urls\":[\"${FIXTURES_URL}/audit-site/forms.html\"],\"options\":{\"screenshot\":false}}"
+assert_ok "audit forms.html"
+
+FINDING=$(echo "$RESULT" | jq '.pages[0].securityFindings[]? | select(.ruleId == "insecure-password-form")')
+if [ -n "$FINDING" ]; then
+ pass_assert "page carries insecure-password-form finding"
+else
+ fail_assert "page carries insecure-password-form finding (got: $(echo "$RESULT" | jq -c '.pages[0].securityFindings'))"
+fi
+
+if echo "$FINDING" | jq -e '.severity == "high"' >/dev/null 2>&1; then
+ pass_assert "finding has severity high"
+else
+ fail_assert "finding has severity high (got: $(echo "$FINDING" | jq -c '.severity'))"
+fi
+
+if echo "$RESULT" | jq -e '.securityFindings[]? | select(.ruleId == "insecure-password-form")' >/dev/null 2>&1; then
+ pass_assert "finding aggregated into the report security section"
+else
+ fail_assert "finding aggregated into the report security section"
+fi
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "clean.html yields zero security findings"
+
+pt_post /audit -d "{\"urls\":[\"${FIXTURES_URL}/audit-site/clean.html\"],\"options\":{\"screenshot\":false}}"
+assert_ok "audit clean.html"
+
+COUNT=$(echo "$RESULT" | jq '[.pages[0].securityFindings[]?] | length')
+if [ "$COUNT" -eq 0 ]; then
+ pass_assert "zero page security findings"
+else
+ fail_assert "zero page security findings (got: $(echo "$RESULT" | jq -c '.pages[0].securityFindings'))"
+fi
+
+SITE_COUNT=$(echo "$RESULT" | jq '[.securityFindings[]?] | length')
+if [ "$SITE_COUNT" -eq 0 ]; then
+ pass_assert "zero report-level security findings"
+else
+ fail_assert "zero report-level security findings (got: $SITE_COUNT)"
+fi
+
+end_test