2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-07-31 18:19:40 +00:00
Files
xcat-core/xCAT-test/unit/grub2_netboot_protocol.t
T
Vinícius Ferrão cb6a139286 fix(grub2): anchor the netboot protocol validation regex
noderes.netboot is parsed as grub2-<protocol> and validated with
/^http|tftp$/, which reads as (^http)|(tftp$) and so also accepts values
such as https, httpx and xtftp.

A value that passes but is not exactly "http" then falls through to
set root=$grub2protocol,$serverip, which drops site.httpport, so the mistake
surfaces as a confusing grub2 failure at boot instead of the "Invalid netboot
method" error this check exists to give. grub2-https cannot work in any case,
since only "insmod http" is emitted.

Supported values are unchanged: grub2, grub2-http and grub2-tftp.
2026-07-25 17:28:27 -03:00

36 lines
1.5 KiB
Perl

#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
my $grub2_path = defined $ENV{XCATROOT} ? "$ENV{XCATROOT}/lib/perl/xCAT_plugin/grub2.pm" : '';
$grub2_path = "xCAT-server/lib/xcat/plugins/grub2.pm"
unless -f $grub2_path;
plan skip_all => "grub2.pm not found" unless -f $grub2_path;
my $grub2 = do { local $/; open my $fh, '<', $grub2_path or die $!; <$fh> };
# pull the real validation pattern out of the plugin so this tests the shipped
# regex rather than a copy of it
my ($pattern) = $grub2 =~ m{unless \(\$grub2protocol =~ (/[^/]+/[a-z]*)\)};
ok($pattern, 'found the netboot protocol validation regex in grub2.pm');
my $accepts = eval "sub { my \$v = shift; return scalar(\$v =~ $pattern) }";
ok($accepts, 'validation regex compiles');
# noderes.netboot is documented as grub2, grub2-http and grub2-tftp, so the
# protocol taken from grub2-<protocol> may only ever be http or tftp
ok($accepts->('http'), 'grub2-http is accepted');
ok($accepts->('tftp'), 'grub2-tftp is accepted');
# an unanchored alternation, /^http|tftp$/, reads as (^http)|(tftp$) and lets
# these through; they then skip the httpport branch and drop the port
ok(!$accepts->('https'), 'grub2-https is rejected rather than silently dropping site.httpport');
ok(!$accepts->('httpx'), 'a protocol merely starting with http is rejected');
ok(!$accepts->('xtftp'), 'a protocol merely ending with tftp is rejected');
ok(!$accepts->('ftp'), 'an unsupported protocol is rejected');
ok(!$accepts->(''), 'an empty protocol is rejected');
done_testing();