Skip to content

fix: reject an unsupported -hash algorithm instead of printing empty brackets - #1049

Open
qianeric-backup wants to merge 1 commit into
projectdiscovery:mainfrom
qianeric-backup:fix/reject-unknown-hash-algorithm
Open

qianeric-backup wants to merge 1 commit into
projectdiscovery:mainfrom
qianeric-backup:fix/reject-unknown-hash-algorithm

Conversation

@qianeric-backup

@qianeric-backup qianeric-backup commented Nov 21, 2026 •

Copy link
Copy Markdown

-hash tacos is not a hash algorithm, but the text writer only fills in a value for md5, sha1 and sha256 while still emitting the brackets, so the host line looks like it is missing a fingerprint:

$ tlsx -u 127.0.0.1 -p 18443 -silent -hash tacos
127.0.0.1:18443 []

It also mangles a partially valid list, and an uppercase name is silently dropped even though it names the same algorithm:

$ tlsx -u 127.0.0.1 -p 18443 -silent -hash md5,tacos
127.0.0.1:18443 [55fc8bad27f4c5425c57e2057744b17f] []

$ tlsx -u 127.0.0.1 -p 18443 -silent -hash md5,,sha1
127.0.0.1:18443 [55fc8bad27f4c5425c57e2057744b17f] [] [c8fe018e782788bc15150a72b8a7070a88d9d5b0]

$ tlsx -u 127.0.0.1 -p 18443 -silent -hash MD5
127.0.0.1:18443 []

Cause

pkg/output/output.go walks the requested list and only assigns value for the three known names, then prints it unconditionally:

for _, hash := range hashOpts {
	var value string
	builder.WriteString(" [")
	switch hash {
	case "md5":
		value = cert.FingerprintHash.MD5
	case "sha1":
		value = cert.FingerprintHash.SHA1
	case "sha256":
		value = cert.FingerprintHash.SHA256
	}
	builder.WriteString(w.aurora.BrightMagenta(value).String())
	builder.WriteString("]")
}

Fix

Every other enumerated flag already rejects a bad value before scanning — -ct tacos answers invalid value "tacos" for flag -ct: allowed values are all, insecure, secure, weak, -sm tacos fails with scan mode tacos not supported (supported: ctls, ztls, openssl, auto), and -tg "" with tls-groups requires at least one group (...). -hash now does the same, using the same validate*Flag helper pattern that -tls-groups introduced next to it:

if err := validateHashFlag(options.Hash); err != nil {
	return err
}
$ tlsx -u 127.0.0.1 -p 18443 -silent -hash tacos
FTL cause="hash: unsupported algorithm \"tacos\" (supported: md5,sha1,sha256)" chain="could not read flags"

The check runs after the -config file is merged, so a bad value supplied there is caught as well:

$ printf 'hash: nosuch\n' > cfg.yaml
$ tlsx -u 127.0.0.1 -p 18443 -silent -config cfg.yaml
FTL cause="hash: unsupported algorithm \"nosuch\" (supported: md5,sha1,sha256)" chain="could not read flags"

Valid values are unchanged — -hash md5, -hash sha1, -hash sha256 and -hash md5,sha1,sha256 print exactly what they printed before, and the JSON output still carries fingerprint_hash.

Tests

TestValidateHashFlag in cmd/tlsx/main_test.go, following TestValidateTLSGroupsFlag in the same file: the four accepted forms plus tacos, MD5, sha512, md5,tacos, md5,,sha1 and md5, rejected.

go test ./... is green and golangci-lint run ./cmd/... reports 0 issues.

Summary by CodeRabbit

  • Bug Fixes
    • Hash algorithm settings are now validated whether they come from command-line flags or configuration files. Unsupported or incorrectly formatted values return an error listing the supported algorithms: md5, sha1, and sha256.
    • Empty hash settings remain accepted.

…brackets

`-hash tacos` is not a hash algorithm, but the text writer only fills in the
value for md5, sha1 and sha256 and still emits its brackets, so the host line
looks like it is missing a fingerprint:

    $ tlsx -u 127.0.0.1 -p 18443 -silent -hash tacos
    127.0.0.1:18443 []

It also mangles a partially valid list, and an uppercase name is silently
dropped even though it means the same algorithm:

    $ tlsx -u 127.0.0.1 -p 18443 -silent -hash md5,tacos
    127.0.0.1:18443 [55fc8bad27f4c5425c57e2057744b17f] []

    $ tlsx -u 127.0.0.1 -p 18443 -silent -hash md5,,sha1
    127.0.0.1:18443 [55fc8bad27f4c5425c57e2057744b17f] [] [c8fe018e782788bc15150a72b8a7070a88d9d5b0]

    $ tlsx -u 127.0.0.1 -p 18443 -silent -hash MD5
    127.0.0.1:18443 []

Every other enumerated flag rejects a bad value up front instead: `-ct tacos`
answers "invalid value \"tacos\" for flag -ct: allowed values are all,
insecure, secure, weak", `-sm tacos` fails with "scan mode tacos not supported
(supported: ctls, ztls, openssl, auto)" and `-tg ""` with "tls-groups requires
at least one group (...)".

Validate -hash the same way, reusing the existing validate*Flag pattern, so the
run stops before the scan:

    $ tlsx -u 127.0.0.1 -p 18443 -silent -hash tacos
    FTL cause="hash: unsupported algorithm \"tacos\" (supported: md5,sha1,sha256)" chain="could not read flags"

The check runs after the -config file is merged, so a bad value coming from a
config file is caught too.  Valid values are untouched: `-hash md5`,
`-hash sha256` and `-hash md5,sha1,sha256` print exactly what they did before.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The command now validates the hash option after merging configuration. It accepts an empty value or comma-separated md5, sha1, and sha256 values. Tests cover supported and unsupported inputs.

Changes

Hash flag validation

Layer / File(s) Summary
Hash option validation
cmd/tlsx/main.go, cmd/tlsx/main_test.go
validateHashFlag checks each comma-separated algorithm against md5, sha1, and sha256. readFlags applies this validation after configuration merging. Tests cover empty, supported, unsupported, uppercase, and malformed values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: mzack9999

Merge Risk: Minimal · up to dc597

This change rejects unsupported hash algorithm values up front instead of producing empty fingerprint output. Valid usage is unchanged. No merge-blocking risk is evident.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title says the change rejects an unsupported -hash algorithm instead of printing empty brackets. The summary confirms that readFlags now validates -hash after the config merge and returns an error…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title says the change rejects an unsupported -hash algorithm instead of printing empty brackets. The summary confirms that readFlags now validates -hash after the config merge and returns an error for unsupported names. The title names the main behavior change, so it is related to the changeset. It uses the conventional fix: prefix and is specific enough for a teammate scanning history.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit checks the hashes in a row
md5, sha1, sha256 glow
Empty fields pass without a sound
Unknown names are turned around
Config and flags now share the rule
The rabbit hops away from the tool

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: caddd9e8-ec74-4c4a-ba3f-efbc2784b0a4
Commits

Reviewing files that changed from the base of the PR and between 53e3d20 and dc597c3.

Files selected for processing: 2

  • cmd/tlsx/main.go
  • cmd/tlsx/main_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant