PHP Type Juggling: OTP Bypass to CRM Takeover
The password reset endpoint compared the submitted OTP using PHP's loose comparison, so sending a JSON boolean true or false bypassed the OTP check. An attacker could change the password of the account tied to their session without knowing the OTP.
That single discrepancy turned a one-line finding into a much longer investigation.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
The target was a CRM platform, tested during an authorised VAPT engagement. The feature under examination was the password reset flow, which is protected by a one-time password (OTP). The endpoint is:
POST /api/forget-pass
It requires two parameters: user_id and otp. On paper this is a reasonable design: the caller identifies the account and supplies the OTP that was sent to that account's owner.
Setup
The work was carried out under an authorised VAPT engagement against the CRM platform. The report does not describe the tooling used, the PHP version, the framework, or the database engine. What it does establish is the shape of the request: a JSON body carrying user_id and otp, sent to /api/forget-pass.
Finding it
The researcher began by sending a JSON boolean in place of the numeric OTP. The first test used "otp": true. The server answered Password Updated !!. That response was unexpected for a value that is not a valid OTP, and it is what turned a one-line observation into a longer investigation.
The reasoning behind the success is PHP's loose comparison. JSON true decodes to PHP bool(true), and PHP's == coerces that boolean against any non-zero integer. Because the stored OTP was an integer, the comparison passed. No brute force and no OTP interception were needed: a single character in the JSON body was enough.
Exploitation
Step 1: otp as true
The first request sent a JSON boolean instead of a numeric OTP:
"otp": true
The server replied Password Updated !!. The important part is the type: true is not a number and not a string, but PHP's loose comparison coerces it to match any non-zero integer, so the stored integer OTP satisfied the check.
Step 2: otp as false against a different account
The second test used the same endpoint with user_id: 2 and otp: false:
"otp": false
The server again replied Password Updated !!. This time, however, the response body showed that the modified account was user_id: 1, not the user_id: 2 that had been supplied in the request.

Step 3: tracing the discrepancy
false == 0 is true in PHP. The account at user_id: 1 had an OTP stored as 0, the database column's default value, because no reset had ever been requested for it. The comparison false == 0 therefore passed silently.
Separately, the password write resolved from session context rather than from the user_id in the request body. The body was used for the OTP lookup; the actual write went to whichever user the session held. Two separate problems were exposed by a single request.
Root cause
PHP's == silently coerces types before comparing. The zero default is the silent enabler: many ORM frameworks initialise unset OTP columns to 0 when there is no NOT NULL constraint and no prior value. That is the bypass surface.
The design compounded the comparison flaw. The OTP lookup used the user_id from the request body, while the password write used the session context, so the account checked and the account changed could differ.
Impact
With no brute force and no phishing, a single JSON boolean was enough to drive the impact chain. In the case observed, supplying otp: false with user_id: 2 changed the password of the account bound to the session, user_id: 1, whose OTP column held the default 0. The report does not describe the wider outcome beyond the two accounts used in testing.
Fix
The report lists three fixes, in order of importance:
- Strict comparison:
===instead of==. - Input validation:
'otp' => 'required|integer|digits:6'. - Database:
NOT NULLon the OTP column with no default zero.
A single validation rule would have blocked every variant tested. Defence in depth means not relying on any one of these alone.
Variants to test
- Send
"otp": trueagainst a reset endpoint. A positive result is a success response for a boolean OTP value. - Send
"otp": falseand observe which account is modified. A positive result is a password change on an account other than the one named in the body, particularly one whose OTP column still holds its default. - Compare the
user_idin the body with the account actually modified in the response, on every reset flow you test. A mismatch is the signal that lookup and write resolve from different sources. - Inspect the OTP column definition in the schema. A nullable column with a zero default is the precondition for the
false == 0path.
Scanner output showed nothing unusual on this target. Finding it required knowing PHP's coercion rules, systematically iterating non-integer OTP types, noticing that the response showed the wrong account, tracing session versus request-body resolution, and connecting database column defaults to the bypass condition. No tool surfaced it; reasoning did.
When a language has forty-seven loose-comparison edge cases, those edge cases will show up in security controls, especially ones written quickly, under deadline, by developers focused on the happy path. Type juggling is not exotic: it is a regular consequence of mixing dynamic typing with integer-based validation, and it appears in production far more often than it should.
What happened, in plain words
A security tester was hired to try to break a company's customer management system, with permission. That kind of paid, permission-based testing is called a VAPT (Vulnerability Assessment and Penetration Test). The feature he looked at was the "forgot my password" flow, the one where you click a link, get a short code, type it in, and set a new password.
That flow is supposed to be protected by a one-time password (OTP): a short code, usually a handful of digits, that is sent to you and can only be used once. The idea is that only the person who owns the account can read the code and finish the reset.
The tester found that he could skip the code entirely. He sent the word true where the code should go, and the server said the password had been updated. Then he sent false, and the server again said the password had been updated, but this time it changed a different account from the one he had named in his request.
That second detail, the wrong account being changed, is what turned a small observation into a long investigation. This article walks through the whole thing from zero: what each piece of the system is, what was sent, why it worked, and how to stop it.
The pieces you need to know first
Before the steps make sense, here are the basic building blocks, explained as if you have only ever used a browser.
- A web application is a program that runs on a company's server and that you talk to through your browser. When you log in to a website, your browser sends your username and password to that program, and the program decides whether to let you in.
- An API (Application Programming Interface) is the part of a web application that accepts requests in a structured, machine-friendly way, rather than returning a web page designed for a human to look at. Think of a restaurant: the dining room is the website you see, and the kitchen door where waiters hand in written orders is the API. The kitchen does not care whether the order is written neatly; it just follows what is on the slip. That is exactly why the shape of what you send matters so much.
- An endpoint is one specific address inside an API, the equivalent of one particular counter in a large office. Each endpoint does one job. The one in this story handles "forgot password".
- HTTP is the language browsers and servers use to exchange requests and responses. A request has a method (such as
POST, meaning "here is some data, please act on it"), a path (the endpoint), and a body (the data itself). - JSON is a simple text format for that data. It looks like
{"name": "value"}. Values can be text in quotes (a string), a number without quotes (an integer), or the special wordstrueandfalse(a boolean, meaning yes/no). - A parameter is one named field inside that data, such as
user_idorotp. - A session is how a website remembers who you are between requests. When you log in, the server gives your browser a small piece of identifying data, often stored in a cookie, and your browser sends it back with every subsequent request. The server looks it up and knows "this is the person who logged in as X". It is like a cloakroom ticket: the ticket itself means nothing, but the attendant matches it to the coat you left.
- A database is where the application stores its records: users, passwords, and in this case the OTP codes. A database table is like a spreadsheet, with columns (fields) and rows (records).
- A default value is what a column is automatically filled with when nobody has supplied anything. If a column's default is
0, then every new row starts with a0in that column until something overwrites it. - PHP is a programming language very widely used for websites. It is dynamically typed, which means a variable does not have a fixed type; the same value can be treated as text in one place and as a number in another.
- Type juggling (or type coercion) is PHP's habit of quietly converting one type into another when you compare or combine them. If you compare the boolean
truewith the number5, PHP does not refuse; it converts one side so the comparison can happen. This is convenient for programmers and dangerous for security checks. - Loose comparison is PHP's
==operator, which performs that conversion before comparing. Strict comparison is===, which also checks that both sides are the same type, sotrue === 1is false. - An ORM (Object-Relational Mapper) is a helper library that lets programmers work with database tables as if they were ordinary code objects. Many ORMs fill in defaults for fields you have not set.
- Brute force means guessing a code by trying every possibility, one after another. Phishing means tricking a person into handing over their code or password. Neither was needed here, which is what makes the finding serious.
The setup
The target was a CRM platform. CRM stands for Customer Relationship Management: software a company uses to keep track of its customers, sales leads, support tickets and contacts. It typically holds a lot of personal and business data, which is why taking one over matters.
The feature under examination was the password reset flow, protected by an OTP. The endpoint is:
POST /api/forget-pass
Line by line: POST is the method, meaning "I am sending data and asking you to act on it". /api/forget-pass is the path, the specific counter in the API that handles forgotten passwords. Together they say: "at this address, please process a password reset for me".
It requires two parameters: user_id and otp. user_id is the number identifying which account is being reset. otp is the code that was supposedly sent to that account's owner.
On paper this is a reasonable design: the caller says which account they want, and proves they own it by supplying the code that only the owner should have received. The tester's job was to check whether the server actually enforced that proof.
Finding it
The tester began by sending a JSON boolean in place of the numeric OTP. In other words, instead of typing a six-digit code, he typed the word true.
The tester sent a JSON boolean in place of the numeric OTP. If the server's check is written with ==, then a boolean will be converted to a number before the comparison, and the result may not be what the programmer intended. Testing the type of the input, rather than its value, is a standard first move against any check that expects a number.
The first test used "otp": true. The server answered Password Updated !!. That response was unexpected for a value that is not a valid OTP, and it is what turned a one-line observation into a longer investigation.
The reasoning behind the success is PHP's loose comparison. JSON true decodes to PHP bool(true), and PHP's == coerces that boolean against any non-zero integer. Because the stored OTP was an integer, the comparison passed. No brute force and no OTP interception were needed: a single character in the JSON body was enough.
In plain words: the server had a number stored as the correct code. The tester sent the word "yes". PHP converted "yes" into a number, and any non-zero number counted as a match. The check that was supposed to prove ownership proved nothing.
Exploitation
Step 1: otp as true
The first request sent a JSON boolean instead of a numeric OTP:
"otp": true
This is one field of the request body. The name is otp, and the value is the boolean true, written without quotes because it is not text. A normal request would have had something like "otp": 482913 here, a number. The tester deliberately changed the type, not the digits.
The server replied Password Updated !!. The important part is the type: true is not a number and not a string, but PHP's loose comparison coerces it to match any non-zero integer, so the stored integer OTP satisfied the check.
Why does this matter beyond one account? Because it means the OTP check can be passed without knowing the code at all. The whole security of the reset flow rests on that check, and the check can be satisfied by a value that carries no secret information whatsoever.
Step 2: otp as false against a different account
The second test used the same endpoint with user_id: 2 and otp: false:
"otp": false
Again, this is a single field. The value is the boolean false, again unquoted. The tester also set user_id to 2, meaning "please reset the account numbered 2".
The server again replied Password Updated !!. This time, however, the response body showed that the modified account was user_id: 1, not the user_id: 2 that had been supplied in the request.

That mismatch is the heart of the story. The tester asked to change account 2 and the system changed account 1. Two things had gone wrong at once, and the response revealed both.
Step 3: tracing the discrepancy
false == 0 is true in PHP. When PHP compares the boolean false with the number 0 using ==, it converts false to 0, and 0 equals 0. The account at user_id: 1 had an OTP stored as 0, the database column's default value, because no reset had ever been requested for it. The comparison false == 0 therefore passed silently.
Think of it like a cloakroom that checks your ticket by asking "is the number on your ticket zero?". Anyone holding a blank ticket, which reads as zero, gets a coat. Account 1 had never had a reset requested, so its OTP field still held the blank value, the default 0. Sending false matched that blank.
Separately, the password write resolved from session context rather than from the user_id in the request body. The body was used for the OTP lookup; the actual write went to whichever user the session held. Two separate problems were exposed by a single request.
In everyday terms: the bouncer checked the invitation against the name you wrote on the form, but the doorman let in whoever was already standing at the front of the queue, the person whose session was active. The name on the form and the person who walked in were decided by two different parts of the system, and nobody compared them.
This is why the tester did not stop at the first success. A response saying "password updated" is only interesting if you check whose password was updated. Comparing the account named in the request with the account actually changed is what exposed the second flaw.
Root cause
PHP's == silently coerces types before comparing. The zero default is the silent enabler: many ORM frameworks initialise unset OTP columns to 0 when there is no NOT NULL constraint and no prior value. That is the bypass surface.
Unpacking that: a database column can be declared NOT NULL, meaning "this field must always contain a real value, never blank". If the column is not declared that way, and the programmer has not set a value, the database or the ORM fills in a default, here 0. So every account that has never asked for a password reset has 0 sitting in its OTP field. That 0 is not a secret. It is the absence of a secret, and false compares equal to it.
The design compounded the comparison flaw. The OTP lookup used the user_id from the request body, while the password write used the session context, so the account checked and the account changed could differ.
Put simply, there were two independent mistakes:
- The comparison accepted a value that was never a valid code.
- The account checked and the account changed were chosen from two different sources, so they could disagree.
Either one alone would be a bug. Together they let a request name one account for the check and change another for the write.
Impact
With no brute force and no phishing, a single JSON boolean was enough to drive the impact chain. In the case observed, supplying otp: false with user_id: 2 changed the password of the account bound to the session, user_id: 1, whose OTP column held the default 0. The report does not describe the wider outcome beyond the two accounts used in testing.
Why is changing a password such a big deal? Because the password is the key to the account. If an attacker can set a new password on an account, they can log in as that user, read and change everything that user can see, and lock the real owner out. In a CRM, that means customer records, contact details and business data. That is what "takeover" means here: not breaking the encryption, just walking in through the front door with a key the system handed over.
Note what was not needed: no guessing of codes, no tricking an employee, no malware. No guessing of codes, no tricking an employee and no malware were needed in the request that was sent.
Fix
The report lists three fixes, in order of importance:
- Strict comparison:
===instead of==. - Input validation:
'otp' => 'required|integer|digits:6'. - Database:
NOT NULLon the OTP column with no default zero.
Taking them one at a time:
- Strict comparison.
===checks both value and type.false === 0is false, because one is a boolean and the other an integer. With===, the boolean tricks in this report stop working, because the types no longer get converted behind the programmer's back. - Input validation. The rule
'otp' => 'required|integer|digits:6'says: the field must be present, must be an integer, and must be exactly six digits. A boolean is not an integer, so the request is rejected before it ever reaches the comparison. This is the single most effective line of defence, because it stops the wrong type at the door. - Database. Declaring the OTP column
NOT NULLwith no default zero means there is no blank value forfalseto match. An account with no pending reset simply has no OTP, rather than a0that looks like one.
A single validation rule would have blocked every variant tested. Defence in depth means not relying on any one of these alone. The report does not state which of these were actually implemented.
Defence in depth is the idea of layering protections, like a house with a locked gate, a locked door and a safe inside. Any one might fail; all three failing at once is much less likely.
Variants to test
If you ever test a reset flow yourself, these are the variations worth trying. Each one is a small change to the same request, and each one probes a different assumption the programmer may have made.
- Send
"otp": trueagainst a reset endpoint. A positive result is a success response for a boolean OTP value. - Send
"otp": falseand observe which account is modified. A positive result is a password change on an account other than the one named in the body, particularly one whose OTP column still holds its default. - Compare the
user_idin the body with the account actually modified in the response, on every reset flow you test. A mismatch is the signal that lookup and write resolve from different sources. - Inspect the OTP column definition in the schema. A nullable column with a zero default is the precondition for the
false == 0path.
Scanner output showed nothing unusual on this target. Finding it required knowing PHP's coercion rules, systematically iterating non-integer OTP types, noticing that the response showed the wrong account, tracing session versus request-body resolution, and connecting database column defaults to the bypass condition. No tool surfaced it; reasoning did.
That last point is worth dwelling on. A scanner is an automated program that sends a fixed list of known-bad inputs and looks for known-bad responses. It is good at finding patterns it has seen before. It is not good at noticing that a response changed the wrong account, because that requires understanding what the request meant. The tester had to read the response, compare it with what he asked for, and ask why they differed.
When a language has forty-seven loose-comparison edge cases, those edge cases will show up in security controls, especially ones written quickly, under deadline, by developers focused on the happy path. Type juggling is not exotic: it is a regular consequence of mixing dynamic typing with integer-based validation, and it appears in production far more often than it should.
The "happy path" is the route through the code where everything goes as intended: the user types a real code, the code matches, the password changes. Security bugs live on the unhappy paths, the ones where the user sends something the programmer never pictured. A boolean where a number was expected is exactly such a case.
Glossary
- API: the machine-facing part of a web application, which accepts structured requests and returns structured responses rather than web pages.
- Boolean: a value that is either true or false, written in JSON as
trueorfalsewithout quotes. - Brute force: trying every possible value of a secret, one after another, until one works.
- Cookie: a small piece of data a website stores in your browser and receives back on each request, often used to identify your session.
- CRM: Customer Relationship Management software, used by companies to track customers, leads and contacts.
- Database: an organised store of records, arranged in tables with columns and rows.
- Default value: the value a field is automatically given when nobody supplies one.
- Defence in depth: layering several independent protections so that one failure does not open the door.
- Endpoint: one specific address inside an API that performs one job.
- Happy path: the route through code where the user behaves exactly as the programmer expected.
- HTTP: the protocol browsers and servers use to exchange requests and responses.
- Integer: a whole number, such as
0,2or482913. - JSON: a text format for structured data, made of names and values.
- Loose comparison: PHP's
==operator, which converts types before comparing. - NOT NULL: a database rule saying a column must always contain a real value, never blank.
- One-time password (OTP): a short code sent to an account owner, valid once, used to prove ownership.
- ORM: a library that lets programmers treat database tables as code objects and fills in defaults for unset fields.
- Parameter: one named field inside a request, such as
user_idorotp. - Phishing: tricking a person into revealing a secret, such as a code or password.
- PHP: a widely used programming language for websites, which converts types automatically in loose comparisons.
- POST: an HTTP method meaning "here is some data, please act on it".
- Scanner: an automated tool that sends known-bad inputs and looks for known-bad responses.
- Session: the server-side record of who you are, kept alive by an identifier your browser sends back each time.
- Strict comparison: PHP's
===operator, which checks value and type together. - String: text, written in JSON inside quotes.
- Takeover: gaining control of another person's account, here by setting a new password on it.
- Type juggling (type coercion): a language's automatic conversion of a value from one type to another during comparison or arithmetic.
- VAPT: Vulnerability Assessment and Penetration Test, an authorised engagement in which a tester tries to break a system with permission.