From 5f79022aebdddd1581ee197ec65b1b0f34d8e603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Sun, 1 Nov 2026 08:13:35 +0000 Subject: [PATCH 1/3] feat: support reading HTML from stdin with "-" argument (#3) Allow piping HTML directly into dit with an explicit "-" argument: curl https://example.com | dit run - When the argument is "-", input is read from stdin instead of fetching a URL or reading a file, matching the existing implicit stdin behavior when no argument is given. Running "dit run -" on a terminal with no piped input returns an error instead of blocking. Also extracts readInput from readFromStdin so stdin handling is unit testable, and adds unit and functional tests plus README/help updates. --- README.md | 3 +++ dit_test.go | 32 +++++++++++++++++++++++++++ internal/cli/run.go | 19 +++++++++++++--- internal/cli/run_test.go | 48 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 internal/cli/run_test.go diff --git a/README.md b/README.md index 65d8a0b..f56f211 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,9 @@ dit run https://github.com/login # Classify forms in a local file dit run login.html +# Read HTML from stdin ("-" reads stdin explicitly) +curl -s https://github.com/login | dit run - + # With probabilities dit run https://github.com/login --proba diff --git a/dit_test.go b/dit_test.go index dbb0169..391ec0d 100644 --- a/dit_test.go +++ b/dit_test.go @@ -64,6 +64,38 @@ func TestFunctional_RunStdin(t *testing.T) { } } +func TestFunctional_RunStdinDash(t *testing.T) { + binary := buildBinary(t) + + // Simulating: curl https://example.com | dit run - + cmd := exec.Command(binary, "run", "-s", "-") + cmd.Stdin = strings.NewReader(loginFormHTML) + + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + if err := cmd.Run(); err != nil { + t.Fatalf("Functional dash test failed: %v\nStderr: %s", err, stderr.String()) + } + + // Output can be either page+forms object or just forms array + var pageResult struct { + Type string `json:"type"` + Forms []struct { + Type string `json:"type"` + Fields map[string]string `json:"fields"` + } `json:"forms"` + } + if err := json.Unmarshal(stdout.Bytes(), &pageResult); err != nil { + t.Fatalf("Output is not valid JSON: %v\nOutput: %s", err, stdout.String()) + } + + if len(pageResult.Forms) == 0 { + t.Fatal("No forms found in output") + } +} + func TestFunctional_RunStdinURL(t *testing.T) { binary := buildBinary(t) diff --git a/internal/cli/run.go b/internal/cli/run.go index a2590c4..8513d72 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -24,7 +24,7 @@ func (c *CLI) newRunCommand() *cobra.Command { var renderTimeout int cmd := &cobra.Command{ - Use: "run [url-or-file]", + Use: "run [url-or-file|-]", Short: "Classify page type and forms in a URL, HTML file, or stdin", Args: cobra.MaximumNArgs(1), Example: ` # Classify a URL directly @@ -36,6 +36,9 @@ func (c *CLI) newRunCommand() *cobra.Command { # Pipe HTML content from a file cat login.html | dit run + # Read HTML from stdin explicitly with "-" + curl -s https://github.com/login | dit run - + # Pipe a URL from stdin echo "https://github.com/login" | dit run @@ -68,8 +71,11 @@ func (c *CLI) newRunCommand() *cobra.Command { timeout: time.Duration(renderTimeout) * time.Second, } - if len(args) == 0 { + if len(args) == 0 || args[0] == "-" { if isStdinTerminal() { + if len(args) == 1 { + return fmt.Errorf(`no input on stdin: pipe HTML or a URL when using "-"`) + } return cmd.Help() } htmlContent, target, err = readFromStdin(fetchOpts) @@ -237,7 +243,14 @@ func isURL(target string) bool { func readFromStdin(opts fetchOptions) (string, string, error) { slog.Debug("Reading from stdin") - body, err := io.ReadAll(os.Stdin) + return readInput(os.Stdin, opts) +} + +// readInput reads HTML content or a URL from r. It returns the HTML content +// and the target it came from: the URL itself when r holds a URL, "stdin" +// otherwise. +func readInput(r io.Reader, opts fetchOptions) (string, string, error) { + body, err := io.ReadAll(r) if err != nil { return "", "", fmt.Errorf("read stdin: %w", err) } diff --git a/internal/cli/run_test.go b/internal/cli/run_test.go new file mode 100644 index 0000000..e3e4029 --- /dev/null +++ b/internal/cli/run_test.go @@ -0,0 +1,48 @@ +package cli + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestReadInputHTML(t *testing.T) { + html := `
` + + content, target, err := readInput(strings.NewReader(html), fetchOptions{}) + if err != nil { + t.Fatalf("readInput: %v", err) + } + if content != html { + t.Errorf("expected HTML content to pass through, got %q", content) + } + if target != "stdin" { + t.Errorf("expected target %q, got %q", "stdin", target) + } +} + +func TestReadInputEmpty(t *testing.T) { + if _, _, err := readInput(strings.NewReader(" \n\t"), fetchOptions{}); err == nil { + t.Error("expected error for empty input") + } +} + +func TestReadInputURL(t *testing.T) { + html := `fetched` + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(html)) + })) + defer srv.Close() + + content, target, err := readInput(strings.NewReader(srv.URL+"\n"), fetchOptions{}) + if err != nil { + t.Fatalf("readInput: %v", err) + } + if content != html { + t.Errorf("expected fetched HTML, got %q", content) + } + if target != srv.URL { + t.Errorf("expected target %q, got %q", srv.URL, target) + } +} From b4fd0df71bd315c8727ab66bb044efa336fa2e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Sun, 1 Nov 2026 09:16:40 +0000 Subject: [PATCH 2/3] fix: bump toolchain to go1.26.10 for GO-2026-6809 Clears the repo-wide Govulncheck CI failure; identical to the bump in the sibling PRs, merges cleanly in any order. --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index f4f0bbf..6fa668b 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/happyhackingspace/dit go 1.26.0 -toolchain go1.26.9 +toolchain go1.26.10 require ( github.com/PuerkitoBio/goquery v1.13.0 From c78c7ea811f0b0491acf8153b4f0339fdf177a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Sun, 1 Nov 2026 10:08:04 +0000 Subject: [PATCH 3/3] fix: bump x/net to v0.62.0 (GO-2026-6866, GO-2026-6860) Same security bump as the dedicated fix PR; identical changes merge cleanly in any order. --- go.mod | 6 +++--- go.sum | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 6fa668b..f040291 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/chromedp/chromedp v0.16.0 github.com/creativeprojects/go-selfupdate v1.6.0 github.com/spf13/cobra v1.10.2 - golang.org/x/net v0.59.0 + golang.org/x/net v0.62.0 ) require ( @@ -34,9 +34,9 @@ require ( github.com/spf13/pflag v1.0.9 // indirect github.com/ulikunitz/xz v0.5.15 // indirect gitlab.com/gitlab-org/api/client-go v1.46.0 // indirect - golang.org/x/crypto v0.57.0 // indirect + golang.org/x/crypto v0.58.0 // indirect golang.org/x/oauth2 v0.36.0 // indirect - golang.org/x/sys v0.48.0 // indirect + golang.org/x/sys v0.49.0 // indirect golang.org/x/time v0.15.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 321555e..1b6a7f9 100644 --- a/go.sum +++ b/go.sum @@ -77,23 +77,23 @@ go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= -golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= -golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= +golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues= -golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY= +golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI= golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs= golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= -golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE= -golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc= +golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM= +golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=