{"record":{"id":"86e61d0d388ad0d1","repo":"JuliusBrussee/caveman","slug":"tenantkey-safe-month-and-request-id-are-required","errorCode":null,"errorMessage":"tenantkey: safe month and request id are required","messagePattern":"tenantkey: safe month and request id are required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/tenantkey/tenantkey.go","lineNumber":38,"sourceCode":"func MonthlySpend(organizationID, projectID, month string) (string, error) {\n\tprefix, err := projectPrefix(\"spend\", organizationID, projectID)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif !safePart(month) {\n\t\treturn \"\", errors.New(\"tenantkey: safe month is required\")\n\t}\n\treturn prefix + \":\" + month, nil\n}\n\n// MonthlySpendEvent returns idempotency-marker key for one spend event.\nfunc MonthlySpendEvent(organizationID, projectID, month, requestID string) (string, error) {\n\tprefix, err := projectPrefix(\"spend-event\", organizationID, projectID)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif !safePart(month) || !safePart(requestID) {\n\t\treturn \"\", errors.New(\"tenantkey: safe month and request id are required\")\n\t}\n\treturn prefix + \":\" + month + \":\" + requestID, nil\n}\n\nfunc projectPrefix(namespace, organizationID, projectID string) (string, error) {\n\tif !safePart(organizationID) || !safePart(projectID) {\n\t\treturn \"\", errors.New(\"tenantkey: safe organization and project scope are required\")\n\t}\n\treturn \"cave:\" + namespace + \":\" + organizationID + \":\" + projectID, nil\n}\n\nfunc safePart(value string) bool {\n\tvalue = strings.TrimSpace(value)\n\treturn value != \"\" && !strings.ContainsAny(value, \":\\r\\n\\x00\")\n}\n","sourceCodeStart":20,"sourceCodeEnd":54,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/tenantkey/tenantkey.go#L20-L54","documentation":"MonthlySpendEvent builds an idempotency-marker key from month and requestID and requires both to pass safePart. If either is empty or contains unsafe characters, the library refuses to construct the key, since a bad segment could break key namespacing or idempotency guarantees.","triggerScenarios":"Calling tenantkey.MonthlySpendEvent(orgID, projectID, month, requestID) with month = \"\", requestID = \"\", or either containing unsafe characters (slashes, colons, spaces) — e.g. an ungenerated request ID or a month not normalized to \"2026-09\" form.","commonSituations":"Retry handler called before requestID was assigned (empty string); request ID taken from a header that was absent; month parsed with slashes; both fields passed through unvalidated from an HTTP request body.","solutions":["Generate the request ID before constructing the key (uuid.NewString() or the gateway request ID) and ensure month is formatted via t.Format(\"2006-01\").","Validate both inputs at the handler boundary: non-empty, trimmed, matching ^\\d{4}-\\d{2}$ for month and a safe ID pattern for requestID.","When requestID comes from a header (e.g. X-Request-Id), require it explicitly and reject the request when missing instead of passing \"\" through."],"exampleFix":"// before\nkey, err := tenantkey.MonthlySpendEvent(org, proj, month, r.Header.Get(\"X-Request-Id\")) // may be \"\"\n// after\nmonth = strings.TrimSpace(month)\nrequestID := r.Header.Get(\"X-Request-Id\")\nif requestID == \"\" {\n    requestID = uuid.NewString()\n}\nif !safe(month) || !safe(requestID) {\n    return errors.New(\"month and request id must be non-empty and safe\")\n}\nkey, err := tenantkey.MonthlySpendEvent(org, proj, month, requestID)","handlingStrategy":"validation","validationCode":"var idRe = regexp.MustCompile(`^[A-Za-z0-9_-]{1,128}$`)\nfunc validSpendEventArgs(month, requestID string) bool {\n    return regexp.MustCompile(`^\\d{4}-\\d{2}$`).MatchString(month) && idRe.MatchString(requestID)\n}","typeGuard":null,"tryCatchPattern":"key, err := tenantkey.MonthlySpendEvent(org, proj, month, requestID)\nif err != nil && strings.Contains(err.Error(), \"safe month and request id are required\") {\n    return fmt.Errorf(\"month %q / request id %q must be non-empty safe tokens\", month, requestID)\n}","preventionTips":["Generate or require the request ID before building the idempotency key","Never pass header values through unvalidated (missing header = empty string)","Normalize month format centrally (one helper) so all callers emit YYYY-MM"],"tags":["tenantkey","idempotency","validation","go"],"backgroundTag":"missing-required-argument","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}