Skip to content

Commit

Permalink
[Gateway] Add extra checks (#10788)
Browse files Browse the repository at this point in the history
  • Loading branch information
bolekk authored Sep 26, 2023
1 parent b0940c0 commit b63399b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/services/gateway/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func StringToAlignedBytes(input string, size int) []byte {

func AlignedBytesToString(data []byte) string {
idx := slices.IndexFunc(data, func(b byte) bool { return b == 0 })
if idx == -1 {
return string(data)
}
return string(data[:idx])
}

Expand Down
4 changes: 4 additions & 0 deletions core/services/gateway/common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func TestUtils_StringAlignedBytesConversions(t *testing.T) {
data := common.StringToAlignedBytes(val, 40)
require.Equal(t, val, common.AlignedBytesToString(data))

val = "0123456789"
data = common.StringToAlignedBytes(val, 10)
require.Equal(t, val, common.AlignedBytesToString(data))

val = "世界"
data = common.StringToAlignedBytes(val, 40)
require.Equal(t, val, common.AlignedBytesToString(data))
Expand Down
2 changes: 1 addition & 1 deletion core/services/gateway/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type gatewayState struct {
}

func NewGatewayConnector(config *ConnectorConfig, signer Signer, handler GatewayConnectorHandler, clock utils.Clock, lggr logger.Logger) (GatewayConnector, error) {
if signer == nil || handler == nil || clock == nil {
if config == nil || signer == nil || handler == nil || clock == nil || lggr == nil {
return nil, errors.New("nil dependency")
}
if len(config.DonId) == 0 || len(config.DonId) > int(network.HandshakeDonIdLen) {
Expand Down
3 changes: 3 additions & 0 deletions core/services/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ func (g *gateway) ProcessRequest(ctx context.Context, rawRequest []byte) (rawRes
if err != nil {
return newError(g.codec, "", api.UserMessageParseError, err.Error())
}
if msg == nil {
return newError(g.codec, "", api.UserMessageParseError, "nil message")
}
if err = msg.Validate(); err != nil {
return newError(g.codec, msg.Body.MessageId, api.UserMessageParseError, err.Error())
}
Expand Down
5 changes: 5 additions & 0 deletions core/services/gateway/network/wsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (c *webSocketClient) Connect(ctx context.Context, url *url.URL) (*websocket
}

challengeStr := resp.Header.Get(WsServerHandshakeChallengeHeaderName)
if challengeStr == "" {
c.lggr.Error("WebSocketClient: empty challenge")
c.tryCloseConn(conn)
return nil, err
}
challenge, err := base64.StdEncoding.DecodeString(challengeStr)
if err != nil {
c.lggr.Errorf("WebSocketClient: couldn't decode challenge: %s: %v", challengeStr, err)
Expand Down

0 comments on commit b63399b

Please sign in to comment.