Files
affaan-m--everything-claude…/docs/zh-CN/rules/perl/security.md
T
wehub-resource-sync d48cda4081
CI / Test (ubuntu-latest, Node 18.x, bun) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, npm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, pnpm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, yarn) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 20.x, bun) (push) Failing after 17m13s
CI / Test (ubuntu-latest, Node 20.x, npm) (push) Failing after 18m42s
CI / Test (ubuntu-latest, Node 20.x, pnpm) (push) Failing after 15m0s
CI / Test (ubuntu-latest, Node 20.x, yarn) (push) Failing after 49m44s
CI / Test (ubuntu-latest, Node 22.x, bun) (push) Failing after 51m55s
CI / Test (ubuntu-latest, Node 22.x, pnpm) (push) Failing after 21m57s
CI / Test (ubuntu-latest, Node 22.x, npm) (push) Failing after 37m39s
CI / Test (ubuntu-latest, Node 22.x, yarn) (push) Failing after 34m7s
CI / Validate Components (push) Failing after 37m15s
CI / Python Tests (push) Failing after 10m1s
CI / Security Scan (push) Failing after 10m1s
CI / Lint (push) Failing after 17m12s
CI / Coverage (push) Failing after 20m19s
CI / Test (macos-latest, Node 18.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, yarn) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:55 +08:00

1.5 KiB

paths
paths
**/*.pl
**/*.pm
**/*.t
**/*.psgi
**/*.cgi

Perl 安全

本文档在 common/security.md 的基础上扩展了 Perl 相关的内容。

污染模式

  • 在所有 CGI/面向 Web 的脚本中使用 -T 标志
  • 在执行任何外部命令前,清理 %ENV ($ENV{PATH}$ENV{CDPATH} 等)

输入验证

  • 使用允许列表正则表达式进行去污化 — 绝不要使用 /(.*)/s
  • 使用明确的模式验证所有用户输入:
if ($input =~ /\A([a-zA-Z0-9_-]+)\z/) {
    my $clean = $1;
}

文件 I/O

  • 仅使用三参数 open — 绝不要使用两参数 open
  • 使用 Cwd::realpath 防止路径遍历:
use Cwd 'realpath';
my $safe_path = realpath($user_path);
die "Path traversal" unless $safe_path =~ m{\A/allowed/directory/};

进程执行

  • 使用 列表形式的 system() — 绝不要使用单字符串形式
  • 使用 IPC::Run3 来捕获输出
  • 绝对不要在反引号中使用变量插值
system('grep', '-r', $pattern, $directory);  # safe

SQL 注入预防

始终使用 DBI 占位符 — 绝不要将变量插值到 SQL 中:

my $sth = $dbh->prepare('SELECT * FROM users WHERE email = ?');
$sth->execute($email);

安全扫描

运行 perlcritic 并使用安全主题,严重级别设为 4 或更高:

perlcritic --severity 4 --theme security lib/

参考

有关全面的 Perl 安全模式、污染模式和安全 I/O,请参阅技能:perl-security