ivan tsai
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # CSRF保護機制 [CSRF](https://zh.wikipedia.org/wiki/%E8%B7%A8%E7%AB%99%E8%AF%B7%E6%B1%82%E4%BC%AA%E9%80%A0) - Cross-site request forgery - 是一種惡意使用者試圖讓你的合法用戶在不知不覺中提交他們不打算提交的資料的方法。 CSRF保護的工作原理是在表單中添加一個隱藏字段,其中包含只有您和您的用戶知道的值。 這確保了用戶 - 而不是其他某個entity - 正在提交特定的資料。 在使用 CSRF 保護之前,請在您的應用中安裝它: ``` composer require symfony/security-csrf ``` 然後,使用```csrf_protection```選項啟用/禁用 CSRF 保護( 有關更多資訊,請參閱[CSRF配置](https://symfony.com/doc/current/reference/configuration/framework.html#reference-framework-csrf-protection)): ``` # config/packages/framework.yaml framework: # ... csrf_protection: ~ ``` 用於 CSRF 保護的token對於每個用戶都是不同的,它們存儲在session中。 這就是為什麼在您呈現具有 CSRF 保護的表單時會自動啟動session的原因。 此外,這意味著您無法完全cachee包含 CSRF 保護表單的頁面。作為替代方案,您可以: - 將表單嵌入 uncached 的 [ESI片段](https://symfony.com/doc/current/http_cache/esi.html)並cache頁面的其餘內容; - cache整個頁面並通過uncached 的 AJAX 請求load表單; - cache整個頁面並使用[hinclude.js](https://symfony.com/doc/current/templating/hinclude.html)來load帶有uncached的 AJAX 請求的 CSRF token,並用它替換表單的field值。 ## Symfony 形式的 CSRF 保護 使用 Symfony Form 組件創建的表單預設包含 CSRF token,Symfony 會自動檢查它們,因此您無需採取任何措施來防止 CSRF 攻擊。 預設情況下,Symfony 在名為```_name```的隱藏field中添加 CSRF token,但這可以在每個表單上進行自定義: ```php= // src/Form/TaskType.php namespace App\Form; // ... use App\Entity\Task; use Symfony\Component\OptionsResolver\OptionsResolver; class TaskType extends AbstractType { // ... public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'data_class' => Task::class, // enable/disable CSRF protection for this form 'csrf_protection' => true, // the name of the hidden HTML field that stores the token 'csrf_field_name' => '_token', // an arbitrary string used to generate the value of the token // using a different string for each form improves its security 'csrf_token_id' => 'task_item', ]); } // ... } ``` 您還可以自定義 CSRF 表單field的呈現,創建自定義[表單主題](https://symfony.com/doc/current/form/form_themes.html)並將```csrf_token```用作field的前綴(例如定義```{% block csrf_token_widget %} ... {% endblock %}```以自定義整個表單field內容)。 ## 登錄表單中的 CSRF 保護 有關受 CSRF 攻擊保護的登錄表單,請參閱[安全](https://symfony.com/doc/current/security.html#form_login-csrf)。 您還可以為[logout action](https://symfony.com/doc/current/reference/configuration/security.html#reference-security-logout-csrf)配置 CSRF 保護。 ## 手動生成和檢查 CSRF token 儘管 Symfony 表單預設自動提供 CSRF 保護,但您可能需要手動生成和檢查 CSRF token,例如在使用不受 Symfony 表單組件管理的常規 HTML 表單時。 考慮創建一個允許刪除項目的 HTML 表單。首先,使用 ```csrf_token() ```Twig 函數在模板中生成 CSRF token並將其存儲為隱藏表單field: ```php= <form action="{{ url('admin_post_delete', { id: post.id }) }}" method="post"> {# the argument of csrf_token() is an arbitrary string used to generate the token #} <input type="hidden" name="token" value="{{ csrf_token('delete-item') }}"/> <button type="submit">Delete item</button> </form> ``` 然後,獲取控制器action中CSRF token的值,並使用 ```isCsrfTokenValid()```方法檢查其有效性: ```php= use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; // ... public function delete(Request $request): Response { $submittedToken = $request->request->get('token'); // 'delete-item' is the same value used in the template to generate the token if ($this->isCsrfTokenValid('delete-item', $submittedToken)) { // ... do something, like deleting an object } } ``` ## CSRF token 和 Compression Side-Channel攻擊 [BREACH](https://en.wikipedia.org/wiki/BREACH)和[CRIME](https://zh.wikipedia.org/wiki/CRIME)是使用 HTTP compression時針對 HTTPS 的安全漏洞。 攻擊者可以利用壓縮洩漏的信息來恢復明文的目標部分。 為了減輕這些攻擊,並防止攻擊者猜測 CSRF token,隨機mask被添加到token並用於對其進行干擾。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully