{"record":{"id":"b33643527e2b2924","repo":"crowdsecurity/crowdsec","slug":"error-compiling-duration-expr-of-s-w","errorCode":null,"errorMessage":"error compiling duration_expr of %s: %w","messagePattern":"error compiling duration_expr of (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/csprofiles/csprofiles.go","lineNumber":69,"sourceCode":"\n\t\tif runtime.Cfg.OnFailure != \"\" && runtime.Cfg.OnFailure != \"continue\" && runtime.Cfg.OnFailure != \"break\" && runtime.Cfg.OnFailure != \"apply\" {\n\t\t\treturn nil, fmt.Errorf(\"invalid 'on_failure' for '%s' : %s\", profile.Name, runtime.Cfg.OnFailure)\n\t\t}\n\n\t\tfor fIdx, filter := range profile.Filters {\n\t\t\tif runtimeFilter, err = expr.Compile(filter, exprhelpers.GetExprOptions(map[string]interface{}{\"Alert\": &models.Alert{}})...); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"error compiling filter of '%s': %w\", profile.Name, err)\n\t\t\t}\n\n\t\t\truntime.RuntimeFilters[fIdx] = runtimeFilter\n\t\t\tif profile.Debug != nil && *profile.Debug {\n\t\t\t\truntime.Logger.Logger.SetLevel(log.DebugLevel)\n\t\t\t}\n\t\t}\n\n\t\tif profile.DurationExpr != \"\" {\n\t\t\tif runtimeDurationExpr, err = expr.Compile(profile.DurationExpr, exprhelpers.GetExprOptions(map[string]interface{}{\"Alert\": &models.Alert{}})...); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"error compiling duration_expr of %s: %w\", profile.Name, err)\n\t\t\t}\n\n\t\t\truntime.RuntimeDurationExpr = runtimeDurationExpr\n\t\t}\n\n\t\tfor _, decision := range profile.Decisions {\n\t\t\tif runtime.RuntimeDurationExpr == nil {\n\t\t\t\tvar duration string\n\t\t\t\tif decision.Duration != nil {\n\t\t\t\t\tduration = *decision.Duration\n\t\t\t\t} else {\n\t\t\t\t\truntime.Logger.Warningf(\"No duration specified for %s, using default duration %s\", profile.Name, defaultDuration)\n\t\t\t\t\tduration = defaultDuration\n\t\t\t\t}\n\n\t\t\t\tif _, err := cstime.ParseDurationWithDays(duration); err != nil {\n\t\t\t\t\treturn nil, fmt.Errorf(\"error parsing duration '%s' of %s: %w\", duration, profile.Name, err)\n\t\t\t\t}","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/csprofiles/csprofiles.go#L51-L87","documentation":"CrowdSec profiles support a `duration_expr` expression (expr language) that dynamically computes a ban duration per alert. At profile load time (NewProfile), each duration_expr is compiled with expr.Compile; if the expression is not valid expr syntax or references unknown fields/functions, compilation fails and NewProfile returns this wrapped error, aborting startup/configuration load.","triggerScenarios":"Calling NewProfile (directly or via getProfilesConfigs/New during config load) with a profile whose `duration_expr` contains invalid expr syntax, an unknown field (e.g. not under Alert), a typo'd function name, or wrong types that expr's type checker rejects.","commonSituations":"Hand-editing profiles.yaml and typo-ing the expression; copying a filter expression that returns a non-duration value; using a helper function not registered in exprhelpers; referencing a variable like `duration` that doesn't exist in the expression environment.","solutions":["Read the wrapped inner error: it pinpoints the expr parse/type error and position in duration_expr.","Test the expression standalone (cscli or an expr playground) against an Alert model before putting it in config.","Verify any function used exists in pkg/exprhelpers registry.","Simplify: if the duration is constant, use the static `duration` field instead of duration_expr."],"exampleFix":"// before (profiles.yaml)\nduration_expr: Alert.StartAt + '4h'\n// after\nduration_expr: \"'4h'\"  # or a valid expr yielding a string like '4h'","handlingStrategy":"validation","validationCode":"if profile.DurationExpr != \"\" {\n    if _, err := expr.Compile(profile.DurationExpr, exprhelpers.GetExprOptions(map[string]interface{}{\"Alert\": &models.Alert{}})...); err != nil {\n        return fmt.Errorf(\"invalid duration_expr for %s: %w\", profile.Name, err)\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Lint duration_expr against the Alert model at config-write time, not at startup","Keep expressions simple; prefer static `duration` when no dynamic value is needed","Test custom profiles in a dev environment with `cscli` before deploying"],"tags":["config","expr","profile"],"backgroundTag":"invalid-config-value","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}