Security Advisories (1)
CVE-2025-15604 (2026-03-28)

Amon2 versions before 6.17 for Perl use an insecure random_string implementation for security functions. In versions 6.06 through 6.16, the random_string function will attempt to read bytes from the /dev/urandom device, but if that is unavailable then it generates bytes by concatenating a SHA-1 hash seeded with the built-in rand() function, the PID, and the high resolution epoch time. The PID will come from a small set of numbers, and the epoch time may be guessed, if it is not leaked from the HTTP Date header. The built-in rand function is unsuitable for cryptographic usage. Before version 6.06, there was no fallback when /dev/urandom was not available. Before version 6.04, the random_string function used the built-in rand() function to generate a mixed-case alphanumeric string. This function may be used for generating session ids, generating secrets for signing or encrypting cookie session data and generating tokens used for Cross Site Request Forgery (CSRF) protection.

NAME

Amon2::Manual::Tutorial.ja - Amon2 日本語チュートリアル

チュートリアル

示板をつくってみましょう。

インストール

cpanm にて、Amon2 のサイトからダウンロードしてください。

% cpanm --mirror http://orepan.64p.org/ Amon2

ハローワールド

まずは、アプリケーションの雛形を作成します。今回は、ORM に DBIx::Skinny をつかうので、--skinny オプションをつけておきます。

$ amon2-setup.pl --skinny Hello
$ cd Hello

雛形のディレクトリ構造は以下のようになっています。非常にシンプルでわかりやすい構造です。

.
├── Hello.psgi
├── Makefile.PL
├── config
│   └── development.pl
├── extlib
├── htdocs
│   └── static
│       ├── css
│       │   └── main.css
│       ├── img
│       └── js
├── lib
│   ├── Hello
│   │   ├── ConfigLoader.pm
│   │   ├── DB.pm
│   │   ├── M
│   │   ├── Web
│   │   │   ├── C
│   │   │   │   └── Root.pm
│   │   │   ├── Dispatcher.pm
│   │   │   ├── Request.pm
│   │   │   └── Response.pm
│   │   └── Web.pm
│   └── Hello.pm
├── script
│   ├── cron
│   ├── maintenance
│   ├── make_schema.pl
│   └── tmp
├── sql
│   └── my.sql
├── t
│   ├── 01_root.t
│   └── 02_mech.t
├── tmpl
│   ├── include
│   │   ├── footer.tt
│   │   └── header.tt
│   └── index.tt
└── xt
    ├── 01_podspell.t
    ├── 02_perlcritic.t
    ├── 03_pod.t
    └── perlcriticrc

Amon2 のアプリケーションを起動させるには plackup コマンドを利用します。

$ plackup Hello.psgi
Plack::Server::Standalone: Accepting connections at http://0:5000/

とうちこめば、もう http://localhost:5000/ にウェブサーバーがたちあがっているはずです。 なんという簡単さ!

DB との連携

やはりウェブアプリケーションには RDBMS との連携がかかせません。 今回は RDBMS として、セットアップ不要なスタンドアロン型の SQLite をつかいましょう。 というわけで、まずは SQLite の Perl binding である DBD::SQLite をインストールしましょう。

% cpanm DBD::SQLite

sql/sqlite.sql には、とりあえず以下のような内容のスキーマを書きましょう。

DROP TABLE entry;
CREATE TABLE `entry` (
    `entry_id` integer NOT NULL primary key,
    `body`     text
);

これができたら、SQLite のデータベースにスキーマをながしこみましょう。

% sqlite3 hello.db < sql/sqlite.sql

DB の設定を config/development.sql に書きます。

+{
    'DBIx::Skinny' => {
        dsn => 'dbi:SQLite:dbname=hello.db',
        username => '',
        password => '',
    },
    ...
};

次に、DB から ORM スキーマを構築します。

% perl script/make_schema.pl

ルーターをかく

なんのサイトかもよくわかりませんが、以下のように、ディスパッチャを設定しましょう。

package Hello::Web::Dispatcher;
use Amon2::Web::Dispatcher::RouterSimple;

connect '/'              => 'Root#index';
connect '/post'          => 'Root#post';

1;

コントローラをかく

コントローラを定義しましょう。みればわかるとおりですね。

package Hello::Web::C::Root;

sub index {
    my ($class, $c) = @_;
    my @entries = $c->db->search(
        entry => {}, {limit => 10, offset => 0, order_by => {'entry_id' => 'DESC'}}
    );
    return $c->render(
        "index.tt" => {
            entries => \@entries,
        }
    );
}

sub post {
    my ($class, $c) = @_;
    if (my $body = $c->req->param('body')) {
        $c->db->insert(
            entry => {
                body => $body,
            },
        );
    }
    return $c->redirect('/');
}

テンプレート

Amon2 では現在、Text::Xslate の TTerse をデフォルトのテンプレートエンジンとして採用しています。

tmpl/index.tt という名前でファイルを配置してください。

[% INCLUDE 'include/header.tt' %]

<form method="post" action="[% uri_for('/post') %]">
    <input type="text" name="body" />
    <input type="submit" value="送信" />
</form>
<ul>
[% FOR entry IN entries %]
    <li>[% entry.entry_id %]. [% entry.body %]</li>
[% END %]
</ul>

[% INCLUDE 'include/footer.tt' %]

まとめ

このチュートリアルでは、Amon2 の基本的なつかいかたをみました。簡単だったでしょ!

SEE ALSO

DBIx::Skinny, Text::Xslate