]> Untitled Git - git4ai.git/commitdiff
Improve parameter checks
authorNikolay Shaplov <dhyan@nataraj.su>
Sun, 7 Jun 2026 18:25:08 +0000 (18:25 +0000)
committerNikolay Shaplov <dhyan@nataraj.su>
Sun, 7 Jun 2026 18:25:08 +0000 (18:25 +0000)
cgi/git4ai-branches.pl
cgi/git4ai-cat-file.pl
cgi/git4ai-log.pl
cgi/git4ai-ls-tree.pl
cgi/git4ai-show.pl

index c81f4d32c1ab47eaa62171c78a94bb8a83b8ac07..c4de464c2e52be1cb597e1ab63cb282a709de871 100755 (executable)
@@ -5,11 +5,11 @@ use warnings;
 my $GIT_ROOT = '/var/lib/git';
 my $BASE_URL = 'https://gitweb.nataraj.world';
 
-my $repo = $ENV{GIT_REPO};
+my $repo = $ENV{GIT_REPO} // '';
 
-$repo =~ s{[^a-zA-Z0-9._-]}{}g;
-
-die unless $repo;
+die "Missing GIT_REPO\n"                    unless $repo;
+die "Invalid GIT_REPO: '$repo'\n"           unless $repo =~ /^[a-zA-Z0-9._-]+$/;
+die "Path traversal in GIT_REPO: '$repo'\n" if     $repo eq '..';
 
 my $git_dir = "$GIT_ROOT/$repo";
 
index 6781098ea5218a3f38ef718781a4703c5b7ce7de..b737c905ec04d8dda071f2192f7b6e6ad5dda1ab 100755 (executable)
@@ -4,14 +4,14 @@ use warnings;
 
 my $GIT_ROOT = '/var/lib/git';
 
-my $repo    = $ENV{GIT_REPO};
-my $blob_id = $ENV{GIT_ID} || $ENV{GIT_BLOB_ID};
-
-$repo    =~ s{[^a-zA-Z0-9._-]}{}g;
-$blob_id =~ s{[^0-9a-f]}{}g;
-
-die unless $repo;
-die unless $blob_id;
+my $repo    = $ENV{GIT_REPO}                      // '';
+my $blob_id = ($ENV{GIT_ID} || $ENV{GIT_BLOB_ID}) // '';
+
+die "Missing GIT_REPO\n"                    unless $repo;
+die "Invalid GIT_REPO: '$repo'\n"           unless $repo    =~ /^[a-zA-Z0-9._-]+$/;
+die "Path traversal in GIT_REPO: '$repo'\n" if     $repo    eq '..';
+die "Missing blob id\n"                     unless $blob_id;
+die "Invalid blob id: '$blob_id'\n"         unless $blob_id =~ /^[0-9a-f]{4,40}$/;
 
 open(my $fh, '-|', 'git', "--git-dir=$GIT_ROOT/$repo",
     'cat-file', 'blob', $blob_id)
index 812d3ed9e53015229149fb042ee32ba0612f58a1..ad38aac4e479e78b8100023b1641d2c015dd8624 100755 (executable)
@@ -6,20 +6,21 @@ my $GIT_ROOT = '/var/lib/git';
 my $PER_PAGE = 20;
 my $BASE_URL = 'https://gitweb.nataraj.world';
 
-my $repo   = $ENV{GIT_REPO};
-my $branch = $ENV{GIT_BRANCH} // 'master';
-my $offset = int($ENV{GIT_OFFSET} // 0);
-my $nonce = $ENV{GIT_NONCE} || $ENV{REQUEST_ID};
+my $repo   = $ENV{GIT_REPO}   // '';
+my $branch = $ENV{GIT_BRANCH} // '';
+my $offset = $ENV{GIT_OFFSET} // '0';
+my $nonce  = ($ENV{GIT_NONCE} || $ENV{REQUEST_ID}) // '';
 
+die "Missing GIT_REPO\n"                    unless $repo;
+die "Invalid GIT_REPO: '$repo'\n"           unless $repo   =~ /^[a-zA-Z0-9._-]+$/;
+die "Path traversal in GIT_REPO: '$repo'\n" if     $repo   eq '..';
+die "Missing GIT_BRANCH\n"                  unless $branch;
+die "Invalid GIT_BRANCH: '$branch'\n"       unless $branch =~ m{^[a-zA-Z0-9._/-]+$} || $branch eq '-';
+die "Invalid GIT_OFFSET: '$offset'\n"       unless $offset =~ /^[0-9]+$/;
 
-$repo   =~ s{[^a-zA-Z0-9._-]}{}g;
-$branch =~ s{[^a-zA-Z0-9._/\-]}{}g; 
-$offset = 0 if $offset < 0;
+$offset = int($offset);
 $nonce  =~ s{[^a-zA-Z0-9._-]}{}g;
 
-die unless $repo;
-die unless $branch;
-
 my $rand    = int(rand(1_000_000_000));
 my $git_dir = "$GIT_ROOT/$repo";
 
index 06ba1f6999875274cb96af8342b9c705017d4f42..7808f6bf63ec22fad370fa41dd4ac67ebadb7ba0 100755 (executable)
@@ -6,16 +6,18 @@ my $GIT_ROOT = '/var/lib/git';
 my $PER_PAGE = 50;
 my $BASE_URL = 'https://gitweb.nataraj.world';
 
-my $repo   = $ENV{GIT_REPO};
-my $commit_id  = $ENV{GIT_ID} || $ENV{GIT_COMMIT_ID};
-my $offset = int($ENV{GIT_OFFSET} // 0);
-
-$repo =~ s{[^a-zA-Z0-9._-]}{}g;
-$commit_id  =~ s{[^0-9a-f]}{}g;
-$offset = 0 if $offset < 0;
-
-die unless $repo;
-die unless $commit_id;
+my $repo      = $ENV{GIT_REPO}                        // '';
+my $commit_id = ($ENV{GIT_ID} || $ENV{GIT_COMMIT_ID}) // '';
+my $offset    = $ENV{GIT_OFFSET}                       // '0';
+
+die "Missing GIT_REPO\n"                      unless $repo;
+die "Invalid GIT_REPO: '$repo'\n"             unless $repo      =~ /^[a-zA-Z0-9._-]+$/;
+die "Path traversal in GIT_REPO: '$repo'\n"   if     $repo      eq '..';
+die "Missing commit id\n"                     unless $commit_id;
+die "Invalid commit id: '$commit_id'\n"       unless $commit_id =~ /^[0-9a-f]{4,40}$/;
+die "Invalid GIT_OFFSET: '$offset'\n"         unless $offset    =~ /^[0-9]+$/;
+
+$offset = int($offset);
 
 my $rand    = int(rand(1_000_000_000));
 my $git_dir = "$GIT_ROOT/$repo";
index 0f9eb9101e8a27c433b122a511847182df56572c..5549337630e80997dd7943456c1df2a73356b35c 100755 (executable)
@@ -4,14 +4,14 @@ use warnings;
 
 my $GIT_ROOT = '/var/lib/git';
 
-my $repo   = $ENV{GIT_REPO};
-my $commit = $ENV{GIT_ID} || $ENV{GIT_COMMIT};
+my $repo   = $ENV{GIT_REPO} // '';
+my $commit = ($ENV{GIT_ID} || $ENV{GIT_COMMIT}) // '';
 
-$repo   =~ s{[^a-zA-Z0-9._-]}{}g;
-$commit =~ s{[^0-9a-f]}{}g;
-
-die unless $repo;
-die unless $commit;
+die "Missing GIT_REPO\n"                    unless $repo;
+die "Invalid GIT_REPO: '$repo'\n"           unless $repo   =~ /^[a-zA-Z0-9._-]+$/;
+die "Path traversal in GIT_REPO: '$repo'\n" if     $repo   eq '..';
+die "Missing commit id\n"                   unless $commit;
+die "Invalid commit id: '$commit'\n"        unless $commit =~ /^[0-9a-f]{4,40}$/;
 
 print "Content-Type: text/plain; charset=utf-8\r\n";
 print "Cache-Control: no-store, no-cache, must-revalidate\r\n";