Skip to content

Commit caaea3b

Browse files
committed
Fixes #148
1 parent 626f85b commit caaea3b

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

.github/workflows/go_dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will build a golang project
22
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
33

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

66
on:
77
push:

.github/workflows/go_main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will build a golang project
22
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
33

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

66
on:
77
push:

docs/CHANGELOG.MD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## 2.1.0 - 2023-12-14
8+
9+
### Fixed
10+
11+
- [Issue 148](https://github.com/Ne0nd0g/merlin/issues/148) - Validate & encode gRPC messages to UTF-8
12+
713
## 2.0.0 - 2023-11-05
814

915
### Added

pkg/merlin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ along with Merlin. If not, see <http://www.gnu.org/licenses/>.
2121
package merlin
2222

2323
// Version is a constant variable containing the version number for the Merlin package
24-
const Version = "2.0.0"
24+
const Version = "2.1.0"
2525

2626
// Build is a hash off the git commit and is stamped it at compile time
2727
var Build = "nonRelease"

pkg/services/rpc/rpc.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"os"
3838
"strings"
3939
"time"
40+
"unicode/utf8"
4041

4142
// 3rd Party
4243
"github.com/google/uuid"
@@ -367,7 +368,7 @@ func (s *Service) SendClientMessage(msg *message.Message) {
367368
func NewPBErrorMessage(err error) *pb.Message {
368369
return &pb.Message{
369370
Level: pb.MessageLevel_WARN,
370-
Message: err.Error(),
371+
Message: validUTF8(err.Error()),
371372
Timestamp: time.Now().UTC().Format(time.RFC3339),
372373
Error: true,
373374
}
@@ -377,7 +378,7 @@ func NewPBErrorMessage(err error) *pb.Message {
377378
func NewPBSuccessMessage(msg string) *pb.Message {
378379
return &pb.Message{
379380
Level: pb.MessageLevel_SUCCESS,
380-
Message: msg,
381+
Message: validUTF8(msg),
381382
Timestamp: time.Now().UTC().Format(time.RFC3339),
382383
}
383384
}
@@ -386,7 +387,7 @@ func NewPBSuccessMessage(msg string) *pb.Message {
386387
func NewPBNoteMessage(msg string) *pb.Message {
387388
return &pb.Message{
388389
Level: pb.MessageLevel_NOTE,
389-
Message: msg,
390+
Message: validUTF8(msg),
390391
Timestamp: time.Now().UTC().Format(time.RFC3339),
391392
}
392393
}
@@ -395,7 +396,7 @@ func NewPBNoteMessage(msg string) *pb.Message {
395396
func NewPBInfoMessage(msg string) *pb.Message {
396397
return &pb.Message{
397398
Level: pb.MessageLevel_INFO,
398-
Message: msg,
399+
Message: validUTF8(msg),
399400
Timestamp: time.Now().UTC().Format(time.RFC3339),
400401
}
401402
}
@@ -404,7 +405,7 @@ func NewPBInfoMessage(msg string) *pb.Message {
404405
func NewPBPlainMessage(msg string) *pb.Message {
405406
return &pb.Message{
406407
Level: pb.MessageLevel_PLAIN,
407-
Message: msg,
408+
Message: validUTF8(msg),
408409
Timestamp: time.Now().UTC().Format(time.RFC3339),
409410
}
410411
}
@@ -413,7 +414,7 @@ func NewPBPlainMessage(msg string) *pb.Message {
413414
func NewPBWarnMessage(msg string) *pb.Message {
414415
return &pb.Message{
415416
Level: pb.MessageLevel_WARN,
416-
Message: msg,
417+
Message: validUTF8(msg),
417418
Timestamp: time.Now().UTC().Format(time.RFC3339),
418419
}
419420
}
@@ -439,12 +440,25 @@ func NewPBMessageFromMessage(msg *message.Message) *pb.Message {
439440
}
440441
return &pb.Message{
441442
Level: level,
442-
Message: msg.Message(),
443+
Message: validUTF8(msg.Message()),
443444
Timestamp: msg.Time().UTC().Format(time.RFC3339),
444445
Error: msg.Error(),
445446
}
446447
}
447448

449+
// validUTF8 ensures the string contains valid UTF-8 and replaces invalid characters with the '�' character
450+
// gRPC messages must be valid UTF-8
451+
func validUTF8(s string) string {
452+
// Ensure the message is a valid UTF-8 string
453+
if utf8.ValidString(s) {
454+
return s
455+
}
456+
return fmt.Sprintf(
457+
"\n*** The message contained invalid UTF-8 that was replaced with the '�' character ***\n\n%s",
458+
strings.ToValidUTF8(s, "�"),
459+
)
460+
}
461+
448462
// getTLSConfig creates a new TLS configuration for the RPC service
449463
func getTLSConfig(secure bool, tlsKey, tlsCert, tlsCA string) (*tls.Config, error) {
450464
slog.Debug("entering into function", "secure", secure, "tlsKey", tlsKey, "tlsCert", tlsCert)

0 commit comments

Comments
 (0)