Skip to content

Commit a619852

Browse files
MiguelBarrochrisbra
authored andcommitted
patch 9.2.0089: netrw: does not take port into account in hostname validation
Problem: netrw: does not take port into account in hostname validation (after v9.2.0073) Solution: Update hostname validation check and test for an optional port number (Miguel Barro) closes: #19533 Signed-off-by: Miguel Barro <miguel.barro@live.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 93cb5e5 commit a619852

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

runtime/pack/dist/opt/netrw/autoload/netrw.vim

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
" 2026 Feb 15 by Vim Project fix global variable initialization for MS-Windows #19287
2222
" 2026 Feb 21 by Vim Project better absolute path detection on MS-Windows #19477
2323
" 2026 Feb 27 by Vim Project Make the hostname validation more strict
24+
" 2026 Mar 01 by Vim Project include portnumber in hostname checking #19533
2425
" Copyright: Copyright (C) 2016 Charles E. Campbell {{{1
2526
" Permission is hereby granted to use and distribute this code,
2627
" with or without modifications, provided that this copyright
@@ -2592,7 +2593,8 @@ endfunction
25922593

25932594
" s:NetrwValidateHostname: Validate that the hostname is valid {{{2
25942595
" Input:
2595-
" hostname, may include an optional username, e.g. user@hostname
2596+
" hostname, may include an optional username and port number, e.g.
2597+
" user@hostname:port
25962598
" allow a alphanumeric hostname or an IPv(4/6) address
25972599
" Output:
25982600
" true if g:netrw_machine is valid according to RFC1123 #Section 2
@@ -2601,17 +2603,19 @@ function s:NetrwValidateHostname(hostname)
26012603
let user_pat = '\%([a-zA-Z0-9._-]\+@\)\?'
26022604
" Hostname: 1-64 chars, alphanumeric/dots/hyphens.
26032605
" No underscores. No leading/trailing dots/hyphens.
2604-
let host_pat = '[a-zA-Z0-9]\%([-a-zA-Z0-9.]{,62}[a-zA-Z0-9]\)\?$'
2606+
let host_pat = '[a-zA-Z0-9]\%([-a-zA-Z0-9.]\{0,62}[a-zA-Z0-9]\)\?'
2607+
" Port: 16 bit unsigned integer
2608+
let port_pat = '\%(:\d\{1,5\}\)\?$'
26052609

26062610
" IPv4: 1-3 digits separated by dots
2607-
let ipv4_pat = '\%(\d\{1,3}\.\)\{3\}\d\{1,3\}$'
2611+
let ipv4_pat = '\%(\d\{1,3}\.\)\{3\}\d\{1,3\}'
26082612

26092613
" IPv6: Hex, colons, and optional brackets
2610-
let ipv6_pat = '\[\?\%([a-fA-F0-9:]\{2,}\)\+\]\?$'
2614+
let ipv6_pat = '\[\?\%([a-fA-F0-9:]\{2,}\)\+\]\?'
26112615

2612-
return a:hostname =~? '^'.user_pat.host_pat ||
2613-
\ a:hostname =~? '^'.user_pat.ipv4_pat ||
2614-
\ a:hostname =~? '^'.user_pat.ipv6_pat
2616+
return a:hostname =~? '^'.user_pat.host_pat.port_pat ||
2617+
\ a:hostname =~? '^'.user_pat.ipv4_pat.port_pat ||
2618+
\ a:hostname =~? '^'.user_pat.ipv6_pat.port_pat
26152619
endfunction
26162620

26172621
" NetUserPass: set username and password for subsequent ftp transfer {{{2

src/testdir/test_plugin_netrw.vim

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ function Test_NetrwFile(fname) abort
133133
return s:NetrwFile(a:fname)
134134
endfunction
135135

136+
" Test hostname validation
137+
function Test_NetrwValidateHostname(hostname) abort
138+
return s:NetrwValidateHostname(a:hostname)
139+
endfunction
140+
136141
" }}}
137142
END
138143

@@ -564,6 +569,30 @@ func Test_netrw_reject_evil_hostname()
564569
let msg = execute(':e scp://x;touch RCE;x/dir/')
565570
let msg = split(msg, "\n")[-1]
566571
call assert_match('Rejecting invalid hostname', msg)
567-
endfunction
572+
endfunc
573+
574+
func Test_netrw_hostname()
575+
let valid_hostnames = [
576+
\ 'localhost',
577+
\ '127.0.0.1',
578+
\ '::1',
579+
\ '0:0:0:0:0:0:0:1',
580+
\ 'user@localhost',
581+
\ 'usuario@127.0.0.1',
582+
\ 'utilisateur@::1',
583+
\ 'benutzer@0:0:0:0:0:0:0:1',
584+
\ 'localhost:22',
585+
\ '127.0.0.1:80',
586+
\ '[::1]:443',
587+
\ '[0:0:0:0:0:0:0:1]:5432',
588+
\ 'user@localhost:22',
589+
\ 'usuario@127.0.0.1:80',
590+
\ 'utilisateur@[::1]:443',
591+
\ 'benutzer@[0:0:0:0:0:0:0:1]:5432']
592+
593+
for hostname in valid_hostnames
594+
call assert_true(Test_NetrwValidateHostname(hostname), $"Valid hostname {hostname} was rejected")
595+
endfor
596+
endfunc
568597

569598
" vim:ts=8 sts=2 sw=2 et

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ static char *(features[]) =
734734

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
89,
737739
/**/
738740
88,
739741
/**/

0 commit comments

Comments
 (0)