{"record":{"id":"cc53363b07f2e64b","repo":"ipfs/kubo","slug":"cannot-overwrite-key-with-name-self","errorCode":null,"errorMessage":"cannot overwrite key with name 'self'","messagePattern":"cannot overwrite key with name 'self'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/coreapi/key.go","lineNumber":190,"sourceCode":"// key was overwritten, or an error.\nfunc (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (coreiface.Key, bool, error) {\n\t_, span := tracing.Span(ctx, \"CoreAPI.KeyAPI\", \"Rename\", trace.WithAttributes(attribute.String(\"oldname\", oldName), attribute.String(\"newname\", newName)))\n\tdefer span.End()\n\n\toptions, err := caopts.KeyRenameOptions(opts...)\n\tif err != nil {\n\t\treturn nil, false, err\n\t}\n\tspan.SetAttributes(attribute.Bool(\"force\", options.Force))\n\n\tks := api.repo.Keystore()\n\n\tif oldName == \"self\" {\n\t\treturn nil, false, errors.New(\"cannot rename key with name 'self'\")\n\t}\n\n\tif newName == \"self\" {\n\t\treturn nil, false, errors.New(\"cannot overwrite key with name 'self'\")\n\t}\n\n\toldKey, err := ks.Get(oldName)\n\tif err != nil {\n\t\treturn nil, false, fmt.Errorf(\"no key named %s was found\", oldName)\n\t}\n\n\tpubKey := oldKey.GetPublic()\n\n\tpid, err := peer.IDFromPublicKey(pubKey)\n\tif err != nil {\n\t\treturn nil, false, err\n\t}\n\n\t// This is important, because future code will delete key `oldName`\n\t// even if it is the same as newName.\n\tif newName == oldName {\n\t\tk, err := newKey(oldName, pid)","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/coreapi/key.go#L172-L208","documentation":"CoreAPI Key.Rename refuses to overwrite the reserved key 'self' when renaming with force: overwriting the identity alias would replace what 'self' means for the whole node, so it is disallowed even with --force.","triggerScenarios":"KeyAPI.Rename(ctx, oldName, \"self\", opts...) or `ipfs key rename mykey self` (including --force).","commonSituations":"Users trying to make another key 'primary' by naming it self; scripts constructing target names from user input without filtering reserved words.","solutions":["Pick a different target name for the rename","Remove the colliding regular key explicitly with 'ipfs key rm' first if appropriate"],"exampleFix":"// before\napi.Key().Rename(ctx, \"mykey\", \"self\", caopts.AllowOverwrite(true))\n// after\nif newName == \"self\" {\n    return errors.New(\"'self' is reserved for the node identity\")\n}\napi.Key().Rename(ctx, \"mykey\", newName, caopts.AllowOverwrite(true))","handlingStrategy":"validation","validationCode":"if newName == \"self\" {\n    return errors.New(\"target name 'self' is reserved for the node identity\")\n}","typeGuard":"func isReservedKeyName(n string) bool { return n == \"self\" }","tryCatchPattern":"ok, overwritten, err := api.Key().Rename(ctx, oldName, newName, opts...)\nif err != nil && strings.Contains(err.Error(), \"overwrite key with name 'self'\") {\n    return errReservedTarget // reprompt for a different target name\n}","preventionTips":["Reject 'self' as a rename target in any user-facing input path","Remember Force/AllowOverwrite does not bypass the 'self' check","Publish under a distinct key instead of renaming onto 'self'"],"tags":["keystore","keys","validation"],"backgroundTag":"reserved-key-name","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}