mirror of
https://github.com/openlibrecommunity/olcrtc.git
synced 2026-05-26 07:08:11 +00:00
feat(config,script): validate UTF-8 config and hex encryption keys
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/openlibrecommunity/olcrtc/internal/app/session"
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -176,6 +177,9 @@ func Load(path string) (File, error) {
|
||||
}
|
||||
return File{}, fmt.Errorf("read config %s: %w", path, err)
|
||||
}
|
||||
if !utf8.Valid(data) {
|
||||
return File{}, fmt.Errorf("parse config %s: file is not valid UTF-8", path)
|
||||
}
|
||||
var f File
|
||||
if err := yaml.Unmarshal(data, &f); err != nil {
|
||||
return File{}, fmt.Errorf("parse config %s: %w", path, err)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/openlibrecommunity/olcrtc/internal/app/session"
|
||||
@@ -320,3 +321,15 @@ func TestLoadMissing(t *testing.T) {
|
||||
t.Fatal("expected error for missing file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadInvalidUTF8(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "olcrtc.yaml")
|
||||
if err := os.WriteFile(path, []byte{'m', 'o', 'd', 'e', ':', ' ', 0xff}, 0o600); err != nil {
|
||||
t.Fatalf("write config: %v", err)
|
||||
}
|
||||
|
||||
_, err := Load(path)
|
||||
if err == nil || !strings.Contains(err.Error(), "file is not valid UTF-8") {
|
||||
t.Fatalf("Load() error = %v, want invalid UTF-8 error", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user