Manual:Edit token/zh

编辑令牌（亦為 csrf 令牌）是一个Mediawiki服务器生成的随机字符串. 客户端获得此编辑令牌后才可以编辑页面. 编辑令牌用于确保用户真的想编辑页面，而不是误点击某个外部链接而不自觉地编辑了什么页面. 另见cross-site request forgery. It is used to check that the user really intended to make the change, rather than being tricked into requesting the change on the wiki while visiting an external site (i.e. cross-site request forgery).

為什麼需要編輯令牌
編輯令牌用作執行更改時的額外安全措施. 如果僅使用cookie檢查用戶身份，則外部站點可以使用連結（類似以下的鏈接）來讓訪問者更改維基. If the user identity were checked using cookies only, an external site could use a link like the following one to have visitors perform changes to the wiki.

按照這樣的鏈接操作後，將導致管理員在不知不覺中請求刪除圖像. 如果管理員仍然登錄，服務器將檢查cookie並准許請求. If the administrator is still logged in, the server would check the cookies and grant the request.

For this reason, actions that perform changes require an additional piece of data that is passed as an HTTP parameter, the edit token. An edit token is embedded into web pages from which the user can request a change; this includes the edit form (where one can change a page by pressing "") but also the image description pages (where an administrator can request deletion of an old version of an image), contributor histories (where administrators can rollback), etc.

當用戶實際請求完成其更改時（即通過按下按鈕或跟隨鏈接），編輯令牌將被發送回服務器. 這將向服務器證明用戶已直接從站點請求更改，而不是從外部站點請求更改（因為外部站點無權訪問用戶的編輯令牌）. This proves to the server that the user has requested the change directly from the site and not from an external site, as external sites do not have access to the edit tokens of the user.

工作原理
編輯令牌是存儲在PHP會話中的隨機字符串，其是存儲在服務器中並且由於cookie（例如：英語維基百科上的 ），而跨會話維護的關聯數組. 編輯令牌存在於PHP會話的 元素之中. The edit token is in particular contained in the  element of the PHP session.

Edit tokens are embedded into web pages from where the user can request a change. When such a page is to be generated, the edit token is retrieved from the  element of the PHP session, if such an element exists; otherwise, a random string is generated and stored in that element.

What is actually embedded into the web page is not the  element itself. Rather, this element is concatenated to the salt, which is a string that depends on the particular action and page; the resulting string is then MD5-hashed; this is what is embedded in the web page. When the user actually requests the action, this string is sent back to the server via an HTTP parameter. The server can then check the correctness of this parameter: it repeats the procedure used to generate it from the PHP session and checks if the result is equal to the parameter.

過期
服務器返回的編輯令牌可以多次重複使用，以進行不同的編輯操作. 令牌僅在特定時間段內有效. 帶有過時令牌的「API調用」將返回「badtoken」的錯誤； 在這種情況下，必須在重試操作之前，從服務器獲取新的編輯令牌. API call with a stale token will return a badtoken error. In this case it is necessary to get a new edit token from the server before retrying the operation.

源代碼
Edit tokens are mainly dealt with in the User.php source file, and in particular by the following methods.
 * getEditToken(salt) : returns the MD5 hash of the concatenation of the  element of the PHP session with the salt. If such an element does not exist in the PHP session, a random one is generated. See getEditToken function in repository.
 * matchEditToken(token, salt) : checks whether its first argument is a valid edit token with respect to the salt; this is done by repeating the procedure of generation and then comparing the result with the first argument; in particular, this function calls  and then compares the result with the first argument;

Salt
The default salt is the empty string; most actions use this default value. As a result, an edit token string received from a server to perform an initial action on a page can also be used to perform additional actions on other pages. However, since an edit token is stored in the PHP session, it can be used only as long as the session is kept in the server and the client has the corresponding session token cookie (e.g., the enwiki_session cookie).

僅當服務器和客戶端使用的「salt」相同時，使用「salt」生成的「edit-token-hash」才能用於執行其他操作； 因此，如果「salt」僅嵌入在執行初始操作的頁面中，則不能使用相同的「edit-token-hash」來啟用對其他頁面的操作. Therefore it follows that if the salt is only embedded in the page where the initial action is performed, then that same edit-token-hash cannot be used to enable actions on additional pages.


 * 不使用默認空「salt」的操作是:


 * rollback : salt是文章的標題（包括名稱空間前綴）與要還原其編輯的用戶名的連接；
 * 刪除舊版本的圖像 : salt是


 * delete the old version of an image : the salt is the

參數（當刪除所有版本的此參數為空字符串時，也是默認的salt）;
 * Specia:UserRights : 此處，salt是要更改其屬性的用戶的用戶名；
 * Special:Watchlist/clear : 此處，salt是字符串


 * Special:UserRights : the salt is the username of the user whose properties are to be changed;
 * Special:Watchlist/clear : the salt is the string 'clearwatchlist'

編輯令牌後綴
Edit tokens end with  to prevent broken proxies from editing: proxies that cannot correctly handle the backslash or plus sign typically also mess up the wiki markup code.

檢索客戶端
在1.18及其更高版本中，您不需要使用AJAX檢索編輯令牌，可使用. 但請注意，您需要將mediawiki.user作為模塊的ResourceLoader依賴項. 建議您使用$code2幫助程序的方法，該方法會在加載網頁後令牌已過期時，自動重試並處理. Note that you need to have defined mediawiki.user as a ResourceLoader dependency for your module. It is recommended that you use the  helper method, which automatically takes care of retries if the token has expired since the web page was loaded.