Skip to content

Commit e45544f

Browse files
Jérémy Christillinclaude
andcommitted
fix: preserve special characters in server passwords
Fixed critical bug where passwords containing quotes were corrupted during parsing. The `tr -d '"'` command was removing ALL quotes, including those within password values. Changes: - Rewrote get_server_config() to use IFS-based parsing instead of cut+tr - Only removes surrounding quotes, preserves quotes within values - Improved test_ssh_connection() with better error logging - Added SSH_MANAGER_DEBUG flag for troubleshooting connection issues - Increased connection timeout from 5s to 10s This fixes issues with passwords containing special characters like: \ [ ] " ( ) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 1ebcd1e commit e45544f

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

cli/lib/config.sh

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,29 @@ get_server_config() {
106106
local server="$1"
107107
local field="$2"
108108
local server_upper="$(echo "$server" | tr '[:lower:]' '[:upper:]')"
109-
109+
110110
if [ ! -f "$SSH_MANAGER_ENV" ]; then
111111
return 1
112112
fi
113-
113+
114114
local field_upper="$(echo "$field" | tr '[:lower:]' '[:upper:]')"
115115
local key="SSH_SERVER_${server_upper}_${field_upper}"
116-
grep "^${key}=" "$SSH_MANAGER_ENV" 2>/dev/null | cut -d'=' -f2- | tr -d '"'
116+
117+
# Read value using IFS to preserve all special characters including quotes
118+
local value=""
119+
while IFS="=" read -r k v; do
120+
if [[ "$k" == "$key" ]]; then
121+
value="$v"
122+
break
123+
fi
124+
done < <(grep "^${key}=" "$SSH_MANAGER_ENV" 2>/dev/null)
125+
126+
# Only remove surrounding quotes if they exist, preserve quotes within the value
127+
if [[ "$value" =~ ^\"(.*)\"$ ]]; then
128+
printf '%s' "${BASH_REMATCH[1]}"
129+
else
130+
printf '%s' "$value"
131+
fi
117132
}
118133

119134
# Add server to .env
@@ -188,39 +203,53 @@ test_ssh_connection() {
188203
local port=$(get_server_config "$server" "PORT")
189204
local keypath=$(get_server_config "$server" "KEYPATH")
190205
local password=$(get_server_config "$server" "PASSWORD")
191-
206+
192207
port=${port:-22}
193-
208+
194209
if [ -z "$host" ] || [ -z "$user" ]; then
195210
print_error "Server '$server' not found or incomplete configuration"
196211
return 1
197212
fi
198-
213+
199214
print_info "Testing connection to $server ($user@$host:$port)..."
200-
201-
local ssh_opts="-o ConnectTimeout=5 -o StrictHostKeyChecking=no"
202-
215+
216+
local ssh_opts="-o ConnectTimeout=10 -o StrictHostKeyChecking=no"
217+
local ssh_output=$(mktemp)
218+
local ssh_status
219+
203220
if [ -n "$keypath" ]; then
204221
ssh_opts="$ssh_opts -i $keypath"
205222
fi
206-
223+
207224
if [ -n "$password" ]; then
208225
# Use sshpass if available
209226
if command -v sshpass >/dev/null 2>&1; then
210-
sshpass -p "$password" ssh $ssh_opts -p "$port" "$user@$host" "echo 'Connection successful'" 2>/dev/null
227+
sshpass -p "$password" ssh $ssh_opts -p "$port" "$user@$host" "echo 'Connection successful'" > "$ssh_output" 2>&1
228+
ssh_status=$?
211229
else
212230
print_warning "sshpass not installed, cannot test password authentication"
231+
rm -f "$ssh_output"
213232
return 1
214233
fi
215234
else
216-
ssh $ssh_opts -p "$port" "$user@$host" "echo 'Connection successful'" 2>/dev/null
235+
ssh $ssh_opts -p "$port" "$user@$host" "echo 'Connection successful'" > "$ssh_output" 2>&1
236+
ssh_status=$?
217237
fi
218-
219-
if [ $? -eq 0 ]; then
238+
239+
if [ $ssh_status -eq 0 ]; then
220240
print_success "Connection successful"
241+
rm -f "$ssh_output"
221242
return 0
222243
else
223244
print_error "Connection failed"
245+
# Show error details if SSH_MANAGER_DEBUG is set
246+
if [ -n "$SSH_MANAGER_DEBUG" ]; then
247+
print_warning "Debug output:"
248+
cat "$ssh_output" | sed 's/^/ /'
249+
else
250+
print_info "Set SSH_MANAGER_DEBUG=1 to see detailed error output"
251+
fi
252+
rm -f "$ssh_output"
224253
return 1
225254
fi
226255
}

0 commit comments

Comments
 (0)