crowdsecurity/crowdsec · error

basename: want string, got %T

Error message

basename: want string, got %T

What it means

basename is a custom expr helper used in hubtest expressions. It requires exactly one string argument; if params[0] is not a string it returns 'basename: want string, got <type>'. This means an expression called basename() with a non-string value (nil, number, map, etc.).

Source

Thrown at pkg/hubtest/helpers.go:18

package hubtest

import (
	"errors"
	"fmt"
	"path/filepath"
)

func basename(params ...any) (any, error) {
	// keep nilaway happy
	if len(params) == 0 || params[0] == nil {
		return "", errors.New("basename: missing argument")
	}

	// keep forcetypeassert happy
	s, ok := params[0].(string)
	if !ok {
		return "", fmt.Errorf("basename: want string, got %T", params[0])
	}

	return filepath.Base(s), nil
}

View on GitHub (pinned to 909b515798)

Solutions

  1. Ensure the argument is a string: wrap with a conversion or use String() where applicable in the expression
  2. Guard the expression against a missing field (check field presence before calling basename)
  3. Fix the expression in the .expr file so it passes evt.Parsed.<field> which is populated as a string

Example fix

// before
basename(evt.Parsed.timestamp)   # timestamp is nil
// after
basename(evt.Parsed.filename)    # filename is a string field
Defensive patterns

Strategy: type-guard

Validate before calling

// in the expression, guard before use:
// evt.Parsed.filename != nil && String(evt.Parsed.filename) != ""

Type guard

func isString(v interface{}) bool { _, ok := v.(string); return ok }
// call basename only when isString(evt.Parsed.field)

Try / catch

_, err := hubtest.EvalExpr(expr, evt)
if err != nil && strings.Contains(err.Error(), "want string") {
    return fmt.Errorf("basename() arg must be a string: %w", err)
}

Prevention

When it happens

Trigger: A parser/scenario expression evaluates basename with a non-string operand — e.g. basename(evt.Parsed.field) where the field is absent (nil) or holds a non-string type at runtime.

Common situations: Assert or parser expression referencing a field that is sometimes missing from the parsed event; passing an integer/bool result of another helper into basename; typo making the expression evaluate to nil.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/035355b0bdcbb2ae. Report an issue: GitHub.