feat(config,script): validate UTF-8 config and hex encryption keys

This commit is contained in:
zarazaex69
2026-05-17 22:20:14 +03:00
parent bbcf8f6ed1
commit 4adea8824f
4 changed files with 48 additions and 1 deletions

View File

@@ -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)

View File

@@ -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)
}
}