Communitygithub.com

perl-security

全面的 Perl 安全性指南,涵蓋污點模式、輸入驗證、安全程序執行、DBI 參數化查詢、網頁安全 (XSS/SQLi/CSRF) 以及 perlcritic 安全策略。在審查 Perl 輸入處理、程序執行、DBI 查詢或面向網頁的程式碼時使用。

perl-security 是什麼?

perl-security 是一個專為 Perl 應用程式設計的全面性安全技能,涵蓋從輸入驗證到輸出編碼的完整安全防護。它詳細說明了如何啟用 Perl 的污點模式 (`-T`),並提供嚴謹的解除污點模式,例如透過特定的正規表達式驗證使用者名稱或檔案名稱,而非使用過於寬鬆的 `(.*)` 來解除污點。在程序執行方面,技能強調使用清單形式的 `system()` 和 `exec()` 來避免 shell 注入,並推薦使用 `IPC::Run3` 或 `Capture::Tiny` 來安全地捕捉外部命令的輸出。對於資料庫操作,它要求所有 DBI 查詢都必須使用佔位符 (placeholder) 來防止 SQL 注入,並針對動態欄位排序提供了基於允許清單的驗證模式。此外,它還涵蓋了網頁安全最佳實踐,包括使用 `HTML::Entities` 進行 XSS 防護、內建 CSRF 令牌生成、以及設定安全相關的 HTTP 標頭(如 CSP、HSTS)。此技能支援所有主要的 Perl 網頁框架(Mojolicious、Dancer2、Catalyst),並提供了一個可整合至 CI/CD 流程的 perlcritic 安全策略設定檔,協助開發者系統性地審查程式碼,避免常見的反模式(如兩參數的 open 和字串形式的 system)。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/ECC/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 "命令失敗 (exit $?):$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));
}

驗證令牌時使用常數時間比較。大多數網頁框架(Mojolicious、Dancer2、Catalyst)都提供內建的 CSRF 保護 — 應優先選用,而非自行實作解決方案。

Session 與標頭安全性

use v5.36;

# Mojolicious session + headers
$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 令牌已產生並在狀態變更請求時驗證
Session 設定安全、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/content-engine

Create platform-native content systems for X, LinkedIn, TikTok, YouTube, newsletters, and repurposed multi-platform campaigns. Use when the user wants social posts, threads, scripts, content calendars, or one source asset adapted cleanly across platforms.

affaan-m/ECC

Multi-platform content distribution across X, LinkedIn, Threads, and Bluesky. Adapts content per platform using content-engine patterns. Never posts identical content cross-platform. Use when the user wants to distribute content across social platforms.

affaan-m/fal-ai-media

Unified media generation via fal.ai MCP — image, video, and audio. Covers text-to-image (Nano Banana), text/image-to-video (Seedance, Kling, Veo 3), text-to-speech (CSM-1B), and video-to-audio (ThinkSound). Use when the user wants to generate images, videos, or audio with AI.

affaan-m/manim-video

日本語翻訳:このファイルは manim-video 用の日本語翻訳が必要です

affaan-m/remotion-video-creation

Remotion のベストプラクティス - React で動画を作成する。3D、アニメーション、音声、字幕、チャート、トランジションなどをカバーするドメイン固有の29のルール。

affaan-m/video-editing

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

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.

agent-self-evaluation

Use after completing any non-trivial task. The agent self-rates its output on 5 axes — accuracy, completeness, clarity, actionability, conciseness — with concrete evidence per criterion. Produces a structured 1-5 scorecard with specific improvement suggestions.

agent-sort

Build an evidence-backed ECC install plan for a specific repo by sorting skills, commands, rules, hooks, and extras into DAILY vs LIBRARY buckets using parallel repo-aware review passes. Use when ECC should be trimmed to what a project actually needs instead of loading the full bundle.

ai-first-engineering

Engineering operating model for teams where AI agents generate a large share of implementation output. Use when setting team process, review gates, or ownership rules for a codebase largely written by agents.

ai-regression-testing

Regression testing strategies for AI-assisted development. Sandbox-mode API testing without database dependencies, automated bug-check workflows, and patterns to catch AI blind spots where the same model writes and reviews code. Use when adding regression coverage to AI-assisted code, or when the same model both wrote and reviewed a change.

android-clean-architecture

Clean Architecture patterns for Android and Kotlin Multiplatform projects — module structure, dependency rules, UseCases, Repositories, and data layer patterns. Use when structuring modules, layers, or data flow in an Android or KMP project.

angular-developer

Generates Angular code and provides architectural guidance. Trigger when creating projects, components, or services, or for best practices on reactivity (signals, linkedSignal, resource), forms, dependency injection, routing, SSR, accessibility (ARIA), animations, styling (component styles, Tailwind CSS), testing, or CLI tooling.

相關技能