How to have id security exposed in links – PHP + Javascript

Question:

PHP language.

I have a grid where I have edit buttons on each line and delete that record with actions in javascript passing the id of that record to perform the action. My problem is related to security since there may be a nasty user that changes the id of that button and accesses improper data or deletes it. How to do it in a safe way?

Example of how it's being done:

Part rendered in HTML exits +/- like this (example of what would exit the grid):

 <td>Dados1</td>
 <td>Dados2</td>
 <td><img src="editar.png" onclick="editar(1)"><img src="excluir.png"> onclick="excluir(1)"></td>
</tr>
<tr>
 <td>Dados3</td>
 <td>Dados4</td>
 <td><img src="editar.png" onclick="editar(2)"><img src="excluir.png"> onclick="excluir(2)"></td>
</tr>

e no javascript

function editar(id) {
   // chamada em ajax pra controller passando o id via post
}

function excluir(id) {
   // chamada em ajax pra controller passando o id via post
}

any suggestions on how to do it safely without exposing the id to the user?

Answer:

Good afternoon friend. I recently had a similar dilemma. following your code example I solved it using the cryptoLib class available at http://cryptolib.ju.je

  1. First we create the functions

 require("path/to/cryptolib.php");

 function tokenizer($id){
 $token = CryptoLib::encryptData($id, "token");
 return $token;
 }

 // Função simples para decodificar o token recebido após click no elemento

 function decodeToken($token){
 $decryptedString = CryptoLib::decryptData($token, "token");
 return $decryptedString;
 }

EXEMPLO: No seu html o código pode ser aplicado da seguinte forma

<td>Dados1</td>
<td>Dados2</td>
<td><img src="e.png" onclick="editar(<? echo tokenizer($row["id"]) ?>)">
<img src="del.png"> onclick="excluir(<? echo tokenizer($row["id"]) ?>)">
</td>
</tr>
<tr>
<td>Dados3</td>
<td>Dados4</td>
<td>
<img src="e.png" onclick="editar(<? echo tokenizer($row["id"]) ?>)">
<img src="del.png"> onclick="excluir(<? echo tokenizer($row["id"]) ?>)">
</td>
</tr>

HTML OUTPUT: No seu html renderizado o código fonte será similar ao exemplo abaixo:

<td>Dados1</td>
<td>Dados2</td>
<td><img src="e.png" onclick="editar("ctnbCH1FXdr41JYI9J82sXjGKbFvIUP3pshgL8KaZsYsEFqFco3NgimLy2xAj")">
<img src="del.png"> onclick="excluir("gimLy2xfUP3pshgL8KaZsYsEFqFco3NAjctnbCH1FXdr41JYI9J82sXjGKbFv")">
</td>
</tr>
<tr>
<td>Dados3</td>
<td>Dados4</td>
<td>
<img src="e.png" onclick="editar("IUPIUP3pshgL8KaZsYsEFqFco3NgimLy2xAj3pshgL8KaZsYsEFqFco3NgimLy2xAj")">
<img src="del.png"> onclick="excluir("8KaZsYsEFqFco3NgctnbCH1FXdr41JYI9J82sXjGKbFvIUP3pshgLimLy2xAj")">
</td>
</tr>

No seu CRUD em PHP. Utilize a função de validação do token:

$id = decodeToken($token);

A função retorna o id para usar na manipulação do CRUD.

Conclusão:

Ao chamar a função

tokenizer($row["id"]);
// O valor retornado para token
sXjGKbFvIUP3pshgLim8KaZsYsEFqFco3NgctnbCH1FXdr41JYI9J82Ly2xAj

Ao receber o token, enviado pelo javascript, chama a função

$id = decodeToken($token);
// se o valor do $row["id"] informado para a função tokenizer() for igual a "99".
// a função retornará o equivalente a:
$id = 99;

Vantagens:

Toda vez que as páginas são renderizadas, o token gerado para cada $row{“id”] é único e aleatório.

Espero que isso ajude a sanar suas dúvidas.

Scroll to Top