{"record":{"id":"56786654b9639783","repo":"gofiber/fiber","slug":"csrf-failed-to-store-key-q-w","errorCode":null,"errorMessage":"csrf: failed to store key %q: %w","messagePattern":"csrf: failed to store key %q: %w","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"middleware/csrf/storage_manager.go","lineNumber":60,"sourceCode":"\t\treturn raw, nil\n\t}\n\n\tif value := m.memory.Get(key); value != nil {\n\t\traw, ok := value.([]byte)\n\t\tif !ok {\n\t\t\treturn nil, fmt.Errorf(\"csrf: unexpected value type %T in storage\", value)\n\t\t}\n\t\treturn raw, nil\n\t}\n\n\treturn nil, nil\n}\n\n// set data to storage or memory\nfunc (m *storageManager) setRaw(ctx context.Context, key string, raw []byte, exp time.Duration) error {\n\tif m.storage != nil {\n\t\tif err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {\n\t\t\treturn fmt.Errorf(\"csrf: failed to store key %q: %w\", m.logKey(key), err)\n\t\t}\n\t\treturn nil\n\t}\n\n\tm.memory.Set(key, raw, exp)\n\treturn nil\n}\n\n// delete data from storage or memory\nfunc (m *storageManager) delRaw(ctx context.Context, key string) error {\n\tif m.storage != nil {\n\t\tif err := m.storage.DeleteWithContext(ctx, key); err != nil {\n\t\t\treturn fmt.Errorf(\"csrf: failed to delete key %q: %w\", m.logKey(key), err)\n\t\t}\n\t\treturn nil\n\t}\n\n\tm.memory.Delete(key)","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/middleware/csrf/storage_manager.go#L42-L78","documentation":"Returned by storageManager.setRaw when the external Storage's SetWithContext fails while persisting a CSRF token. This is the storage-driver-level failure that bubbles up as error 152 during token creation/extension.","triggerScenarios":"CSRF token set against cfg.Storage where Storage.SetWithContext errors: backend down, write timeout, quota exceeded, ACL missing SET, context cancellation.","commonSituations":"Redis at maxmemory, storage outage during token issuance, ACL misconfiguration, connection pool exhaustion under load, custom Storage impl returning errors on Set.","solutions":["Inspect the wrapped error to identify capacity/network/permission cause.","Verify Storage credentials have SET and the backend has free capacity.","Tune write timeout and connection pool size; confirm the backend supports the configured IdleTimeout expiration semantics.","If using a custom Storage impl, ensure SetWithContext only errors on genuine failures, not on routine operations."],"exampleFix":"// before: token issuance fails opaquely when storage is unavailable\nif err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {\n    return fmt.Errorf(\"csrf: failed to store key %q: %w\", m.logKey(key), err)\n}\n\n// after: distinguish capacity vs transient and degrade cleanly\nif err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {\n    if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {\n        return err\n    }\n    log.Error(\"csrf token store failed:\", err)\n    return err // caller decides fail-open vs fail-closed\n}","handlingStrategy":"retry","validationCode":"func validateCsrfSet(ctx context.Context, s fiber.Storage) error {\n    return s.SetWithContext(ctx, \"__csrf_set__\", []byte(\"x\"), time.Second)\n}","typeGuard":null,"tryCatchPattern":"if err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {\n    if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {\n        return err\n    }\n    log.Error(\"csrf token store failed:\", err)\n    return err // caller returns 503 to client\n}","preventionTips":["Provision Storage capacity and SET permission.","Confirm backend TTL support matches IdleTimeout semantics.","Monitor write latency and capacity under load."],"tags":["csrf","storage","network","write","auth","go","fiber"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}