Skip to content

Commit dc4930b

Browse files
committed
action: auto-sanitize default or error on invalid user-defined hostname
Fixes #192
1 parent 6d2f249 commit dc4930b

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

action.yml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ inputs:
3737
required: false
3838
default: ''
3939
hostname:
40-
description: 'Fixed hostname to use.'
40+
description: 'Fixed hostname to use. Must be a valid DNS label (alphanumeric and dashes only, 1-63 characters, cannot start or end with a dash). If not provided, a hostname will be generated based on the runner name.'
4141
required: false
4242
default: ''
4343
statedir:
@@ -301,13 +301,42 @@ runs:
301301
TIMEOUT: ${{ inputs.timeout }}
302302
RETRY: ${{ inputs.retry }}
303303
run: |
304+
sanitize_hostname() {
305+
local hostname="$1"
306+
hostname=$(echo "$hostname" | sed 's/[^a-zA-Z0-9-]/-/g') # Replace invalid characters with dashes
307+
hostname=$(echo "$hostname" | cut -c1-63) # Truncate to 63 characters maximum
308+
hostname=$(echo "$hostname" | sed 's/^-*//;s/-*$//') # Remove leading/trailing dashes
309+
echo "$hostname"
310+
}
311+
312+
is_valid_dns_label() {
313+
local hostname="$1"
314+
if [ ${#hostname} -eq 0 ] || [ ${#hostname} -gt 63 ]; then # Check length (1-63 characters)
315+
return 1
316+
fi
317+
if ! echo "$hostname" | grep -qE '^[a-zA-Z0-9-]+$'; then # Check for valid characters (alphanumeric and dashes only)
318+
return 1
319+
fi
320+
if echo "$hostname" | grep -qE '^-|-$'; then # Check that it doesn't start or end with dash
321+
return 1
322+
fi
323+
return 0
324+
}
325+
304326
if [ -z "${HOSTNAME}" ]; then
305327
if [ "${{ runner.os }}" == "Windows" ]; then
306328
HOSTNAME="github-$COMPUTERNAME"
307-
else
329+
else
308330
HOSTNAME="github-$(hostname)"
309331
fi
332+
HOSTNAME=$(sanitize_hostname "$HOSTNAME")
333+
else
334+
if ! is_valid_dns_label "$HOSTNAME"; then
335+
echo "::error::HOSTNAME '$HOSTNAME' is not a valid DNS label. It should contain only alphanumeric characters and dashes, be 1-63 characters long, and not start or end with a dash."
336+
exit 1
337+
fi
310338
fi
339+
311340
if [ -n "${{ inputs['oauth-secret'] }}" ]; then
312341
TAILSCALE_AUTHKEY="${{ inputs['oauth-secret'] }}?preauthorized=true&ephemeral=true"
313342
TAGS_ARG="--advertise-tags=${{ inputs.tags }}"

0 commit comments

Comments
 (0)