Skip to content

Fix: Replace URL-based error messages with secure Flask flash messages#11

Closed
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-10
Closed

Fix: Replace URL-based error messages with secure Flask flash messages#11
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-10

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 16, 2025

Issue

Error messages were being passed through URL query parameters, making them easily manipulable by users and creating security/UX issues:

  • Security vulnerability: Users could modify URLs to display fake error messages
  • URL pollution: Long error messages made URLs messy and unprofessional
  • Message persistence: Messages stayed in URLs even after page refresh
  • No validation: No verification that message source was legitimate

Before: http://localhost:5000/create_dataset?error=データセット名を入力してください。

Solution

Replaced URL query parameter-based messaging with Flask's secure session-based flash messaging system.

After: http://localhost:5000/create_dataset (message securely stored in server-side session)

Key Changes

1. Added Session Support

app.secret_key = 'study-cards-secret-key-for-flash-messages-2024'

2. Replaced URL Parameter Messages

Before:

return redirect(url_for('create_dataset', error='データセット名を入力してください。'))

After:

flash('データセット名を入力してください。', 'error')
return redirect(url_for('create_dataset'))

3. Updated Template System

Before: Templates received message and message_type variables from routes

After: Templates use Flask's built-in get_flashed_messages(with_categories=true):

{% with messages = get_flashed_messages(with_categories=true) %}
    {% if messages %}
        {% for category, message in messages %}
            <div class="alert alert-{{ 'danger' if category == 'error' else 'success' }}">
                {{ message }}
            </div>
        {% endfor %}
    {% endif %}
{% endwith %}

Security Benefits

  • No URL manipulation: Messages stored server-side, cannot be tampered with
  • Clean URLs: Professional appearance without query parameters
  • Temporary messages: Displayed once and automatically consumed
  • Standard practice: Following Flask's recommended messaging pattern

Testing

Comprehensive testing confirmed:

  • URL parameters no longer affect displayed messages
  • All error and success scenarios work correctly
  • Messages appear exactly once and don't persist incorrectly
  • Application maintains all existing functionality
![Flash Message Security Demo](https://github.com/user-attachments/assets/79ce6969-14d7-4607-8568-1dc3d0f29e1d)

Screenshot showing secure error message display with clean URL (no query parameters)

Fixes #10.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: tak-s <20725737+tak-s@users.noreply.github.com>
Copilot AI changed the title [WIP] [Improve] エラーメッセージの扱い Fix: Replace URL-based error messages with secure Flask flash messages Jul 16, 2025
Copilot AI requested a review from tak-s July 16, 2025 10:52
Copilot finished work on behalf of tak-s July 16, 2025 10:52
@tak-s tak-s marked this pull request as ready for review July 16, 2025 11:20
Copy link
Copy Markdown
Owner

@tak-s tak-s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラーメッセージだけのために、シークレットキーを用意するのは避けたい。他の方法を検討して

…t key requirement)

Co-authored-by: tak-s <20725737+tak-s@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jul 16, 2025

エラーメッセージだけのために、シークレットキーを用意するのは避けたい。他の方法を検討して

シークレットキーを削除し、代わりに事前定義されたメッセージコードシステムに変更しました。これにより:

  • app.secret_key が不要になりました
  • ✅ URLパラメータ操作を防止(有効なコードのみ受付)
  • ✅ URLがクリーンに保たれます(短いコード使用)
  • ✅ セッション処理が不要でパフォーマンス向上

セキュリティテスト済みで、無効なメッセージコードは全て拒否されます。

コミット: 9670b32

Copilot AI requested a review from tak-s July 16, 2025 11:38
Copilot finished work on behalf of tak-s July 16, 2025 11:38
@tak-s
Copy link
Copy Markdown
Owner

tak-s commented Jul 16, 2025

不採用

@tak-s tak-s closed this Jul 16, 2025
@tak-s tak-s deleted the copilot/fix-10 branch July 16, 2025 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Improve] エラーメッセージの扱い

2 participants