Client-Side JavaScript Validation Bypass
Last modified: 2023-08-18
If the website checks the user input and validates them using JavaScript, we may be able to bypass this validation by adding breakpoint and modify some variable with developer tools in web browser.
Investigation
For example, assume that website modifies user input then validation check when logged in as the following JavaScript (e.g. validation.js
).
// validation.js
function validate() {
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
// Change the username input if it contains "admin" to confuse attackers.
username = username.replace(/admin/g, "noob");
// If the input value of the username/password are both "admin", we can bypass this validation.
if (username === "admin" && password === "admin") {
return true;
}
return false;
}
By the way, this example may not exist in real world because it’s too simple.
Exploitation
- In web browser, open Developer tools.
- Go to Sources tab and select target JavaScript file which checks user input.
- Add a breakpoint at the line where the validation is executed e.g. at the line
if (username === "admin"...
in the abovevalidation.js
example. - Back to the web login page, and input username/password in login form. Then submit.
- Browser stops at the breakpoint we've added.
- Go to Console tab in developer tool.
- Assign the desired value to the target variable e.g.
username = "admin"
(reassign "admin" to undo what was modified by JavaScript).