Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion daemon/handlers.sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package daemon

import (
"context"
"encoding/json"
"log/slog"
"time"

Expand Down Expand Up @@ -74,7 +75,7 @@ func handlePubSubSync(ctx context.Context, socketMsgPayload interface{}) error {
slog.Error("Failed to encrypt key", slog.Any("err", err))
}

buf, err := msgpack.Marshal(payload)
buf, err := json.Marshal(payload)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While you've updated this Marshal call to use JSON, the function still uses msgpack at the beginning (lines 14-21) to process the socketMsgPayload. To complete the refactoring in this file and fully remove the msgpack dependency, that initial processing should also be updated to use json.Marshal and json.Unmarshal.


if err != nil {
slog.Error("Failed to marshal payload", slog.Any("err", err))
Expand Down
7 changes: 3 additions & 4 deletions model/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/sirupsen/logrus"
"github.com/vmihailenco/msgpack/v5"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

Expand Down Expand Up @@ -51,7 +50,7 @@ func (hs handshakeService) send(ctx context.Context, path string, jsonData []byt
return
}

req.Header.Set("Content-Type", "application/msgpack")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", fmt.Sprintf("shelltimeCLI@%s", commitID))
resp, err := hc.Do(req)
if err != nil {
Expand Down Expand Up @@ -103,7 +102,7 @@ func (hs handshakeService) Init(ctx context.Context) (string, error) {
OSVersion: sysInfo.Version,
}

jsonData, err := msgpack.Marshal(data)
jsonData, err := json.Marshal(data)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

While you've correctly switched to json.Marshal here, the send function (called on line 111) still hardcodes the Content-Type header as "application/msgpack". This will cause the server to reject the request because the payload is now JSON.

This needs to be updated to "application/json" in the send function.

Additionally, for code cleanup, the msgpack struct tags in handshakeInitRequest (lines 79-81) are now redundant and should be removed.

if err != nil {
logrus.Errorln(err)
return "", err
Expand All @@ -130,7 +129,7 @@ func (hs handshakeService) Check(ctx context.Context, handshakeId string) (token
EncodedID: handshakeId,
}

jsonData, err := msgpack.Marshal(data)
jsonData, err := json.Marshal(data)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Similar to the Init function, you've switched to json.Marshal here, but the send function (called on line 138) will send the request with an incorrect Content-Type of "application/msgpack". This should be "application/json".

Also, the msgpack tag in the handshakeCheckRequest struct (line 124) should be removed as it's no longer needed.

if err != nil {
logrus.Errorln(err)
return "", err
Expand Down
10 changes: 5 additions & 5 deletions model/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package model

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/vmihailenco/msgpack/v5"
)

type handshakeTestSuite struct {
Expand All @@ -21,12 +21,12 @@ func (s *handshakeTestSuite) TestHandshakeInitSuccess() {
// Verify request
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "/api/v1/handshake/init", r.URL.Path)
assert.Equal(t, "application/msgpack", r.Header.Get("Content-Type"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Contains(t, r.Header.Get("User-Agent"), "shelltimeCLI@")

// Decode request body
var payload handshakeInitRequest
err := msgpack.NewDecoder(r.Body).Decode(&payload)
err := json.NewDecoder(r.Body).Decode(&payload)
assert.NoError(t, err)

// Verify payload
Expand Down Expand Up @@ -74,11 +74,11 @@ func (s *handshakeTestSuite) TestHandshakeCheckWithToken() {
// Verify request
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "/api/v1/handshake/check", r.URL.Path)
assert.Equal(t, "application/msgpack", r.Header.Get("Content-Type"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))

// Decode request body
var payload handshakeCheckRequest
err := msgpack.NewDecoder(r.Body).Decode(&payload)
err := json.NewDecoder(r.Body).Decode(&payload)
assert.NoError(t, err)

// Verify payload
Expand Down
Loading