From 1a55be7245d463aa84e274531cbdb0dd2bb8c743 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Sun, 27 Jun 2021 13:20:26 -0700 Subject: [PATCH] Project.pm/Validator.pm: switch to refactored valid_branch_name check Replace the haphazard regex that has become unmaintainable (and incomplete) with a call to the new valid_branch_name function instead. This results in more readable and maintainable code. Be careful with the call out from Girocco::Validator as unusual scoping is in effect while validating and an explicit call to the actual Girocco::ValidUtil::valid_branch_name function must be used. Together with that, be careful to avoid import name pollution too. To make this easy, modify Girocco::Util to automatically use the new Girocco::ValidUtil module and export its valid_branch_name function along with all the other valid_xxx functions. The other valid_xxx functions in Girocco::Util are just begging to be moved into the new Girocco::ValidUtil module. Signed-off-by: Kyle J. McKay --- Girocco/Project.pm | 5 ++--- Girocco/Util.pm | 4 +++- Girocco/Validator.pm | 4 +++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Girocco/Project.pm b/Girocco/Project.pm index 29f27ef..f3f97db 100644 --- a/Girocco/Project.pm +++ b/Girocco/Project.pm @@ -1229,7 +1229,7 @@ sub conjure { my $def_head = $self->{HEAD}; defined($def_head) && $def_head ne "" or $def_head = $Girocco::Config::initial_branch; defined($def_head) or $def_head = ""; - $def_head ne "" && $def_head !~ /^\/|^\.|[\x00-\x1f \x7f\[\[~^"'"<>*?\\:]|\@\{|\.\.|\.lock$|\.$|\/$/ or + $def_head ne "" && valid_branch_name($def_head) or $def_head = "master"; my $cur_head = read_HEAD_ref($self->{path}); defined($cur_head) && $cur_head =~ m{^refs/heads/\Q$def_head\E$} or @@ -1678,9 +1678,8 @@ sub set_HEAD { my $self = shift; my $newHEAD = shift; # Cursory checks only -- if you want to break your HEAD, be my guest - if ($newHEAD =~ /^\/|^\.|[\x00-\x1f \x7f\[\[~^"'"<>*?\\:]|\@\{|\.\.|\.lock$|\.$|\/$/) { + valid_branch_name($newHEAD) or die "grossly invalid new HEAD: $newHEAD"; - } system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD"); die "could not set HEAD" if ($? >> 8); ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob' diff --git a/Girocco/Util.pm b/Girocco/Util.pm index 78bc55a..21e981e 100644 --- a/Girocco/Util.pm +++ b/Girocco/Util.pm @@ -7,6 +7,7 @@ use warnings; use Girocco::Config; use Girocco::ConfigUtil; use Girocco::TimedToken; +use Girocco::ValidUtil; use Time::Local; use Scalar::Util qw(looks_like_number); use Encode (); @@ -28,7 +29,8 @@ BEGIN { read_config_file_hash is_git_dir git_bool util_path is_shellish read_HEAD_ref git_add_config to_json json_bool from_json ref_indicator get_token_key - get_timed_token get_token_field check_timed_token); + get_timed_token get_token_field check_timed_token + valid_branch_name); } BEGIN {require "Girocco/extra/capture_command.pl"} diff --git a/Girocco/Validator.pm b/Girocco/Validator.pm index f1a4fb2..b6fc102 100644 --- a/Girocco/Validator.pm +++ b/Girocco/Validator.pm @@ -15,6 +15,8 @@ BEGIN { die "Girocco::Config must already be 'use'd before ".__PACKAGE__." is 'use'd.\n"; } +use Girocco::ValidUtil (); + package Girocco::Config; use strict; @@ -74,7 +76,7 @@ $project_edit_timeout =~ /^[1-9][0-9]*$/ or defined($mailsh_sizelimit) && $mailsh_sizelimit =~ /^[1-9][0-9]*$/ or die "Girocco::Config: \$mailsh_sizelimit must be a positive number"; !defined($initial_branch) || $initial_branch eq "" || - $initial_branch !~ /^\/|^\.|[\x00-\x1f \x7f\[\[~^"'"<>*?\\:]|\@\{|\.\.|\.lock$|\.$|\/$/ or + Girocco::ValidUtil::valid_branch_name($initial_branch) or die "Girocco::Config: \$initial_branch grossly invalid: $initial_branch"; if (defined($empty_commit_message)) { $empty_commit_message =~ s/^\s+//; -- 2.11.4.GIT