Skip to content

Panic: invalid regex in --assert-body/--assert-header/--assert-redirect crashes with a stack trace #17

Description

@korya

Problem

All regex-based assertions compile user-supplied patterns with regexp.MustCompile, so any invalid pattern panics instead of producing a clean error.

Reproduction

$ http-assert --assert-body '[unclosed' http://127.0.0.1:8791/ok
panic: regexp: Compile(`[unclosed`): error parsing regexp: missing closing ]: `[unclosed`

goroutine 1 [running]:
regexp.MustCompile({0x16f0022d0, 0x9})
	/opt/homebrew/Cellar/go/1.26.5/libexec/src/regexp/regexp.go:313 +0xb0
main.AssertBodyMatch({0x16f0022d0, 0x9})
	/…/assertions.go:125 +0x24
main.parseAssertionFlags(0x91639da4008)
	/…/main.go:236 +0x620

[exit=2]

Same for --assert-header 'X: (bad' (assertions.go:81) and --assert-redirect.

Why it matters

Assertion patterns require heavy shell escaping, so typos are routine. In CI this surfaces as a raw Go crash with exit code 2, which is not a documented exit code (see the exit-code issue).

Affected code

  • assertions.go:81AssertHeaderMatch
  • assertions.go:125AssertBodyMatch
  • AssertRedirectMatch
  • called from main.go:221,236,264

Suggested fix

Compile patterns during flag parsing, return an error instead of panicking, and exit with the invalid-arguments code, e.g.:

Error: Invalid value for --assert-body flag: error parsing regexp: missing closing ]: `[unclosed`

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions