txeh-v1.8.12026-05-31
txeh v1.8.1
A maintenance release. The notable change is hardening how comments and hostnames are written to the hosts file.
Bug fixes
- Strip line-break characters from hostnames and comments (#90). Comments and hostnames passed to AddHost / AddHostWithComment were trimmed but kept embedded \r, \n, \v, \f, and NUL characters. Since the line formatter interpolated those fields directly, an embedded \r\n could split one logical entry across multiple physical lines and corrupt the file when read back (CWE-117). These characters are now stripped both at the input boundary and at render time. Tabs, spaces, and # are preserved, so normal comments are unaffected, and the public API is unchanged.
This addresses a reported "CRLF injection." For the record: exploiting it through the CLI requires root plus an attacker-controlled -w path, which already grants arbitrary file write without any comment trick, so the privilege-escalation framing does not hold. The real gap was data hygiene for library callers that may pass untrusted input, which is what this fixes.
Documentation
- Adopt the txn2 design system for the docs site (#77).
Maintenance
Routine CI and dependency bumps (codeql-action, gosec, goreleaser, cosign, sbom-action, golangci-lint, setup-go, codecov, rapid). See the full commit list below.
<details>
<summary>Full changelog</summary>
Others
* e6835b87e1466fd86e38a0ed0e2638c22d49c7f2 chore(ci): bump CI dependencies (#71)
* 4a44bdb112f4ad57d196e86f6080a3661f6dd76a chore(ci): bump actions/setup-go from 6.2.0 to 6.3.0 (#54)
* 5b2294eccb18709a25ae85e9a3d849424c17b043 chore(ci): bump actions/upload-pages-artifact from 4.0.0 to 5.0.0 (#73)
* 22bc0a5bf7644d5a18bd81de19681415e850aaad chore(ci): bump anchore/sbom-action from 0.22.2 to 0.23.0 (#57)
* 96f971c5cbabee7e470d53fd336704c88c991c4a chore(ci): bump anchore/sbom-action from 0.23.0 to 0.23.1 (#60)
* fd47a7ecea468b35b159d859ff1cce3400ccf5f7 chore(ci): bump anchore/sbom-action from 0.23.1 to 0.24.0 (#65)
* 5fedd09c28d32b47afa4140c5837e6063d7caeb4 chore(ci): bump codecov/codecov-action from 5.5.2 to 5.5.3 (#62)
* 6ee1551f2a14419fdb81fda288d85b16de860d06 chore(ci): bump codecov/codecov-action from 6.0.0 to 6.0.1 (#86)
* 44af6237a9fa80ea90ba074bcb160ea0876c39d5 chore(ci): bump github/codeql-action from 3.32.1 to 4.32.2 (#49)
* a74204e34bf05cc4e19149a1848dbc4a22fcfcaf chore(ci): bump github/codeql-action from 4.32.2 to 4.32.3 (#50)
* 35fb19049113c39162883684cdfeda21cdbdb842 chore(ci): bump github/codeql-action from 4.32.3 to 4.32.4 (#52)
* dad96ee41d3e89d3c470d085f671775afe99679f chore(ci): bump github/codeql-action from 4.32.4 to 4.32.5 (#55)
* e66cb5e1053e25333cb9579682a61139d1c71f70 chore(ci): bump github/codeql-action from 4.32.5 to 4.32.6 (#59)
* cdb139f7d386d14151252fc9f8a68bab5282d7ae chore(ci): bump github/codeql-action from 4.32.6 to 4.33.0 (#61)
* d94b163d8c9d5b340f7af28f720cc62ee7a44516 chore(ci): bump github/codeql-action from 4.33.0 to 4.34.1 (#63)
* 49645c9a52abfe674d677a2feaf6af62f427bf67 chore(ci): bump github/codeql-action from 4.35.1 to 4.35.2 (#74)
* 6d0d8ede565ce831efebfe82fdac2c7a3c189e76 chore(ci): bump github/codeql-action from 4.35.2 to 4.35.3 (#81)
* c15ac928b4fca8916d81871df3d180223b3322fd chore(ci): bump github/codeql-action from 4.35.3 to 4.35.4 (#82)
* e4fb26d01a15a2258408a375df1658ef8d5f8801 chore(ci): bump github/codeql-action from 4.35.4 to 4.35.5 (#84)
* 879894bc4a6d6f574bb2fb98df451e696fffde30 chore(ci): bump github/codeql-action from 4.35.5 to 4.36.0 (#88)
* d3243398ac65ad3d69d4a2e6194e4d0e6d1715d8 chore(ci): bump golangci/golangci-lint-action from 9.2.0 to 9.2.1 (#89)
* da578acee902a376051bf7b6bcfe89162f38de21 chore(ci): bump goreleaser/goreleaser-action from 6.4.0 to 7.0.0 (#53)
* 872853a1a6f2ef784e223c099ea5c771405a3f89 chore(ci): bump goreleaser/goreleaser-action from 7.0.0 to 7.1.0 (#75)
* e78b0d04e0d1fd55bf4b13f51ebe1cb8414eeb1e chore(ci): bump goreleaser/goreleaser-action from 7.1.0 to 7.2.1 (#79)
* 1c52bc7cd703ec83a574b80a88da75a4f7707436 chore(ci): bump goreleaser/goreleaser-action from 7.2.1 to 7.2.2 (#85)
* 64d5adedacfea2831ad19210b5306e526abc64af chore(ci): bump securego/gosec from 2.22.11 to 2.23.0 (#51)
* d5fbd59b6cc6d2b5333889234ae481797d7be0cd chore(ci): bump securego/gosec from 2.23.0 to 2.24.7 (#56)
* 9bff86e7baf955f63537b6b1c8c7546ef57bcb99 chore(ci): bump securego/gosec from 2.24.7 to 2.25.0 (#64)
* df4a40a31dd23339dbeab1a03b6183ff11e96e4c chore(ci): bump securego/gosec from 2.25.0 to 2.26.1 (#78)
* 2c37172dace3aef63f88f900e08366043b1699fc chore(ci): bump sigstore/cosign-installer from 4.0.0 to 4.1.0 (#58)
* afdc323653d30c8d53d6f36d899f7b54e5a0789a chore(ci): bump sigstore/cosign-installer from 4.1.0 to 4.1.1 (#72)
* bbac621163e4a1bfc3791d6f63e09bdb405ed4e4 chore(ci): bump sigstore/cosign-installer from 4.1.1 to 4.1.2 (#83)
* d244aa0b7f0b2bccfe058b3cf2e35da80d65bedc chore(deps): bump pgregory.net/rapid from 1.2.0 to 1.3.0 (#80)
</details>
txeh-v1.8.02026-02-09
Features
- DNS cache flush after hosts file changes (#43, #48)
- Library: FlushDNSCache() function, AutoFlush option in HostsConfig
- CLI: --flush / -f flag, TXEHAUTOFLUSH=1 env var
- macOS: dscacheutil -flushcache + killall -HUP mDNSResponder
- Linux: resolvectl flush-caches (systemd 239+), falls back to systemd-resolve --flush-caches
- Windows: ipconfig /flushdns
- Flush failures are non-fatal. The hosts file is saved, a warning is printed to stderr.
Improvements
- Refactored CLI mutation commands (add, remove host/ip/cidr/comment) to use shared saveHosts helper
- Descriptive error message when Linux has no systemd-resolved (explains that flushing isn't needed)
- Source citations in platform flush implementations (man pages, vendor docs)
Fixes
- Build-tag platform-specific tests so they only compile on the correct OS
- Bump Go from 1.24.4 to 1.24.7 (fixes GO-2025-3956 os/exec.LookPath vuln)
Documentation
- CLI reference: --flush flag, TXEHAUTOFLUSH, platform requirements table
- Troubleshooting: cross-reference to --flush from manual flush instructions
Changelog
Others
* 99a9be89258822319df5fcf79ce28acb09b85448 Add DNS cache flush support (--flush flag) (#48)
* 7e2837d8f3b32864318e018b0e0f0ec93f71f213 housekeeping: fix OpenSSF Scorecard token permissions and pin docs deps (#42)
* e1816974e0c631fc1c2f2ccd5ac9285a9f46e3f9 housekeeping: tighten linting, testing, and verification standards (#47)
txeh-v1.7.22026-02-06
Features
- Enterprise-grade CI pipeline, linting, and code quality tooling (#31)
- Fuzz tests, property-based tests, dead code detection (#37)
Improvements
- CLI cmd package test coverage: 44.7% to 81.6% (#39)
- Overall test coverage: 90.2% to 92.4% (#39)
- Documentation overhaul (#38)
- README attribution footer (#39)
Fixes
- Fix cosign signing to use --bundle flag for cosign v2.4+ (#40)
- Fix goreleaser homebrew formula push to use HOMEBREWTAPTOKEN (#41)
Dependencies
- Bump github.com/spf13/cobra from 1.9.1 to 1.10.2 (#33)
CI
- Bump codecov/codecov-action (#32)
- Bump github/codeql-action (#34)
- Bump ossf/scorecard-action (#35)
- Bump securego/gosec (#36)
Changelog
Others
* 50162d4378df48b3bb3e113d5d329b5fcdbeacbc Add fuzz tests, property-based tests, dead code detection, and docs CNAME (#37)
* fe7ba4e34401c8d26a498250bd8f90f13ad1d748 Documentation Overhaul (#38)
* 2ca0f7c29277415b7aea0fc0473f80898767cd6a Feat/enterprise standards (#31)
* d2f988f540a3c71f14adaa70a8ce1746b92a8d38 Test Coverage, CLAUDE.md, and README Footer (#39)
* bedfe2c89aebcdfd598fa68f4e725500d99a68d7 chore(ci): bump codecov/codecov-action (#32)
* c1d8a21995080b98f881e4e8fe505714f783d278 chore(ci): bump github/codeql-action (#34)
* aecd4bec44a30791c097deb1f0086703037200d6 chore(ci): bump ossf/scorecard-action (#35)
* 2d8d0ca5c81f379d26739c08ecc070cf4229a6f2 chore(ci): bump securego/gosec (#36)
* 319b00b16c62f8c2661eeca8d7a94d0dc149a67b chore(deps): bump github.com/spf13/cobra from 1.9.1 to 1.10.2 (#33)
77ad3964d79af54de7896879dd02dfcc946760b5 housekeeping: add HOMEBREWTAP_TOKEN to .goreleaser.yml (#41)
* fa1f898f13e6e6b19e9a962870d1f6f2599c4d28 housekeeping: update .goreleaser.yml to adjust signing args and output configuration (#40)
txeh-v1.7.02025-12-20
This release introduces two major features: inline comment support for organizing and tracking host entries, and MaxHostsPerLine for controlling line length (particularly important for Windows compatibility).
---
New Features
1. Inline Comment Support
Host entries can now include comments for organization, tracking, and grouping. Comments follow the standard hosts file format: IP HOSTNAME [HOSTNAME...] # comment
How Comments Work
- Grouping by comment: Hosts with the same IP AND same comment are placed on the same line
- Comment isolation: Hosts added without a comment only match lines without a comment
- Comment matching: Hosts added with a comment only match lines with the same comment
- Comment preservation: Comments are never modified on existing lines
- New line creation: If no matching line exists, a new line is created
Library API
``go
// Add hosts with a comment (for grouping/tracking)
hosts.AddHostWithComment("127.0.0.1", "myapp", "local development")
hosts.AddHostsWithComment("127.0.0.1", []string{"svc1", "svc2"}, "my project services")
// Add hosts without a comment (original behavior)
hosts.AddHost("127.0.0.1", "myhost")
hosts.AddHosts("127.0.0.1", []string{"a", "b", "c"})
// List all hosts with a specific comment
devHosts := hosts.ListHostsByComment("local development")
// Remove all entries with specific comments
hosts.RemoveByComment("old environment")
hosts.RemoveByComments([]string{"deprecated", "temp"})
`
CLI Usage
`bash
Add host with a comment
sudo txeh add 127.0.0.1 myapp --comment "local development"
Result: 127.0.0.1 myapp # local development
Add another host with the same comment (groups together)
sudo txeh add 127.0.0.1 myapp2 --comment "local development"
Result: 127.0.0.1 myapp myapp2 # local development
Add host with a different comment (new line)
sudo txeh add 127.0.0.1 api --comment "staging environment"
Result: 127.0.0.1 api # staging environment
List all hosts with a specific comment
sudo txeh list bycomment "local development"
Remove all entries with a specific comment
sudo txeh remove bycomment "local development"
`
---
2. MaxHostsPerLine Configuration
Control the maximum number of hostnames per line. This is particularly important for Windows, which only reads the first 9 hostnames per line (additional hostnames are silently ignored).
> Note: The Windows 9-host limit is empirically observed behavior, not officially documented by Microsoft. See: kubefwd#258, ddev#948, crc#2710
Configuration Values
| Value | Behavior |
|-------|----------|
| 0 (default) | Auto mode: 9 hosts per line on Windows, unlimited elsewhere |
| -1 | Force unlimited (no wrapping) |
| >0 | Explicit limit (e.g., 5 means max 5 hosts per line) |
Library API
`go
hosts, err := txeh.NewHosts(&txeh.HostsConfig{
MaxHostsPerLine: 9, // Explicit limit
})
// Or use auto mode (default)
hosts, err := txeh.NewHostsDefault() // Uses 9 on Windows, unlimited elsewhere
`
CLI Usage
`bash
Set explicit limit
sudo txeh add 127.0.0.1 host1 host2 host3 --max-hosts-per-line 2
Use auto mode (default, 9 on Windows)
sudo txeh add 127.0.0.1 host1 host2 host3
Force unlimited
sudo txeh add 127.0.0.1 host1 host2 host3 --max-hosts-per-line -1
`
---
CLI Changes
New Flags
| Flag | Short | Description |
|------|-------|-------------|
| --comment | -c | Add comment to host entry (for add command) |
| --max-hosts-per-line | -m | Max hostnames per line (0=auto, -1=unlimited, >0=explicit) |
New Subcommands
| Command | Description |
|---------|-------------|
| txeh list bycomment "COMMENT" | List all hosts with a specific comment |
| txeh remove bycomment "COMMENT" | Remove all entries with a specific comment |
---
API Additions
New Methods
| Method | Description |
|--------|-------------|
| AddHostWithComment(address, hostname, comment string) | Add a host with an inline comment |
| AddHostsWithComment(address string, hostnames []string, comment string) | Add multiple hosts with an inline comment |
| ListHostsByComment(comment string) []string | Get all hostnames with a specific comment |
| RemoveByComment(comment string) | Remove all entries with a specific comment |
| RemoveByComments(comments []string) | Remove all entries with any of the specified comments |
New HostsConfig Field
| Field | Type | Description |
|-------|------|-------------|
| MaxHostsPerLine | int | Maximum hostnames per line (0=auto, -1=unlimited, >0=explicit) |
---
Use Cases
Tracking Tool-Managed Entries
`bash
Docker adding entries
sudo txeh add 127.0.0.1 app.local --comment "docker"
Kubernetes port-forward (kubefwd)
sudo txeh add 127.0.0.1 myservice.mynamespace --comment "kubefwd"
Clean up all entries from a specific tool
sudo txeh remove bycomment "docker"
`
Development Environment Organization
`bash
Add dev services
sudo txeh add 127.0.0.1 api.local web.local --comment "dev services"
sudo txeh add 127.0.0.1 db.local cache.local --comment "dev services"
Add staging services (separate line)
sudo txeh add 192.168.1.100 api.staging --comment "staging"
List dev services
sudo txeh list bycomment "dev services"
`
Modifying Comments
Comments are never modified on existing lines. To change a comment, remove and re-add:
`bash
Remove all entries with the old comment
sudo txeh remove bycomment "old comment"
Re-add with the new comment
sudo txeh add 127.0.0.1 app1 app2 --comment "new comment"
`
---
Full Changelog
Features
- Add inline comment support for host entries
- Add AddHostWithComment() and AddHostsWithComment() methods
- Add ListHostsByComment() method
- Add RemoveByComment() and RemoveByComments() methods
- Add --comment / -c flag to CLI add command
- Add list bycomment CLI subcommand
- Add remove bycomment CLI subcommand
- Add MaxHostsPerLine configuration option
- Add --max-hosts-per-line / -m CLI flag
- Auto-detect Windows and apply 9-host limit by default
Testing
- Add comprehensive tests for comment functionality
- Add tests for MaxHostsPerLine and Windows 9-host limit
- Add whitespace handling tests for comments
Documentation
- Add comprehensive "Comments" section to README
- Document comment matching behavior
- Add CLI and library usage examples
- Update CLI help output with new flags
---
Installation
CLI
`bash
Using Go install (requires Go 1.24+)
go install github.com/txn2/txeh/txeh@v1.7.0
Using Homebrew
brew install txn2/tap/txeh
`
Library
`bash
go get github.com/txn2/txeh@v1.7.0
``
---
Checksums
See the assets below for SHA256 checksums of all release binaries.
txeh-v1.6.02025-12-20
This release contains critical bug fixes addressing thread safety, parsing edge cases, and panic conditions. It also includes a breaking API change to improve data safety, enhanced Windows compatibility, and significantly expanded test coverage.
---
Breaking Changes
GetHostFileLines() Return Type Changed
GetHostFileLines() now returns HostFileLines (value) instead of *HostFileLines (pointer).
Reason: The previous implementation returned a pointer to internal state, which allowed external code to modify the Hosts struct's data without going through the mutex-protected API. This created potential data races and violated encapsulation.
Migration:
``go
// Before (v1.5.x)
lines := hosts.GetHostFileLines()
for i := range *lines {
line := (*lines)[i]
// ...
}
// After (v1.6.0)
lines := hosts.GetHostFileLines()
for i := range lines {
line := lines[i]
// ...
}
`
---
Bug Fixes
1. Last Line Dropped When Hosts File Has No Trailing Newline
When parsing a hosts file that did not end with a newline character, the last line was silently dropped. This could result in missing host entries. The parser now correctly handles files regardless of whether they end with a newline.
2. GetHostFileLines Data Race Fixed
As noted in the breaking changes section, GetHostFileLines() now returns a defensive copy of the internal data. This prevents data races when the returned slice is accessed concurrently with modifications to the Hosts struct.
3. TOCTOU Race Condition in AddHost
The AddHost method had a time-of-check-to-time-of-use (TOCTOU) race condition where it would release the mutex between checking existing entries and adding new ones. The lock is now held for the entire operation, ensuring atomic read-modify-write behavior.
4. Nil Pointer Panic in ListHostsByCIDR
Calling ListHostsByCIDR with an invalid CIDR string (e.g., malformed or unparseable) would cause a nil pointer panic. The method now validates input and returns an empty result for invalid CIDR strings instead of panicking.
---
New Features and Improvements
Windows Hosts File Location
The Windows hosts file location is now determined using the SystemRoot environment variable instead of assuming C:\Windows. This improves compatibility with non-standard Windows installations where the system root is on a different drive or path.
Contributors: @errpunk (PR #27)
Code Quality Improvements
- Updated to Go 1.23
- Applied gofumpt and gofmt formatting
- Fixed all staticcheck warnings
- Improved error handling for deferred file operations in tests
Contributors: @shantanugadgil (PR #29)
---
Test Coverage
This release includes a significant expansion of the test suite:
- Over 1,500 lines of new test code
- Comprehensive CLI integration tests covering all commands
- Library unit tests for edge cases and error conditions
- Regression tests for all bug fixes listed above
- Race condition tests to verify thread safety
---
Upgrade Recommendations
All users should upgrade. The parsing bug that drops the last line when files lack a trailing newline could cause silent data loss. The thread safety fixes are important for any concurrent usage of the library.
If you have code that depends on GetHostFileLines(), see the migration guide in the Breaking Changes section above.
---
Full Changelog
Bug Fixes
- Fix last line being dropped when hosts file has no trailing newline
- Fix data race in GetHostFileLines by returning a copy
- Fix TOCTOU race condition in AddHost
- Fix nil pointer panic in ListHostsByCIDR with invalid CIDR
Enhancements
- Use SystemRoot environment variable for Windows hosts file location
- Update to Go 1.23
- Apply gofumpt/gofmt formatting
- Fix staticcheck warnings
Testing
- Add comprehensive CLI integration tests
- Add library unit tests for edge cases
- Add regression tests for all bug fixes
Documentation
- Update README with clarified Go install requirements
- Add Go Reference badge
- Add feature list and fix typos
---
Contributors
Thank you to the contributors who helped with this release:
- @errpunk - Windows SystemRoot environment variable support
- @shantanugadgil - Go version update and staticcheck fixes
---
Installation
CLI
`bash
Using Go install (requires Go 1.23+)
go install github.com/txn2/txeh/txeh@v1.6.0
Using Homebrew
brew install txn2/tap/txeh
`
Library
`bash
go get github.com/txn2/txeh@v1.6.0
``
---
Checksums
See the assets below for SHA256 checksums of all release binaries.
txeh-v1.5.4 craig2023-09-06
Changelog
* 3446338 Merge pull request #23 from txn2/add-support-for-go-install
* f9fd663 Update documentation for release
* ec2d936 Update version management
* 224ab3b Add support for 'go install' command
* 6a28df2 housekeeping
txeh-v1.5.3 craig2023-07-24
Changelog
* e8cdde2 small go test for each method
* f47bf69 added RemoveCIDRs
* bec8513 trim spaces for host address
* c4fba31 added locks for list methods
* 52bfc1a test update
txeh-v1.5.2 craig2023-07-23
Changelog
* 138208f added the ability to create a Hosts object with a string (RawText)
* ae6552a added simple test
* c879719 fixed typos and documentation cleanup
* fd6fec4 added list command to cli, along with new hosts methods ListHostsByIP, ListHostsByCIDR and ListAddressesByHost
* 171fcae housekeeping, fixed typos and deprications
* ca3571a upgrade Go to 1.20, upgrade all packages
txeh-v1.4.0 craig2023-05-05
Changelog
* 16104e9 housekeeping
* 6c60742 build update
* 1df13ff go 1.19
* d454958 Merge pull request #21 from girishsg24/supportdualstack
* 0c4a4f4 support dualstack entries in /etc/hosts file
* 8a33103 Merge pull request #18 from mattkasun/master
* 4d96640 remove erroneous extra blank line during parse
* b4a4cf7 Merge pull request #16 from docooler/master
* cd12e07 Update txeh.go
* 09e2953 Merge pull request #11 from chinglinwen/master
* a69484b fixes #10 handle no newline issue
txeh-v1.3.0 cjimti2019-12-03
Changelog
b5c7075 Merge pull request #5 from tommyblue/master
dc65154 Add 'show' argument to print the /etc/hosts file