Skip to content

Commit

Permalink
Fixes #148
Browse files Browse the repository at this point in the history
  • Loading branch information
Ne0nd0g committed Dec 14, 2023
1 parent 626f85b commit caaea3b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go_dev.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: "Merlin Server - Scan, Build, & Test: Development Work"
name: "Scan, Build, & Test: Development Work"

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go_main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: "Merlin Server - Scan, Build, & Test: Main"
name: "Scan, Build, & Test: Main"

on:
push:
Expand Down
6 changes: 6 additions & 0 deletions docs/CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 2.1.0 - 2023-12-14

### Fixed

- [Issue 148](https://github.com/Ne0nd0g/merlin/issues/148) - Validate & encode gRPC messages to UTF-8

## 2.0.0 - 2023-11-05

### Added
Expand Down
2 changes: 1 addition & 1 deletion pkg/merlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ along with Merlin. If not, see <http://www.gnu.org/licenses/>.
package merlin

// Version is a constant variable containing the version number for the Merlin package
const Version = "2.0.0"
const Version = "2.1.0"

// Build is a hash off the git commit and is stamped it at compile time
var Build = "nonRelease"
28 changes: 21 additions & 7 deletions pkg/services/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"os"
"strings"
"time"
"unicode/utf8"

// 3rd Party
"github.com/google/uuid"
Expand Down Expand Up @@ -367,7 +368,7 @@ func (s *Service) SendClientMessage(msg *message.Message) {
func NewPBErrorMessage(err error) *pb.Message {
return &pb.Message{
Level: pb.MessageLevel_WARN,
Message: err.Error(),
Message: validUTF8(err.Error()),
Timestamp: time.Now().UTC().Format(time.RFC3339),
Error: true,
}
Expand All @@ -377,7 +378,7 @@ func NewPBErrorMessage(err error) *pb.Message {
func NewPBSuccessMessage(msg string) *pb.Message {
return &pb.Message{
Level: pb.MessageLevel_SUCCESS,
Message: msg,
Message: validUTF8(msg),
Timestamp: time.Now().UTC().Format(time.RFC3339),
}
}
Expand All @@ -386,7 +387,7 @@ func NewPBSuccessMessage(msg string) *pb.Message {
func NewPBNoteMessage(msg string) *pb.Message {
return &pb.Message{
Level: pb.MessageLevel_NOTE,
Message: msg,
Message: validUTF8(msg),
Timestamp: time.Now().UTC().Format(time.RFC3339),
}
}
Expand All @@ -395,7 +396,7 @@ func NewPBNoteMessage(msg string) *pb.Message {
func NewPBInfoMessage(msg string) *pb.Message {
return &pb.Message{
Level: pb.MessageLevel_INFO,
Message: msg,
Message: validUTF8(msg),
Timestamp: time.Now().UTC().Format(time.RFC3339),
}
}
Expand All @@ -404,7 +405,7 @@ func NewPBInfoMessage(msg string) *pb.Message {
func NewPBPlainMessage(msg string) *pb.Message {
return &pb.Message{
Level: pb.MessageLevel_PLAIN,
Message: msg,
Message: validUTF8(msg),
Timestamp: time.Now().UTC().Format(time.RFC3339),
}
}
Expand All @@ -413,7 +414,7 @@ func NewPBPlainMessage(msg string) *pb.Message {
func NewPBWarnMessage(msg string) *pb.Message {
return &pb.Message{
Level: pb.MessageLevel_WARN,
Message: msg,
Message: validUTF8(msg),
Timestamp: time.Now().UTC().Format(time.RFC3339),
}
}
Expand All @@ -439,12 +440,25 @@ func NewPBMessageFromMessage(msg *message.Message) *pb.Message {
}
return &pb.Message{
Level: level,
Message: msg.Message(),
Message: validUTF8(msg.Message()),
Timestamp: msg.Time().UTC().Format(time.RFC3339),
Error: msg.Error(),
}
}

// validUTF8 ensures the string contains valid UTF-8 and replaces invalid characters with the '�' character
// gRPC messages must be valid UTF-8
func validUTF8(s string) string {
// Ensure the message is a valid UTF-8 string
if utf8.ValidString(s) {
return s
}
return fmt.Sprintf(
"\n*** The message contained invalid UTF-8 that was replaced with the '�' character ***\n\n%s",
strings.ToValidUTF8(s, "�"),
)
}

// getTLSConfig creates a new TLS configuration for the RPC service
func getTLSConfig(secure bool, tlsKey, tlsCert, tlsCA string) (*tls.Config, error) {
slog.Debug("entering into function", "secure", secure, "tlsKey", tlsKey, "tlsCert", tlsCert)
Expand Down

0 comments on commit caaea3b

Please sign in to comment.