Manual:Edit token/fr

Un jeton d'édition (csrf token) est une chaîne aléatoire passée entre un client et le serveur MediaWiki lorsque vous réalisez des actions qui modifient les pages. Il est utilisé pour vérifier que l'utilisateur a bien l'intention de faire une modification, plutôt que d'être amené à demander le changement sur le wiki lors de la visite d'un site externe (par exemple cross-site request forgery).

Pourquoi c'est nécessaire
Edit tokens are used as an additional security measure when performing changes. 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. https://en.wikipedia.org/w/index.php?title=Image:Abcd.jpg&action=delete&oldimage=324242234 Following such a link would lead an administrator to unknowingly request deletion of an image. 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.

When the user actually requests the change to be done (by pressing a button or following a link), the edit token is sent back to the server. 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.

Comment ça marche ?
An edit token is a random string stored in the PHP session, which is an associative array that is stored in the server and maintained across sessions because of a cookie (e.g.,  on the English Wikipedia). Le jeton d'édition est en particulier contenu dans l'élément  de la session PHP.

Les jetons d'édition sont inclus dans les pages web à partir desquelles l'utilisateur peut demander une modification. 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.

Ce qui se trouve actuellement dans la page web n'est pas l'élément  lui-même. Mais plutôt, cet élément est concaténé dans le sel, qui est une chaîne dépendant de l'action particulière et de la page; la chaîne résultante est ensuite hachée MD5; c'est ce qui est inclus dans la page web. Lorsque l'utilisateur demande actuellement l'action, cette chaîne est renvoyée vers le serveur dans un paramètre HTTP. 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.

Validité
The edit token returned by the server can be reused multiple times for different edit operations. The token is only valid for a specific period of time. 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.

Code source
Edit tokens are mainly dealt with in the User.php source file, and in particular by the following methods.
 * getEditToken(salt) : renvoie la valeur de hachage MD5 de la concaténation de l'élément  de la session PHP et du sel. si un tel élément n'existe pas dans la session PHP, un élément aléatoire équivalent est généré. Voir la fonction getEditToken dans le dépôt.
 * 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;

Le sel
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).

An edit-token-hash generated using a salt can be used for performing additional actions only if the salt used by both server and client is the same. 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.


 * Les actions qui n'utilisent pas le sel vide par défaut sont : 


 * rollback : the salt is the title of the article (including the namespace prefix) concatenated with the name of the user whose edits are to be reverted;
 * delete the old version of an image : the salt is the  parameter (when deleting all version this parameter is the empty string, which is also the default salt);
 * Special:UserRights : le sel est le nom de l'utilisateur dont les propriétés vont être modifiées;
 * Special:Watchlist/clear : le sel est la chaîne 'clearwatchlist'

Suffixe du jeton d'édition
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.

Récupération du côté client
Dans 1.18 et supérieur, vous n'avez pas besoin de récupérer le jeton d'édition en utilisant AJAX. Il est disponible en tant que. Notez que pour votre module, vous devez avoir défini mediawiki.user comme une dépendance de ResourceLoader. 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.