Communitygithub.com

Perl 安全性技能

涵蓋污點模式、輸入驗證、安全行程執行、DBI 參數化查詢、網頁安全 (XSS/SQLi/CSRF) 以及 perlcritic 安全性規範。適用於審查 Perl 的輸入處理、行程執行、DBI 查詢或對外網路程式碼。

Perl 安全性技能 是什麼?

這是一套專門為 Perl 應用程式設計的全方位安全性指南,旨在幫助開發者在處理使用者輸入、執行系統指令或操作資料庫時,遵循最佳安全實踐。技能涵蓋了從啟用 `-T` 污點模式、使用允許清單進行輸入驗證、利用三引數 open 進行安全檔案操作、採用清單形式呼叫 `system` 與 `exec` 以避免 shell 注入,到全面使用 DBI 佔位符以防止 SQL 注入等關鍵領域。在網頁安全方面,提供了對抗 XSS、CSRF 以及設定安全標頭 (如 CSP、HSTS) 的具體做法。此外,技能還推薦使用 `perlcritic` 搭配安全性主題設定檔進行自動化程式碼審查,並提供一份快速安全檢查清單,幫助開發者快速定位常見弱點。這些模式適用於任何使用 CGI、Mojolicious、Dancer2 或 Catalyst 等框架的 Perl 環境,強調「縱深防禦」原則,確保每一層防護都能獨立運作,共同抵禦攻擊。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/perl-security

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Perl 安全性模式

針對 Perl 應用程式的全方位安全性指南,涵蓋輸入驗證、注入預防以及安全編碼實作。

啟用時機

  • 在 Perl 應用程式中處理使用者輸入
  • 建置 Perl 網頁應用程式 (CGI、Mojolicious、Dancer2、Catalyst)
  • 審查 Perl 程式碼的安全性弱點
  • 對使用者提供的路徑進行檔案操作
  • 從 Perl 執行系統指令
  • 編寫 DBI 資料庫查詢

運作方式

從感知污點的輸入邊界開始,然後向外擴展:驗證並清除污點、限制檔案系統與行程執行範圍,並在每個地方使用參數化 DBI 查詢。下列範例展示這項技能期望你在部署會接觸使用者輸入、shell 或網路的 Perl 程式碼之前,所應套用的安全預設值。

污點模式

Perl 的污點模式 (-T) 會追蹤來自外部來源的資料,並防止它們在未經明確驗證的情況下用於不安全的操作。

啟用污點模式

#!/usr/bin/perl -T
use v5.36;

# 受污點:任何來自程式外部的資料
my $input    = $ARGV[0];        # 受污點
my $env_path = $ENV{PATH};      # 受污點
my $form     = <STDIN>;         # 受污點
my $query    = $ENV{QUERY_STRING}; # 受污點

# 及早清除 PATH(在污點模式下是必要的)
$ENV{PATH} = '/usr/local/bin:/usr/bin:/bin';
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};

清除污點模式

use v5.36;

# 好:使用特定正則表達式驗證並清除污點
sub untaint_username($input) {
    if ($input =~ /^([a-zA-Z0-9_]{3,30})$/) {
        return $1;  # $1 是未受污點的
    }
    die "無效的使用者名稱:必須是 3-30 個字母數字字元\n";
}

# 好:驗證並清除污點檔案路徑
sub untaint_filename($input) {
    if ($input =~ m{^([a-zA-Z0-9._-]+)$}) {
        return $1;
    }
    die "無效的檔名:包含不安全字元\n";
}

# 壞:過度寬鬆的清除污點(違背目的)
sub bad_untaint($input) {
    $input =~ /^(.*)$/s;
    return $1;  # 接受任何東西 — 毫無意義
}

輸入驗證

允許清單勝於封鎖清單

use v5.36;

# 好:允許清單 — 明確定義哪些是允許的
sub validate_sort_field($field) {
    my %allowed = map { $_ => 1 } qw(name email created_at updated_at);
    die "無效的排序欄位:$field\n" unless $allowed{$field};
    return $field;
}

# 好:使用特定模式驗證
sub validate_email($email) {
    if ($email =~ /^([a-zA-Z0-9._%+-]+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$/) {
        return $1;
    }
    die "無效的電子郵件地址\n";
}

sub validate_integer($input) {
    if ($input =~ /^(-?\d{1,10})$/) {
        return $1 + 0;  # 轉換為數字
    }
    die "無效的整數\n";
}

# 壞:封鎖清單 — 永遠不完整
sub bad_validate($input) {
    die "無效" if $input =~ /[<>"';&|]/;  # 遺漏編碼攻擊
    return $input;
}

長度限制

use v5.36;

sub validate_comment($text) {
    die "需要提供評論\n"        unless length($text) > 0;
    die "評論超過 10000 字元\n" if length($text) > 10_000;
    return $text;
}

安全正則表達式

ReDoS 預防

當巢狀量詞搭配重疊模式時,會發生災難性回溯。

use v5.36;

# 壞:易受 ReDoS 攻擊(指數級回溯)
my $bad_re = qr/^(a+)+$/;           # 巢狀量詞
my $bad_re2 = qr/^([a-zA-Z]+)*$/;   # 字元類別上的巢狀量詞
my $bad_re3 = qr/^(.*?,){10,}$/;    # 重複的貪婪/懶惰組合

# 好:改寫為無巢狀結構
my $good_re = qr/^a+$/;             # 單一量詞
my $good_re2 = qr/^[a-zA-Z]+$/;     # 字元類別上的單一量詞

# 好:使用佔有量詞或原子群來防止回溯
my $safe_re = qr/^[a-zA-Z]++$/;             # 佔有量詞 (5.10+)
my $safe_re2 = qr/^(?>a+)$/;                # 原子群

# 好:對不受信任的模式強制設定逾時
use POSIX qw(alarm);
sub safe_match($string, $pattern, $timeout = 2) {
    my $matched;
    eval {
        local $SIG{ALRM} = sub { die "正則表達式逾時\n" };
        alarm($timeout);
        $matched = $string =~ $pattern;
        alarm(0);
    };
    alarm(0);
    die $@ if $@;
    return $matched;
}

安全檔案操作

三引數 open

use v5.36;

# 好:三引數 open、詞彙檔案控制代碼、檢查回傳值
sub read_file($path) {
    open my $fh, '<:encoding(UTF-8)', $path
        or die "無法開啟 '$path':$!\n";
    local $/;
    my $content = <$fh>;
    close $fh;
    return $content;
}

# 壞:搭配使用者資料的雙引數 open(指令注入)
sub bad_read($path) {
    open my $fh, $path;        # 如果 $path = "|rm -rf /",會執行指令!
    open my $fh, "< $path";   # Shell 元字元注入
}

TOCTOU 預防與路徑遍歷

use v5.36;
use Fcntl qw(:DEFAULT :flock);
use File::Spec;
use Cwd qw(realpath);

# 原子性檔案建立
sub create_file_safe($path) {
    sysopen(my $fh, $path, O_WRONLY | O_CREAT | O_EXCL, 0600)
        or die "無法建立 '$path':$!\n";
    return $fh;
}

# 驗證路徑保持在允許目錄內
sub safe_path($base_dir, $user_path) {
    my $real = realpath(File::Spec->catfile($base_dir, $user_path))
        // die "路徑不存在\n";
    my $base_real = realpath($base_dir)
        // die "基礎目錄不存在\n";
    die "封鎖路徑遍歷\n" unless $real =~ /^\Q$base_real\E(?:\/|\z)/;
    return $real;
}

對於暫存檔案,請使用 File::Temp(tempfile(UNLINK => 1)),並使用 flock(LOCK_EX) 來防止競爭條件。

安全行程執行

清單形式的 system 和 exec

use v5.36;

# 好:清單形式 — 無 shell 插值
sub run_command(@cmd) {
    system(@cmd) == 0
        or die "指令失敗:@cmd\n";
}

run_command('grep', '-r', $user_pattern, '/var/log/app/');

# 好:使用 IPC::Run3 安全地擷取輸出
use IPC::Run3;
sub capture_output(@cmd) {
    my ($stdout, $stderr);
    run3(\@cmd, \undef, \$stdout, \$stderr);
    if ($?) {
        die "指令失敗 (退出碼 $?):$stderr\n";
    }
    return $stdout;
}

# 壞:字串形式 — shell 注入!
sub bad_search($pattern) {
    system("grep -r '$pattern' /var/log/app/");  # 如果 $pattern = "'; rm -rf / #"
}

# 壞:搭配插值的反引號
my $output = `ls $user_dir`;   # Shell 注入風險

也可以使用 Capture::Tiny 來安全地從外部指令擷取 stdout/stderr。

SQL 注入預防

DBI 佔位符

use v5.36;
use DBI;

my $dbh = DBI->connect($dsn, $user, $pass, {
    RaiseError => 1,
    PrintError => 0,
    AutoCommit => 1,
});

# 好:參數化查詢 — 一律使用佔位符
sub find_user($dbh, $email) {
    my $sth = $dbh->prepare('SELECT * FROM users WHERE email = ?');
    $sth->execute($email);
    return $sth->fetchrow_hashref;
}

sub search_users($dbh, $name, $status) {
    my $sth = $dbh->prepare(
        'SELECT * FROM users WHERE name LIKE ? AND status = ? ORDER BY name'
    );
    $sth->execute("%$name%", $status);
    return $sth->fetchall_arrayref({});
}

# 壞:SQL 中的字串插值(SQLi 弱點!)
sub bad_find($dbh, $email) {
    my $sth = $dbh->prepare("SELECT * FROM users WHERE email = '$email'");
    # 如果 $email = "' OR 1=1 --",回傳所有使用者
    $sth->execute;
    return $sth->fetchrow_hashref;
}

動態欄位允許清單

use v5.36;

# 好:針對允許清單驗證欄位名稱
sub order_by($dbh, $column, $direction) {
    my %allowed_cols = map { $_ => 1 } qw(name email created_at);
    my %allowed_dirs = map { $_ => 1 } qw(ASC DESC);

    die "無效的欄位:$column\n"    unless $allowed_cols{$column};
    die "無效的排序方向:$direction\n" unless $allowed_dirs{uc $direction};

    my $sth = $dbh->prepare("SELECT * FROM users ORDER BY $column $direction");
    $sth->execute;
    return $sth->fetchall_arrayref({});
}

# 壞:直接插值使用者選擇的欄位
sub bad_order($dbh, $column) {
    $dbh->prepare("SELECT * FROM users ORDER BY $column");  # SQLi!
}

DBIx::Class (ORM 安全)

use v5.36;

# DBIx::Class 產生安全的參數化查詢
my @users = $schema->resultset('User')->search({
    status => 'active',
    email  => { -like => '%@example.com' },
}, {
    order_by => { -asc => 'name' },
    rows     => 50,
});

網頁安全

XSS 預防

use v5.36;
use HTML::Entities qw(encode_entities);
use URI::Escape qw(uri_escape_utf8);

# 好:對 HTML 上下文進行輸出編碼
sub safe_html($user_input) {
    return encode_entities($user_input);
}

# 好:對 URL 上下文進行編碼
sub safe_url_param($value) {
    return uri_escape_utf8($value);
}

# 好:對 JSON 上下文進行編碼
use JSON::MaybeXS qw(encode_json);
sub safe_json($data) {
    return encode_json($data);  # 處理跳脫
}

# 範本自動跳脫 (Mojolicious)
# <%= $user_input %>   — 自動跳脫(安全)
# <%== $raw_html %>    — 原始輸出(危險,僅用於受信任內容)

# 範本自動跳脫 (Template Toolkit)
# [% user_input | html %]  — 明確的 HTML 編碼

# 壞:HTML 中的原始輸出
sub bad_html($input) {
    print "<div>$input</div>";  # 如果 $input 包含 <script>,則為 XSS
}

CSRF 防護

use v5.36;
use Crypt::URandom qw(urandom);
use MIME::Base64 qw(encode_base64url);

sub generate_csrf_token() {
    return encode_base64url(urandom(32));
}

驗證 token 時請使用常數時間比較。大多數網頁框架(Mojolicious、Dancer2、Catalyst)都提供內建的 CSRF 防護 — 優先使用它們而非自行實作。

Session 與標頭安全

use v5.36;

# Mojolicious session + 標頭
$app->secrets(['long-random-secret-rotated-regularly']);
$app->sessions->secure(1);          # 僅限 HTTPS
$app->sessions->samesite('Lax');

$app->hook(after_dispatch => sub ($c) {
    $c->res->headers->header('X-Content-Type-Options' => 'nosniff');
    $c->res->headers->header('X-Frame-Options'        => 'DENY');
    $c->res->headers->header('Content-Security-Policy' => "default-src 'self'");
    $c->res->headers->header('Strict-Transport-Security' => 'max-age=31536000; includeSubDomains');
});

輸出編碼

一律根據上下文對輸出進行編碼:HTML 使用 HTML::Entities::encode_entities()、URL 使用 URI::Escape::uri_escape_utf8()、JSON 使用 JSON::MaybeXS::encode_json()。

CPAN 模組安全

  • 固定版本 在 cpanfile 中:requires 'DBI', '== 1.643';
  • 優先選擇有維護的模組:在 MetaCPAN 檢查最近的發行版本
  • 最小化依賴:每個依賴都是一個攻擊面

安全性工具

perlcritic 安全性規範

# .perlcriticrc — 以安全為重點的設定
severity = 3
theme = security + core

# 要求三引數 open
[InputOutput::RequireThreeArgOpen]
severity = 5

# 要求檢查系統呼叫
[InputOutput::RequireCheckedSyscalls]
functions = :builtins
severity = 4

# 禁止字串 eval
[BuiltinFunctions::ProhibitStringyEval]
severity = 5

# 禁止反引號運算子
[InputOutput::ProhibitBacktickOperators]
severity = 4

# 在 CGI 中要求污點檢查
[Modules::RequireTaintChecking]
severity = 5

# 禁止雙引數 open
[InputOutput::ProhibitTwoArgOpen]
severity = 5

# 禁止裸字檔案控制代碼
[InputOutput::ProhibitBarewordFileHandles]
severity = 5

執行 perlcritic

# 檢查一個檔案
perlcritic --severity 3 --theme security lib/MyApp/Handler.pm

# 檢查整個專案
perlcritic --severity 3 --theme security lib/

# CI 整合
perlcritic --severity 4 --theme security --quiet lib/ || exit 1

快速安全檢查清單

檢查項目需驗證的內容
污點模式CGI/網頁腳本的 -T 旗標
輸入驗證允許清單模式、長度限制
檔案操作三引數 open、路徑遍歷檢查
行程執行清單形式的 system、無 shell 插值
SQL 查詢DBI 佔位符、絕不插值
HTML 輸出encode_entities()、範本自動跳脫
CSRF Token產生、在修改狀態的請求上驗證
Session 設定Secure、HttpOnly、SameSite cookie
HTTP 標頭CSP、X-Frame-Options、HSTS
依賴套件固定版本、已稽核的模組
正則表達式安全性無巢狀量詞、已錨定的模式
錯誤訊息不向使用者洩漏堆疊追蹤或路徑

反模式

# 1. 搭配使用者資料的雙引數 open(指令注入)
open my $fh, $user_input;               # 重大弱點

# 2. 字串形式的 system(shell 注入)
system("convert $user_file output.png"); # 重大弱點

# 3. SQL 字串插值
$dbh->do("DELETE FROM users WHERE id = $id");  # SQLi

# 4. 搭配使用者輸入的 eval(程式碼注入)
eval $user_code;                         # 遠端程式碼執行

# 5. 未經清理即信任 $ENV
my $path = $ENV{UPLOAD_DIR};             # 可能被操控
system("ls $path");                      # 雙重弱點

# 6. 未經驗證即停用污點
($input) = $input =~ /(.*)/s;           # 懶惰清除污點 — 違背目的

# 7. HTML 中的原始使用者資料
print "<div>歡迎, $username!</div>";  # XSS

# 8. 未經驗證的重導向
print $cgi->redirect($user_url);         # 開放式重導向

請記住:Perl 的靈活性強大,但也需要紀律。對外網路程式碼請使用污點模式、使用允許清單驗證所有輸入、對每個查詢使用 DBI 佔位符,並針對上下文對所有輸出進行編碼。縱深防禦 — 永遠不要依賴單一保護層。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相關技能