1#!/usr/bin/perl -T 2# 3# Check that Term::ANSIColor untaints generated constants. 4# 5# It's possible that the name of the constant function that we're calling 6# could be tained (such as by loading the name of the constant function from 7# an environment variable). Term::ANSIColor does the work to untaint it; be 8# sure that the taint flag is properly cleared. 9# 10# Copyright 2012, 2020 Russ Allbery <rra@cpan.org> 11# 12# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl 13 14use 5.008; 15use strict; 16use warnings; 17 18use Test::More tests => 4; 19 20# Load the module. 21BEGIN { 22 delete $ENV{ANSI_COLORS_ALIASES}; 23 delete $ENV{ANSI_COLORS_DISABLED}; 24 delete $ENV{NO_COLOR}; 25 use_ok('Term::ANSIColor', qw(:pushpop)); 26} 27 28# Generate a tainted constant name. PATH is always tainted, and tainting is 29# sticky, so we can prepend the name to whatever PATH holds and then chop it 30# off again. 31my $constant = substr('BOLD' . $ENV{PATH}, 0, length('BOLD')); 32 33# Using that as a constant should now work without any tainting problems. 34## no critic (TestingAndDebugging::ProhibitNoStrict) 35{ 36 no strict 'refs'; 37 is(&{$constant}(), "\e[1m", 'Constant subs are not tainted'); 38 is(BOLD(), "\e[1m", '...and we can call the sub again'); 39 ok(defined(&Term::ANSIColor::BOLD), '...and it is now defined'); 40} 41