LoginSignup
0
0

Laravelは強力なセキュリティ機能とテストツールを提供しており、アプリケーションの保護と品質保証を簡単に実現できます。今回は、セキュリティの強化方法とテストの自動化について記述します。

目次

  1. セキュリティの強化
  2. テストの自動化
  3. 実践プロジェクトの例

1. セキュリティの強化

CSRF(クロスサイトリクエストフォージェリ)対策

LaravelはデフォルトでCSRF保護を提供しています。すべてのPOST、PUT、PATCH、およびDELETEリクエストに対してCSRFトークンを検証します。

フォームにCSRFトークンを含める方法:

<form action="/example" method="POST">
    @csrf
    <!-- フォームフィールド -->
    <button type="submit">Submit</button>
</form>

入力バリデーション

入力データのバリデーションは、SQLインジェクションやXSS(クロスサイトスクリプティング)攻撃を防ぐために重要です。Laravelのバリデーション機能を使用して、入力データの検証を行います。

$request->validate([
    'name' => 'required|string|max:255',
    'email' => 'required|string|email|max:255|unique:users',
    'password' => 'required|string|min:8|confirmed',
]);

パスワードのハッシュ化

ユーザーパスワードは、必ずハッシュ化して保存します。Laravelのbcrypt関数を使用します。

use Illuminate\Support\Facades\Hash;

$user->password = Hash::make($request->password);
$user->save();

認証と認可

Laravelは認証と認可のための強力なツールを提供しています。たとえば、認証ミドルウェアを使用して、認証されたユーザーのみが特定のルートにアクセスできるようにします。

routes/web.php

Route::get('/dashboard', function () {
    // ダッシュボードロジック
})->middleware('auth');

ポリシーを使用して、リソースごとの権限を管理します。

php artisan make:policy PostPolicy --model=Post

app/Policies/PostPolicy.php

public function update(User $user, Post $post)
{
    return $user->id === $post->user_id;
}

ポリシーをコントローラーで使用:

$this->authorize('update', $post);

2. テストの自動化

ユニットテスト

LaravelはPHPUnitを使用してユニットテストを作成および実行できます。テストクラスはtests/Unitディレクトリに配置されます。

ユニットテストの作成例:

php artisan make:test ExampleTest

tests/Unit/ExampleTest.php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
}

フィーチャーテスト

フィーチャーテストは、アプリケーションの機能全体をテストします。テストクラスはtests/Featureディレクトリに配置されます。

フィーチャーテストの作成例:

php artisan make:test UserTest --unit

tests/Feature/UserTest.php

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

class UserTest extends TestCase
{
    use RefreshDatabase;

    public function testUserCanBeCreated()
    {
        $response = $this->post('/register', [
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

        $response->assertStatus(302);
        $this->assertDatabaseHas('users', ['email' => 'test@example.com']);
    }
}

ブラウザテスト

Laravel Duskを使用して、ブラウザテストを自動化できます。

Duskのインストール:

composer require --dev laravel/dusk
php artisan dusk:install

ブラウザテストの作成例:

php artisan dusk:make LoginTest

tests/Browser/LoginTest.php

namespace Tests\Browser;

use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class LoginTest extends DuskTestCase
{
    public function testLogin()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/login')
                    ->type('email', 'user@example.com')
                    ->type('password', 'password')
                    ->press('Login')
                    ->assertPathIs('/home');
        });
    }
}

3. 実践プロジェクトの例

プロジェクト:セキュリティ強化とテストの自動化を備えたユーザー管理

  1. ルートと認証ミドルウェアの設定

routes/web.php

Route::get('/dashboard', [DashboardController::class, 'index'])->middleware('auth');
  1. ユーザー登録と認証

app/Http/Controllers/Auth/RegisterController.php

use Illuminate\Support\Facades\Hash;
use App\Models\User;
use Illuminate\Http\Request;

class RegisterController extends Controller
{
    public function register(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:8|confirmed',
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        auth()->login($user);

        return redirect('/dashboard');
    }
}
  1. テストの追加

tests/Feature/RegisterTest.php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class RegisterTest extends TestCase
{
    use RefreshDatabase;

    public function testUserRegistration()
    {
        $response = $this->post('/register', [
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

        $response->assertStatus(302);
        $this->assertDatabaseHas('users', ['email' => 'test@example.com']);
    }
}

まとめ

この記事では、Laravelでのセキュリティ強化とテストの自動化について記述しました。CSRF保護や入力バリデーション、認証と認可を活用してアプリケーションのセキュリティを強化し、ユニットテストやフィーチャーテスト、ブラウザテストを導入することでアプリケーションの品質を保証する方法を学びました。これらのテクニックを活用して、より安全で信頼性の高いアプリケーションの開発を目指します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0