パラメータの暗号化[Parameter encryption]

パラメータの暗号化[Why is URL encryption necessary?]

まず、なぜパラメーターの暗号化が必要なのかを説明致します。
例えば、レコードIDなど個人を特定できるデータをコントローラー側にGETやPOSTで送って何らかの処理を行う場合、フロント側のデータを書き換えることは容易です。
なので、パラメーターを暗号化することが大切なのです。

First, we will explain why parameter encryption is necessary.
For example, when personally identifiable data such as record IDs are sent to the controller side via GET or POST for some processing, it is easy to rewrite the data on the front side.
Therefore, it is important to encrypt the parameters.

パラメーターを暗号化する方法[How to encrypt URLs]

Encryption method call

パラメーターを暗号化するには、STATIC_URL_OPERATORクラスの【encryptedUrlParam】を使用します。
To encrypt parameters, use【encryptedUrlParam】 of the STATIC_URL_OPERATOR class.

[View側での暗号化の書き方]
[How to write encryption on the View side]

<a href="<?=SYSTEM_DIR?>/controller/method?i=<?=STATIC_URL_OPERATOR::encryptedUrlParam($target_id)?>">redirect</a>

※aタグ以外にもredirectUrlで送るときにも使用したりします。
※In addition to the *a tag, it is also used when sending with redirectUrl.

Compound method call

パラメーターを複合するには、STATIC_URL_OPERATORクラスの【decryptedUrlParam】を使用します。
To compound the parameter, use【decryptedUrlParam】 of the STATIC_URL_OPERATOR class.

[controller側での複合の書き方]
[How to write composite on controller side]

$targetId = STATIC_URL_OPERATOR::decryptedUrlParam($_GET['i'] ?? '');

SQLインジェクション対策
[SQL injection Countermeasures]

PDOを使用してのSQL文の書き方は以下のように記述をすれば
プリペアドステートメント設定ができる為SQLインジェクション対策も可能となります。
ただ、idiormを使用する場合は既にライブラリーに搭載されている為、意識せずに使用することが出来ます。

If you write SQL statements using PDO as follows
The SQL injection countermeasure is also possible because of the prepared statement setting.
However, when using idiorm, you can use it without being aware of it because it is already included in the library.

PDOでの書き方[How to write in PDO]

// Inserts a line

stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (name, :value)");
$stmt->bindParam(':value', 'tanaka');
$stmt->execute();

idiormでの書き方[How to write in IDIORM]

$stmt = ORM::for_table('REGISTRY')->create();
$stmt ->name= 'satou';
$stmt ->save();

XSS対策
[XSS Cross Site Scripting Countermeasures]

XSS攻撃とは何か

XSS攻撃とは、Webアプリケーションのフォームに実行可能な文字列を挿入し、
Webアプリケーション利用者の環境で悪意のある操作を実行する攻撃です。
例えば<script>〜</script>という文字列を含む内容がサニタイジングされていないWebアプリケーションに埋め込まれるように投稿された場合、
投稿内容として表示されているボタンやリンクなどをクリックするとJavaScriptなどのスクリプトが実行されます。

An XSS attack is an attack that inserts executable strings into web application forms to
This is an attack that inserts executable strings into web application forms to execute malicious operations in the web application user’s environment.
For example, if content containing the strings is submitted to be embedded in a non-sanitized web application, the
JavaScript or other scripts will be executed when a button, link, etc. displayed as the posted content is clicked.

XSS攻撃の対策するために

XSS対策として、[htmlspecialchars]を使用します。
htmlspecialchars]の引数に文字列を入力すると、その文字列がHTMLエンティティに変換されます。

Use [htmlspecialchars] as a countermeasure against XSS cross-site scripting.
By entering a string as an argument to [htmlspecialchars], the string is converted into an HTML entity.

変換前[Before conversion] 変換後[After conversion]
( ‘ )
&#039;
( ” )
&quot;
( & )
&amp;
( < )
&lt;
( > )
&gt;
構文:htmlspecialchars(変換する文字列,オプション);

※ENT_QUOTESを使用することで、ダブルコーテーション、シングルコーテーションも変換することが出来る。
Double and single citations can also be converted by using *ENT_QUOTES.


//For receipt by GET and POST
<?php echo htmlspecialchars($_GET['name'], ENT_QUOTES); ?>

//When used for a tag
<a href="detail.php<?php echo htmlspecialchars($url, ENT_QUOTES); ?>">リンク</a>

//input type hidden
<input type="hidden" name="name" value="<?php echo htmlspecialchars($name, ENT_QUOTES); ?>">

//data attribute
<div data-targetId="<?php echo htmlspecialchars($targetId, ENT_QUOTES); ?>"></div>

CSRF対策(クロスサイト・リクエスト・フォージェリ)
[CSRF countermeasures (Cross Site Request Forgery)]

CSRF攻撃とは何か[What is a CSRF attack?]

CSRF攻撃とは、利用中のWEBサービスを利用している時に、ログアウトをしないで悪意のある外部のURLにアクセスをして
利用中のWEBアプリケーションにユーザーの意図しないリクエストを投げてしまう攻撃になります。
※被害例としては、個人情報の変更、SNSの乗っ取りなどがあります。

A CSRF attack is an attack in which a user accesses a malicious external URL without logging out while using the web service in use and
This is an attack that throws an unintended request to the web service being used by the user without logging out.
*Examples of damage include modification of personal information and hijacking of social networking services.

CSRF攻撃を対策するために[To counter CSRF attacks]

CSRF対策としては、送信前にランダム文字列を作成してセッションとinput type hiddenに値をセットします。
そして、controller側でセッションに入っているランダム文字列とhiddenの中身を比較して内容が正しくない場合はエラーとなる仕組みとなっております。
そうすることで、攻撃者のページでは、トークンをあなたのサイトのセッションに入れることができないため、
上記のチェックでエラーとなり、攻撃を阻止できます。

As a CSRF countermeasure, a random string is created and set in the session and input type hidden before sending.
Then, the controller compares the random string in the session with the contents of hidden, and if the contents are incorrect, an error is generated.
This way, the attacker’s page will not be able to put the token into your site’s session.
The above check will result in an error, thus thwarting the attack.

customer-list.php

//Token Generation
$token = bin2hex(openssl_random_pseudo_bytes(16));
$_SESSION['token'] = $token;
?>
<form action="post.php" method="post">
	<input type="hidden" name="token" value="<?php echo $token; ?>">
	<label>title</label>
	<input type="text" name="title">
	<label>body</label>
	<textarea name="content"></textarea>
	<button type="submit">posting</button>
</form>

customer-form.php

// token check
if (empty($_POST['token'])|| empty($_SESSION['token'])|| $_POST['token'] !== $_SESSION['token']) {
    throw new Exception('token mismatched');
}

//get input value
$title = $_POST['title'] ?? null;
$content = $_POST['content'] ?? null;

//Input value check
if (!$title || !$content) {
    throw new Exception('invalid input');
}

//Store in DB
$custmanageObj = ORM::for_table('customer')->create();
$custmanageObj->title = $title ;
$custmanageObj->content = $content ;
$custmanageObj->save();