Skip to content

Commit 69afd9d

Browse files
committed
api: Allow unauthenticated access to user's SSH keys
This patch relaxes constraints on getting user's SSH keys via the JSON API. The same has been allowed by both GitHub and Gitlab and the output is already readable via http://domain/user.keys endpoint. The benefit of allowing it via the API are twofold: first this is a structured output and second it can be CORS-enabled. As a privacy precaution the `Title` property is set to an empty string if the request is unauthenticated. Fixes: #30681
1 parent 1e749b8 commit 69afd9d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

routers/api/v1/api.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,6 @@ func Routes() *web.Route {
916916
// Users (requires user scope)
917917
m.Group("/users", func() {
918918
m.Group("/{username}", func() {
919-
m.Get("/keys", user.ListPublicKeys)
920919
m.Get("/gpg_keys", user.ListGPGKeys)
921920

922921
m.Get("/followers", user.ListFollowers)
@@ -931,6 +930,13 @@ func Routes() *web.Route {
931930
}, context.UserAssignmentAPI())
932931
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser), reqToken())
933932

933+
// Users SSH keys (publicly readable)
934+
m.Group("/users", func() {
935+
m.Group("/{username}", func() {
936+
m.Get("/keys", user.ListPublicKeys)
937+
}, context.UserAssignmentAPI())
938+
})
939+
934940
// Users (requires user scope)
935941
m.Group("/user", func() {
936942
m.Get("", user.GetAuthenticatedUser)

routers/api/v1/user/key.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ func listPublicKeys(ctx *context.APIContext, user *user_model.User) {
8989
apiKeys := make([]*api.PublicKey, len(keys))
9090
for i := range keys {
9191
apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
92-
if ctx.Doer.IsAdmin || ctx.Doer.ID == keys[i].OwnerID {
93-
apiKeys[i], _ = appendPrivateInformation(ctx, apiKeys[i], keys[i], user)
92+
if ctx.Doer != nil {
93+
if ctx.Doer.IsAdmin || ctx.Doer.ID == keys[i].OwnerID {
94+
apiKeys[i], _ = appendPrivateInformation(ctx, apiKeys[i], keys[i], user)
95+
}
96+
} else {
97+
// unauthenticated requests will not receive the title property
98+
// to preserve privacy
99+
apiKeys[i].Title = ""
94100
}
95101
}
96102

0 commit comments

Comments
 (0)