Compare commits

..

No commits in common. "e7bdabb667dafcdb103d0b4606ed616f1799e1b4" and "15963d39eff2f6045b14db2a99e4e93f071631a3" have entirely different histories.

9 changed files with 69 additions and 128 deletions

View File

@ -200,19 +200,15 @@ func handleCloudRegister(c *gin.Context) {
sessionID, _ := c.Cookie("sessionId") sessionID, _ := c.Cookie("sessionId")
authToken, _ := c.Cookie("authToken") authToken, _ := c.Cookie("authToken")
// Require authentication for this endpoint if sessionID != "" && authToken != "" && authToken == config.LocalAuthToken {
if authToken == "" || authToken != config.LocalAuthToken {
c.JSON(401, gin.H{"error": "Authentication required"})
return
}
// Check session permissions if session exists
if sessionID != "" {
session := sessionManager.GetSession(sessionID) session := sessionManager.GetSession(sessionID)
if session != nil && !session.HasPermission(PermissionSettingsWrite) { if session != nil && !session.HasPermission(PermissionSettingsWrite) {
c.JSON(403, gin.H{"error": "Permission denied: settings modify permission required"}) c.JSON(403, gin.H{"error": "Permission denied: settings modify permission required"})
return return
} }
} else if sessionID != "" {
c.JSON(401, gin.H{"error": "Authentication required"})
return
} }
var req CloudRegisterRequest var req CloudRegisterRequest

View File

@ -12,7 +12,6 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
@ -133,14 +132,8 @@ func writeJSONRPCEvent(event string, params any, session *Session) {
err = session.RPCChannel.SendText(requestString) err = session.RPCChannel.SendText(requestString)
if err != nil { if err != nil {
// Check if it's a closed/closing error (expected during reconnection) // Only log at debug level - closed pipe errors are expected during reconnection
errStr := err.Error() scopedLogger.Debug().Err(err).Str("event", event).Msg("Could not send JSONRPC event (channel may be closing)")
if strings.Contains(errStr, "closed") || strings.Contains(errStr, "closing") {
scopedLogger.Debug().Err(err).Str("event", event).Msg("Could not send JSONRPC event (channel closing)")
} else {
// Other errors (buffer full, protocol errors) should be visible
scopedLogger.Warn().Err(err).Str("event", event).Msg("Failed to send JSONRPC event")
}
return return
} }
} }

View File

@ -95,11 +95,42 @@ func handleRequestSessionApprovalRPC(session *Session) (any, error) {
return map[string]interface{}{"status": "requested"}, nil return map[string]interface{}{"status": "requested"}, nil
} }
func validateNickname(nickname string) error {
if len(nickname) < minNicknameLength {
return fmt.Errorf("nickname must be at least %d characters", minNicknameLength)
}
if len(nickname) > maxNicknameLength {
return fmt.Errorf("nickname must be %d characters or less", maxNicknameLength)
}
if !isValidNickname(nickname) {
return errors.New("nickname can only contain letters, numbers, spaces, and - _ . @")
}
for i, r := range nickname {
if r < 32 || r == 127 {
return fmt.Errorf("nickname contains control character at position %d", i)
}
if r >= 0x200B && r <= 0x200D {
return errors.New("nickname contains zero-width character")
}
}
trimmed := ""
for _, r := range nickname {
trimmed += string(r)
}
if trimmed != nickname {
return errors.New("nickname contains disallowed unicode")
}
return nil
}
func handleUpdateSessionNicknameRPC(params map[string]any, session *Session) (any, error) { func handleUpdateSessionNicknameRPC(params map[string]any, session *Session) (any, error) {
sessionID, _ := params["sessionId"].(string) sessionID, _ := params["sessionId"].(string)
nickname, _ := params["nickname"].(string) nickname, _ := params["nickname"].(string)
if err := sessionManager.validateNickname(nickname); err != nil { if err := validateNickname(nickname); err != nil {
return nil, err return nil, err
} }

View File

@ -2,8 +2,6 @@ package kvm
import ( import (
"time" "time"
"github.com/pion/webrtc/v4"
) )
// emergencyPromotionContext holds context for emergency promotion attempts // emergencyPromotionContext holds context for emergency promotion attempts
@ -218,31 +216,20 @@ func (sm *SessionManager) promoteAfterGraceExpiration(expiredSessionID string, n
} }
} }
// handlePendingSessionTimeout removes timed-out pending sessions only if disconnected // handlePendingSessionTimeout removes timed-out pending sessions (DoS protection)
// Connected pending sessions remain visible for approval (consistent UX) // Returns true if any pending session was removed
// This prevents resource leaks while maintaining good user experience
func (sm *SessionManager) handlePendingSessionTimeout(now time.Time) bool { func (sm *SessionManager) handlePendingSessionTimeout(now time.Time) bool {
toDelete := make([]string, 0) toDelete := make([]string, 0)
for id, session := range sm.sessions { for id, session := range sm.sessions {
if session.Mode == SessionModePending && if session.Mode == SessionModePending &&
now.Sub(session.CreatedAt) > defaultPendingSessionTimeout { now.Sub(session.CreatedAt) > defaultPendingSessionTimeout {
// Only remove if the connection is closed/failed
// This prevents resource leaks while keeping connected sessions visible
if session.peerConnection != nil {
connectionState := session.peerConnection.ConnectionState()
if connectionState == webrtc.PeerConnectionStateClosed ||
connectionState == webrtc.PeerConnectionStateFailed ||
connectionState == webrtc.PeerConnectionStateDisconnected {
websocketLogger.Debug(). websocketLogger.Debug().
Str("sessionId", id). Str("sessionId", id).
Dur("age", now.Sub(session.CreatedAt)). Dur("age", now.Sub(session.CreatedAt)).
Str("connectionState", connectionState.String()). Msg("Removing timed-out pending session")
Msg("Removing timed-out disconnected pending session")
toDelete = append(toDelete, id) toDelete = append(toDelete, id)
} }
} }
}
}
for _, id := range toDelete { for _, id := range toDelete {
delete(sm.sessions, id) delete(sm.sessions, id)
} }

View File

@ -173,8 +173,11 @@ func (sm *SessionManager) AddSession(session *Session, clientSettings *SessionSe
} }
if session.Nickname != "" { if session.Nickname != "" {
if err := sm.validateNickname(session.Nickname); err != nil { if len(session.Nickname) < minNicknameLength {
return err return fmt.Errorf("nickname must be at least %d characters", minNicknameLength)
}
if len(session.Nickname) > maxNicknameLength {
return fmt.Errorf("nickname must be %d characters or less", maxNicknameLength)
} }
} }
if len(session.Identity) > maxIdentityLength { if len(session.Identity) > maxIdentityLength {
@ -248,13 +251,8 @@ func (sm *SessionManager) AddSession(session *Session, clientSettings *SessionSe
if existing.Mode == SessionModePrimary { if existing.Mode == SessionModePrimary {
isBlacklisted := sm.isSessionBlacklisted(session.ID) isBlacklisted := sm.isSessionBlacklisted(session.ID)
// SECURITY: Prevent dual-primary - check actual mode, not just existence // SECURITY: Prevent dual-primary - only restore if no other primary exists
primaryExists := false primaryExists := sm.primarySessionID != "" && sm.sessions[sm.primarySessionID] != nil
if sm.primarySessionID != "" {
if existingPrimary, ok := sm.sessions[sm.primarySessionID]; ok && existingPrimary.Mode == SessionModePrimary {
primaryExists = true
}
}
if sm.lastPrimaryID == session.ID && !isBlacklisted && !primaryExists { if sm.lastPrimaryID == session.ID && !isBlacklisted && !primaryExists {
sm.primarySessionID = session.ID sm.primarySessionID = session.ID
sm.lastPrimaryID = "" sm.lastPrimaryID = ""
@ -426,8 +424,8 @@ func (sm *SessionManager) RemoveSession(sessionID string) {
// Only add grace period if this is NOT an intentional logout // Only add grace period if this is NOT an intentional logout
if !isIntentionalLogout { if !isIntentionalLogout {
// Limit grace period entries to prevent memory exhaustion // Limit grace period entries to prevent memory exhaustion
// Evict entries ONLY when full, and only evict one entry // Evict the entry that will expire soonest (oldest expiration time)
if len(sm.reconnectGrace) >= maxGracePeriodEntries { for len(sm.reconnectGrace) >= maxGracePeriodEntries {
var evictID string var evictID string
var earliestExpiration time.Time var earliestExpiration time.Time
for id, graceTime := range sm.reconnectGrace { for id, graceTime := range sm.reconnectGrace {
@ -440,15 +438,8 @@ func (sm *SessionManager) RemoveSession(sessionID string) {
if evictID != "" { if evictID != "" {
delete(sm.reconnectGrace, evictID) delete(sm.reconnectGrace, evictID)
delete(sm.reconnectInfo, evictID) delete(sm.reconnectInfo, evictID)
sm.logger.Debug().
Str("evictedSessionID", evictID).
Msg("Evicted oldest grace period entry due to limit")
} else { } else {
// Defensive: if we couldn't evict, don't add grace period break
sm.logger.Error().
Int("graceCount", len(sm.reconnectGrace)).
Msg("Failed to evict grace period entry, skipping grace period for this session")
goto skipGracePeriod
} }
} }
@ -465,8 +456,6 @@ func (sm *SessionManager) RemoveSession(sessionID string) {
} }
} }
skipGracePeriod:
// If this was the primary session, clear primary slot and track for grace period // If this was the primary session, clear primary slot and track for grace period
if wasPrimary { if wasPrimary {
if isIntentionalLogout { if isIntentionalLogout {
@ -1220,9 +1209,11 @@ func (sm *SessionManager) transferPrimaryRole(fromSessionID, toSessionID, transf
return fmt.Errorf("cannot promote: another primary session exists (%s)", existingPrimaryID) return fmt.Errorf("cannot promote: another primary session exists (%s)", existingPrimaryID)
} }
// Promote target session
toSession.Mode = SessionModePrimary toSession.Mode = SessionModePrimary
toSession.hidRPCAvailable = false toSession.hidRPCAvailable = false
if transferType == "emergency_timeout_promotion" { // Reset LastActive for all emergency promotions to prevent immediate re-timeout
if strings.HasPrefix(transferType, "emergency_") {
toSession.LastActive = time.Now() toSession.LastActive = time.Now()
} }
sm.primarySessionID = toSessionID sm.primarySessionID = toSessionID
@ -1471,19 +1462,16 @@ func extractBrowserFromUserAgent(userAgent string) *string {
ua := strings.ToLower(userAgent) ua := strings.ToLower(userAgent)
// Check for common browsers (order matters - Chrome contains Safari, etc.) // Check for common browsers (order matters - Chrome contains Safari, etc.)
// Optimize Safari check by caching Chrome detection
hasChrome := strings.Contains(ua, "chrome")
if strings.Contains(ua, "edg/") || strings.Contains(ua, "edge") { if strings.Contains(ua, "edg/") || strings.Contains(ua, "edge") {
return &BrowserEdge return &BrowserEdge
} }
if strings.Contains(ua, "firefox") { if strings.Contains(ua, "firefox") {
return &BrowserFirefox return &BrowserFirefox
} }
if hasChrome { if strings.Contains(ua, "chrome") {
return &BrowserChrome return &BrowserChrome
} }
if strings.Contains(ua, "safari") { if strings.Contains(ua, "safari") && !strings.Contains(ua, "chrome") {
return &BrowserSafari return &BrowserSafari
} }
if strings.Contains(ua, "opera") || strings.Contains(ua, "opr/") { if strings.Contains(ua, "opera") || strings.Contains(ua, "opr/") {
@ -1526,37 +1514,6 @@ func generateNicknameFromUserAgent(userAgent string) string {
} }
// ensureNickname ensures session has a nickname, auto-generating if needed // ensureNickname ensures session has a nickname, auto-generating if needed
func (sm *SessionManager) validateNickname(nickname string) error {
if len(nickname) < minNicknameLength {
return fmt.Errorf("nickname must be at least %d characters", minNicknameLength)
}
if len(nickname) > maxNicknameLength {
return fmt.Errorf("nickname must be %d characters or less", maxNicknameLength)
}
if !isValidNickname(nickname) {
return errors.New("nickname can only contain letters, numbers, spaces, and - _ . @")
}
for i, r := range nickname {
if r < 32 || r == 127 {
return fmt.Errorf("nickname contains control character at position %d", i)
}
if r >= 0x200B && r <= 0x200D {
return errors.New("nickname contains zero-width character")
}
}
trimmed := ""
for _, r := range nickname {
trimmed += string(r)
}
if trimmed != nickname {
return errors.New("nickname contains disallowed unicode")
}
return nil
}
func (sm *SessionManager) ensureNickname(session *Session) { func (sm *SessionManager) ensureNickname(session *Session) {
// Skip if session already has a nickname // Skip if session already has a nickname
if session.Nickname != "" { if session.Nickname != "" {

View File

@ -1,4 +1,4 @@
import { useEffect, useState, useCallback, useRef } from "react"; import { useEffect, useState, useCallback } from "react";
import { useNavigate } from "react-router"; import { useNavigate } from "react-router";
import { XCircleIcon } from "@heroicons/react/24/outline"; import { XCircleIcon } from "@heroicons/react/24/outline";
@ -30,7 +30,6 @@ export default function AccessDeniedOverlay({
const { maxRejectionAttempts } = useSettingsStore(); const { maxRejectionAttempts } = useSettingsStore();
const [countdown, setCountdown] = useState(10); const [countdown, setCountdown] = useState(10);
const [isRetrying, setIsRetrying] = useState(false); const [isRetrying, setIsRetrying] = useState(false);
const hasCountedRef = useRef(false);
const handleLogout = useCallback(async () => { const handleLogout = useCallback(async () => {
try { try {
@ -51,15 +50,7 @@ export default function AccessDeniedOverlay({
}, [navigate, setUser, clearSession, clearNickname]); }, [navigate, setUser, clearSession, clearNickname]);
useEffect(() => { useEffect(() => {
if (!show) { if (!show) return;
hasCountedRef.current = false;
setCountdown(10);
return;
}
// Only count rejection once per showing
if (hasCountedRef.current) return;
hasCountedRef.current = true;
const newCount = incrementRejectionCount(); const newCount = incrementRejectionCount();

View File

@ -31,16 +31,9 @@ export default function InfoBar() {
useEffect(() => { useEffect(() => {
if (!rpcDataChannel) return; if (!rpcDataChannel) return;
rpcDataChannel.onclose = () => { rpcDataChannel.onclose = () => console.log("rpcDataChannel has closed");
if (rpcDataChannel.readyState === "closed") { rpcDataChannel.onerror = (e: Event) =>
console.debug("rpcDataChannel closed"); console.error(`Error on DataChannel '${rpcDataChannel.label}': ${e}`);
}
};
rpcDataChannel.onerror = (e: Event) => {
if (rpcDataChannel.readyState === "open" || rpcDataChannel.readyState === "connecting") {
console.error(`Error on DataChannel '${rpcDataChannel.label}':`, e);
}
};
}, [rpcDataChannel]); }, [rpcDataChannel]);
const { keyboardLedState, usbState } = useHidStore(); const { keyboardLedState, usbState } = useHidStore();

View File

@ -29,10 +29,7 @@ export function useVersion() {
return new Promise<SystemVersionInfo>((resolve, reject) => { return new Promise<SystemVersionInfo>((resolve, reject) => {
send("getUpdateStatus", {}, (resp: JsonRpcResponse) => { send("getUpdateStatus", {}, (resp: JsonRpcResponse) => {
if ("error" in resp) { if ("error" in resp) {
const errorMsg = typeof resp.error === 'object' && resp.error.message notifications.error(`Failed to check for updates: ${resp.error}`);
? resp.error.message
: String(resp.error);
notifications.error(`Failed to check for updates: ${errorMsg}`);
reject(new Error("Failed to check for updates")); reject(new Error("Failed to check for updates"));
} else { } else {
const result = resp.result as SystemVersionInfo; const result = resp.result as SystemVersionInfo;
@ -59,11 +56,8 @@ export function useVersion() {
console.warn("Failed to get device version, using legacy version"); console.warn("Failed to get device version, using legacy version");
return getVersionInfo().then(result => resolve(result.local)).catch(reject); return getVersionInfo().then(result => resolve(result.local)).catch(reject);
} }
console.error("Failed to get device version:", resp.error); console.error("Failed to get device version N", resp.error);
const errorMsg = typeof resp.error === 'object' && resp.error.message notifications.error(`Failed to get device version: ${resp.error}`);
? resp.error.message
: String(resp.error);
notifications.error(`Failed to get device version: ${errorMsg}`);
reject(new Error("Failed to get device version")); reject(new Error("Failed to get device version"));
} else { } else {
const result = resp.result as VersionInfo; const result = resp.result as VersionInfo;

View File

@ -965,11 +965,10 @@ export default function KvmIdRoute() {
useEffect(() => { useEffect(() => {
if (appVersion) return; if (appVersion) return;
if (rpcDataChannel?.readyState !== "open") return; if (rpcDataChannel?.readyState !== "open") return;
if (currentMode === "pending") return;
getLocalVersion(); getLocalVersion();
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [appVersion, rpcDataChannel?.readyState, currentMode]); }, [appVersion, rpcDataChannel?.readyState]);
const ConnectionStatusElement = useMemo(() => { const ConnectionStatusElement = useMemo(() => {
const isOtherSession = location.pathname.includes("other-session"); const isOtherSession = location.pathname.includes("other-session");