Skip to content

Commit 22c8fce

Browse files
TMHSDigitalclaude
andcommitted
Undo the CSV formula guard when a baseline is read back (#479)
The CSV writer prefixes ' to values starting with = + - @ tab or CR, but diff.Parse read them back as written, so such a TXT record was reported changed on every -diff-records run against a CSV baseline. Parse now strips that one quote. The writer also guards a value that already starts with ', so stripping exactly one quote is lossless for every value. A write-then-Parse round-trip test pins it. Fixes #479 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 61d7b67 commit 22c8fce

5 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- With `-dL` and `-rate`, the next target's `-axfr` zone transfer and `-nsec-walk` no longer run beside the current target's scan, where their queries came on top of `-rate`; only its passive sources are fetched ahead, and the transfer and walk run when that target starts. Without `-rate` nothing changes (#461).
2222
- Takeover candidates and internal addresses found by the `-permute` and `-ptr` passes now count in `takeover_candidates` and `internal_addresses` (in `-stats`, `subenum report` and the Action's `takeovers` and `internal` outputs), as they already did in the results; under `-permute-only` they were always 0. The Action's `found` output and the report's Found column also include `ptr_found` (#458).
2323
- A dead `-r` pool member is benched after 3 failures in a row instead of a window of 20 lookups, each repeat bench doubles (30 s up to 4 min) until it answers again, and a failed canary check counts as a failure, so dead entries in a resolver list stop costing a window of timeouts every 30 s (#463).
24+
- A CSV `-diff` baseline no longer reports a TXT value starting with `=`, `+`, `-`, `@`, a tab or a carriage return as changed on every `-diff-records` run: the formula guard's leading `'` is removed when the file is read back. A CSV value starting with `'` now gets the guard too, so every value reads back exactly (#479).
2425
- The five wildcard probes each scan pass sends before it starts now go out together, so a pass waits one round trip for them instead of five (#464).
2526
- `-hosts` with `-dL` no longer walks the whole host list once per target: the names are sorted by target once, so a list of 200,000 names over 2,000 targets is split in about 11 ms instead of about 9 s of CPU before and between targets (#465).
2627

‎docs/output.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ CSV has one row per record, so a name with three records has three rows, and a n
4545
|---|---|---|
4646
| `subdomain` | always | As in JSON. |
4747
| `type` | always | The record type. |
48-
| `value` | always | The record value. A value starting with `=`, `+`, `-`, `@`, a tab or a carriage return gets a leading `'`, so a spreadsheet does not read it as a formula. |
48+
| `value` | always | The record value. A value starting with `=`, `+`, `-`, `@`, a tab or a carriage return gets a leading `'`, so a spreadsheet does not read it as a formula; so does one starting with `'`, so that `-diff` and `subenum diff` read every value back exactly by removing that one quote. |
4949
| `simulated` | `-simulate` | `true`. |
5050
| `takeover_candidate` | `CNAME`, `NS`, `SRV` or `HTTPS` in `-type` | As in JSON; empty when there is no hint. |
5151
| `change` | `-diff` | As in JSON. |
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package diff
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/TMHSDigital/subenum/internal/dns"
8+
"github.com/TMHSDigital/subenum/internal/output"
9+
"github.com/TMHSDigital/subenum/internal/scan"
10+
)
11+
12+
// TestCSVBaselineRoundTrip covers #479: values the CSV writer guards against
13+
// formula injection read back as they were, so an unchanged TXT record is
14+
// not reported changed against a CSV baseline, including one that starts
15+
// with a quote itself.
16+
func TestCSVBaselineRoundTrip(t *testing.T) {
17+
values := []string{"=abc", "+1", "-x", "@home", "\tlead", "\rcr", "'quoted", "''=", "'=x", "'", "plain", "v=spf1 -all"}
18+
var out bytes.Buffer
19+
w := output.New(nil, false, output.FormatCSV)
20+
w.SetStdout(&out)
21+
for i, v := range values {
22+
w.Emit(output.Result{Result: scan.Result{
23+
Subdomain: string(rune('a'+i)) + ".example.com",
24+
Records: []dns.Record{{Type: "TXT", Value: v}},
25+
}})
26+
}
27+
if err := w.Finish(); err != nil {
28+
t.Fatal(err)
29+
}
30+
b, err := Parse(out.Bytes())
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
for i, v := range values {
35+
name := string(rune('a'+i)) + ".example.com"
36+
if got := b.Records[name]; len(got) != 1 || got[0].Value != v {
37+
t.Errorf("%s: read back %q, wrote %q", name, got, v)
38+
}
39+
}
40+
}

‎internal/diff/parse.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ func Load(path string) (Baseline, error) {
5656
return Parse(data)
5757
}
5858

59+
// csvUnguard undoes the CSV writer's formula guard (output.csvSafe): one
60+
// leading single quote it added before a value starting with =, +, -, @, a
61+
// tab, a carriage return or a quote. Without it a CSV baseline's guarded TXT
62+
// value differs from the live one on every -diff-records run (#479).
63+
func csvUnguard(v string) string {
64+
if len(v) >= 2 && v[0] == '\'' && strings.ContainsRune("=+-@\t\r'", rune(v[1])) {
65+
return v[1:]
66+
}
67+
return v
68+
}
69+
5970
// Parse reads the names of a subenum results file in any output format:
6071
// JSONL (summary lines are skipped), a JSON array, CSV, or text (bare names,
6172
// "Found: name", -show-records lines, or -diff's +/-/~ lines). The CLI's
@@ -116,7 +127,7 @@ func Parse(data []byte) (Baseline, error) {
116127
for _, row := range rows[1:] {
117128
switch {
118129
case len(row) >= 3:
119-
add(row[0], dns.Record{Type: row[1], Value: row[2]})
130+
add(row[0], dns.Record{Type: row[1], Value: csvUnguard(row[2])})
120131
case len(row) > 0:
121132
add(row[0])
122133
}

‎internal/output/writer.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,11 @@ func (w *Writer) writeJSONL(res Result) {
373373

374374
// csvSafe keeps a record value from being read as a formula when the CSV
375375
// is opened in a spreadsheet: a value starting with =, +, -, @, a tab or
376-
// a carriage return gets a leading single quote.
376+
// a carriage return gets a leading single quote. So does one that starts
377+
// with a quote already, so reading the file back (diff.Parse) can strip
378+
// exactly one quote and get every value as it was (#479).
377379
func csvSafe(v string) string {
378-
if v != "" && strings.ContainsRune("=+-@\t\r", rune(v[0])) {
380+
if v != "" && strings.ContainsRune("=+-@\t\r'", rune(v[0])) {
379381
return "'" + v
380382
}
381383
return v

0 commit comments

Comments
 (0)