それは世界で最も安全なコードです


Just the Gist

A not uncommon criticism of PHP is that it lacks in security. The sad part is, we can't argue against it. It's too broad of a criticism which can be leveraged against most languages, as most have some way a secret can be leaked. Modern PHP and modern coding practices exists to mitigate this. But today, we are going to take a look at one very basic security flaw example, and a way we could mitigate it. It's what could happen if we don't run a web server but still have our php-files public.


私たちにはindex.php パブリックフォルダにあります.このファイルは、クリスマスの日まで秘密サンタのアイデンティティを保護しています.次のようになります.
<?php 

define('SECRET_SANTA', "Olaf ⛄");

?>

<!DOCTYPE html>
<html lang="en">
<body>
    <h1>The Secret Santa is a secret!</h1>
    <p>You have to wait until Christmas Day to know who it is.</p>

    <?php 

    $today = new DateTime("now");
    $christmas = new DateTime("2021-12-25");

    if ($today >= $christmas) {
        echo "<p>The Secret Santa is " . SECRET_SANTA . "!</p>";
    } else {
        echo "<p>The Secret Santa is still a secret.</p>";
    }

    ?>
</body>
</html>
Webサーバサービスがアクティブである限り( Apacheなど).

しかし、サービスがダウンしていた場合、我々はウェブサイトへの訪問者を持って何が起こるか?PHPファイルを解析し、正しい出力を出力する代わりに、単純なテキストとしてファイルを提供します.すべてのコードが表示され、いずれかのOLAFを見ることができる!😱

では、どうやってこの出来事から身を守ることができるのでしょうか?我々は、パブリックフォルダ(またはWebルート)からの訪問者がアクセスできないフォルダに私たちの秘密を移動することができます.以下の構造を持っているといいます.
/
  |-- public-folder
  |   |-- index.php
  |   
  |-- private-folder
      |-- SecretSanta.php
私たちの訪問者はSecretSanta.php ファイルが、彼らはindex.php ファイル.だからここでどのように我々はこれを行うことができます.
SecretSantaPHP
<?php

class SecretSanta 
{
    private const SECRET_SANTA = 'Olaf ⛄';

    public static function getSecretSanta(): bool|string
    {
        if ((new DateTime("now")) >= new DateTime("2021-12-25")) {
            return self::SECRET_SANTA;
        } else {
            return false;
        }
    }
}
このクラスは秘密のサンタのアイデンティティで定数を宣言します.他のファイルにアクセスできないのでプライベートです.静的関数を使わなければならないgetSecretSanta() 秘密サンタにアクセスする.この関数は秘密のサンタを返すでしょうfalse .
に戻るindex.php ファイルには、静的関数をクラスの呼び出しによって秘密のサンタを取得することができますSecretSanta::getSecretSanta() ):
<?php 

require('../private/SecretSanta.php');

?>

<!DOCTYPE html>
<html lang="en">
<body>
    <h1>The Secret Santa is a secret!</h1>
    <p>You have to wait until Christmas Day to know who it is.</p>

    <?php 
    if (SecretSanta::getSecretSanta()) {
        echo "<p>The Secret Santa is " . SecretSanta::getSecretSanta() . "!</p>";
    } else {
        echo "<p>The Secret Santa is still a secret.</p>";
    }

    ?>
</body>
</html>
我々はまだ、Webサーバーサービスが実行されている前に、同じ出力を取得しますが、現在、我々は、秘密のサンタは、サービスがダウンしている場合、パブリックに表示されないことがわかります.これは、パブリックフォルダの外部にあなたのアプリケーションのロジックと秘密を持っているのは良い考えかもしれません.

There is so much more to keep track of when it comes to security. This article hasn't covered even a fraction of it. And many of the issues are not specific to PHP. There are Cross Origin Resource Forgery (CSRF) attacks, SQL Injection, Cross Site Scripting (XSS), and many more.



あなたはどうですか.
おそらく、あなたはPHP生態系の良いセキュリティ慣行にいくつかの良い洞察力を持って、共有してください!あなたは悪いセキュリティでプロジェクトに遭遇しましたか?PHPのプロジェクトの最初のカップルを構築するWeb開発者にとって最善のセキュリティ推奨事項は何ですか?下記のコメントとあなたが何を考えて知ってみよう✍

更なる読書
  • セキュリティマニュアルhttps://www.php.net/manual/en/security.php
  • セキュリティリスクの取り扱い方法https://www.zend.com/blog/managing-security-risks-php-engine-and-web-applications