Skip to content

Commit

Permalink
fix(api): public key percent encoding (#4165)
Browse files Browse the repository at this point in the history
  • Loading branch information
heiytor authored and gustavosbarreto committed Sep 16, 2024
1 parent cdf5aec commit 5d752db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
5 changes: 5 additions & 0 deletions api/routes/sshkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package routes

import (
"net/http"
"net/url"
"strconv"

"github.com/shellhub-io/shellhub/api/pkg/gateway"
Expand Down Expand Up @@ -124,6 +125,10 @@ func (h *Handler) DeletePublicKey(c gateway.Context) error {
return err
}

// NOTE: This is a temporary workaround.
// TODO: Investigate why echo is not decoding the Fingerprint.
req.Fingerprint, _ = url.QueryUnescape(req.Fingerprint)

if err := c.Validate(&req); err != nil {
return err
}
Expand Down
31 changes: 24 additions & 7 deletions api/routes/sshkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestDeletePublicKey(t *testing.T) {
}{
{
description: "fails when role is observer",
fingerprint: "fingerprint",
fingerprint: "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8",
headers: map[string]string{
"Content-Type": "application/json",
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
Expand All @@ -202,7 +202,7 @@ func TestDeletePublicKey(t *testing.T) {
},
{
description: "fails when role is operator",
fingerprint: "fingerprint",
fingerprint: "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8",
headers: map[string]string{
"Content-Type": "application/json",
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
Expand All @@ -214,8 +214,8 @@ func TestDeletePublicKey(t *testing.T) {
expected: Expected{status: http.StatusForbidden},
},
{
description: "fails when try to deleting an existing public key",
fingerprint: "fingerprint",
description: "fails when try to deleting a non existent public key",
fingerprint: "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8",
headers: map[string]string{
"Content-Type": "application/json",
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
Expand All @@ -224,15 +224,32 @@ func TestDeletePublicKey(t *testing.T) {
},
requiredMocks: func() {
svcMock.
On("DeletePublicKey", gomock.Anything, "fingerprint", "00000000-0000-4000-0000-000000000000").
On("DeletePublicKey", gomock.Anything, "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8", "00000000-0000-4000-0000-000000000000").
Return(svc.ErrNotFound).
Once()
},
expected: Expected{status: http.StatusNotFound},
},
{
description: "success when fingerprint is encoded",
fingerprint: "8e%3Ab3%3Ae2%3Ace%3A3c%3A6c%3A27%3Aff%3A51%3Ac9%3A5d%3A77%3Aaf%3A92%3A2f%3Ad8",
headers: map[string]string{
"Content-Type": "application/json",
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
"X-Role": "owner",
"X-ID": "000000000000000000000000",
},
requiredMocks: func() {
svcMock.
On("DeletePublicKey", gomock.Anything, "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8", "00000000-0000-4000-0000-000000000000").
Return(nil).
Once()
},
expected: Expected{status: http.StatusOK},
},
{
description: "success when try to deleting an existing public key",
fingerprint: "fingerprint",
fingerprint: "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8",
headers: map[string]string{
"Content-Type": "application/json",
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
Expand All @@ -241,7 +258,7 @@ func TestDeletePublicKey(t *testing.T) {
},
requiredMocks: func() {
svcMock.
On("DeletePublicKey", gomock.Anything, "fingerprint", "00000000-0000-4000-0000-000000000000").
On("DeletePublicKey", gomock.Anything, "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8", "00000000-0000-4000-0000-000000000000").
Return(nil).
Once()
},
Expand Down

0 comments on commit 5d752db

Please sign in to comment.