Repository navigation
cmux-tui SDKs: align session socket validation #10254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
5c68a63
03c47e6
83ad35b
5ad19db
decf539
e09d4e9
4a20172
84b58db
cfb148e
f1150fe
7f94fc3
cbeb42e
ba4f525
44a26cc
e9c177d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package sessionpath | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "errors" | ||
| "unicode" | ||
| "unicode/utf8" | ||
| ) | ||
|
|
||
| // ErrInvalid identifies session text that cannot be one socket path component. | ||
| var ErrInvalid = errors.New( | ||
| "session name must be a non-empty path component without separators or control characters", | ||
| ) | ||
|
|
||
| // Validate applies the cmux-tui server's relaxed session-name contract. | ||
| func Validate(session string) error { | ||
| if session == "" || session == "." || session == ".." || !utf8.ValidString(session) { | ||
| return ErrInvalid | ||
| } | ||
| for _, character := range session { | ||
| if character == '/' || character == '\\' || character == '\x00' || | ||
| unicode.IsControl(character) || | ||
| character == '\u0085' || character == '\u2028' || character == '\u2029' { | ||
| return ErrInvalid | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Digest returns a stable, collision-resistant leaf for compatibility path | ||
| // queries that cannot return an error. It must not be used to open a socket | ||
| // for an invalid session; connector paths use Validate first. | ||
| func Digest(session string) string { | ||
| digest := sha256.Sum256([]byte(session)) | ||
| return hex.EncodeToString(digest[:]) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package cmux | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/manaflow-ai/cmux/cmux-tui/bindings/go/internal/sessionpath" | ||
| ) | ||
|
|
||
| func TestHighLevelValidationPreservesCause(t *testing.T) { | ||
| t.Setenv("CMUX_TUI_SOCKET", "") | ||
| t.Setenv("CMUX_MUX_SOCKET", "") | ||
| _, err := resolveSocketPath("", string([]byte{'b', 0xff, 'd'})) | ||
| if !errors.Is(err, ErrInvalidArgument) || !errors.Is(err, sessionpath.ErrInvalid) { | ||
| t.Fatalf("error = %v, want invalid argument and sessionpath cause", err) | ||
| } | ||
| } | ||
|
|
||
| func TestHighLevelClientRejectsUnsafeSessionBeforeDial(t *testing.T) { | ||
| t.Setenv("CMUX_TUI_SOCKET", "") | ||
| t.Setenv("CMUX_MUX_SOCKET", "") | ||
| if _, err := resolveSocketPath("", ""); !errors.Is(err, ErrInvalidArgument) { | ||
| t.Fatalf("empty derived session error = %v, want invalid argument", err) | ||
| } | ||
| for _, session := range []string{ | ||
| ".", | ||
| "..", | ||
| "../escape", | ||
| "nested/session", | ||
| "nested\\session", | ||
| "bad\x00name", | ||
| "bad\nname", | ||
| "bad\u0085name", | ||
| "bad\u2028name", | ||
| "bad\u2029name", | ||
| } { | ||
| called := false | ||
| _, err := NewClient(context.Background(), ClientOptions{ | ||
| Session: session, | ||
| DialContext: func(context.Context, string, string) (net.Conn, error) { | ||
| called = true | ||
| return nil, errors.New("dial must not run") | ||
| }, | ||
| }) | ||
| if !errors.Is(err, ErrInvalidArgument) { | ||
| t.Errorf("unsafe session %q error = %v, want invalid argument", session, err) | ||
| } | ||
| if called { | ||
| t.Errorf("unsafe session %q reached the dialer", session) | ||
| } | ||
| } | ||
|
|
||
| if path, err := resolveSocketPath("/tmp/explicit.sock", "../escape"); err != nil || | ||
| path != "/tmp/explicit.sock" { | ||
| t.Fatalf("explicit path with unsafe session = %q, %v", path, err) | ||
| } | ||
| t.Setenv("CMUX_TUI_SOCKET", "/tmp/inherited.sock") | ||
| if path, err := resolveSocketPath("", "../escape"); err != nil || | ||
| path != "/tmp/inherited.sock" { | ||
| t.Fatalf("inherited path with unsafe session = %q, %v", path, err) | ||
| } | ||
| } | ||
|
|
||
| func TestHighLevelInvalidCompatibilityPathIsDeterministicAndIsolated(t *testing.T) { | ||
| t.Setenv("CMUX_TUI_SOCKET", "") | ||
| t.Setenv("CMUX_MUX_SOCKET", "") | ||
| t.Setenv("XDG_RUNTIME_DIR", "/run/user-test") | ||
| first := defaultSocketPath("../escape") | ||
| second := defaultSocketPath("../escape") | ||
| if first != second { | ||
| t.Fatalf("invalid compatibility paths differ: %q != %q", first, second) | ||
| } | ||
| if (!strings.HasPrefix(first, "/run/user-test/cmux-tui-invalid-") && | ||
| !strings.HasPrefix(first, "/tmp/cmux-tui-invalid-")) || | ||
| !strings.HasSuffix(first, ".sock") || strings.Contains(first, "escape") { | ||
| t.Fatalf("invalid compatibility path is not an isolated digest leaf: %q", first) | ||
| } | ||
| } | ||
|
|
||
| func TestHighLevelSocketPathPreservesLegacySafeNames(t *testing.T) { | ||
| t.Setenv("CMUX_TUI_SOCKET", "") | ||
| t.Setenv("CMUX_MUX_SOCKET", "") | ||
| t.Setenv("XDG_RUNTIME_DIR", "/run/user-test") | ||
| for _, session := range []string{ | ||
| "contains space", | ||
| "名前", | ||
| "_leading", | ||
| "-leading", | ||
| ".leading", | ||
| "legacy:colon", | ||
| "legacy-" + strings.Repeat("x", 200), | ||
| } { | ||
| path, err := resolveSocketPath("", session) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win Use a defined socket-path helper. Line 41 calls 🧰 Tools🪛 golangci-lint (2.12.2)[error] 41-41: undefined: resolveSocketPath (typecheck) 🤖 Prompt for AI AgentsSource: Linters/SAST tools |
||
| if err != nil { | ||
| t.Fatalf("session %q rejected: %v", session, err) | ||
| } | ||
| if !strings.HasSuffix(path, "/"+session+".sock") { | ||
| t.Fatalf("session %q path = %q, want suffix %q", session, path, "/"+session+".sock") | ||
| } | ||
|
Comment on lines
+67
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Make these path-shape tests platform-specific. This file runs on Windows. On Windows, Move Unix path-shape assertions to a Unix-specific test file. Keep platform-neutral assertions for determinism, isolation, and validation here. 🧰 Tools🪛 ast-grep (0.45.1)[warning] 100-100: A log/format call (log.Print/Printf/Println, the Fatal/Panic variants, fmt.Sprintf, or a structured logger's Info/Warn/Error/Debug method) is given a message built by concatenating a string literal with a non-literal value such as request data. Unsanitized, attacker-controlled input written to logs enables log forging / CRLF injection: an attacker can inject newlines to spoof log entries or break log parsers. Do not concatenate raw input into the log message; pass it as a separate structured field/argument (e.g. 'log.Printf("user: %s", user)' or 'logger.Info("login", "user", user)') and strip or escape newline characters first. (log-injection-request-data-concat-go) 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.