Skip to content

Commit 5d752db

Browse files
heiytorgustavosbarreto
authored andcommitted
fix(api): public key percent encoding (#4165)
1 parent cdf5aec commit 5d752db

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

api/routes/sshkeys.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package routes
22

33
import (
44
"net/http"
5+
"net/url"
56
"strconv"
67

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

128+
// NOTE: This is a temporary workaround.
129+
// TODO: Investigate why echo is not decoding the Fingerprint.
130+
req.Fingerprint, _ = url.QueryUnescape(req.Fingerprint)
131+
127132
if err := c.Validate(&req); err != nil {
128133
return err
129134
}

api/routes/sshkeys_test.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func TestDeletePublicKey(t *testing.T) {
189189
}{
190190
{
191191
description: "fails when role is observer",
192-
fingerprint: "fingerprint",
192+
fingerprint: "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8",
193193
headers: map[string]string{
194194
"Content-Type": "application/json",
195195
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
@@ -202,7 +202,7 @@ func TestDeletePublicKey(t *testing.T) {
202202
},
203203
{
204204
description: "fails when role is operator",
205-
fingerprint: "fingerprint",
205+
fingerprint: "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8",
206206
headers: map[string]string{
207207
"Content-Type": "application/json",
208208
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
@@ -214,8 +214,8 @@ func TestDeletePublicKey(t *testing.T) {
214214
expected: Expected{status: http.StatusForbidden},
215215
},
216216
{
217-
description: "fails when try to deleting an existing public key",
218-
fingerprint: "fingerprint",
217+
description: "fails when try to deleting a non existent public key",
218+
fingerprint: "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8",
219219
headers: map[string]string{
220220
"Content-Type": "application/json",
221221
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
@@ -224,15 +224,32 @@ func TestDeletePublicKey(t *testing.T) {
224224
},
225225
requiredMocks: func() {
226226
svcMock.
227-
On("DeletePublicKey", gomock.Anything, "fingerprint", "00000000-0000-4000-0000-000000000000").
227+
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").
228228
Return(svc.ErrNotFound).
229229
Once()
230230
},
231231
expected: Expected{status: http.StatusNotFound},
232232
},
233+
{
234+
description: "success when fingerprint is encoded",
235+
fingerprint: "8e%3Ab3%3Ae2%3Ace%3A3c%3A6c%3A27%3Aff%3A51%3Ac9%3A5d%3A77%3Aaf%3A92%3A2f%3Ad8",
236+
headers: map[string]string{
237+
"Content-Type": "application/json",
238+
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
239+
"X-Role": "owner",
240+
"X-ID": "000000000000000000000000",
241+
},
242+
requiredMocks: func() {
243+
svcMock.
244+
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").
245+
Return(nil).
246+
Once()
247+
},
248+
expected: Expected{status: http.StatusOK},
249+
},
233250
{
234251
description: "success when try to deleting an existing public key",
235-
fingerprint: "fingerprint",
252+
fingerprint: "8e:b3:e2:ce:3c:6c:27:ff:51:c9:5d:77:af:92:2f:d8",
236253
headers: map[string]string{
237254
"Content-Type": "application/json",
238255
"X-Tenant-ID": "00000000-0000-4000-0000-000000000000",
@@ -241,7 +258,7 @@ func TestDeletePublicKey(t *testing.T) {
241258
},
242259
requiredMocks: func() {
243260
svcMock.
244-
On("DeletePublicKey", gomock.Anything, "fingerprint", "00000000-0000-4000-0000-000000000000").
261+
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").
245262
Return(nil).
246263
Once()
247264
},

0 commit comments

Comments
 (0)