High severity
Protected variable written without its guard
Updated 2026-11-10
Some state variables should change only through a function that checks who is calling: the address funds are swept to, the oracle a vault trusts, the owner slot itself. Solidity lets you write that intent down. A NatSpec annotation of the form /// @custom:security write-protection="onlyOwner()" on the variable declares that every write must pass through the named guard. The bug is the gap between that annotation and the code: one function writes the variable behind the guard, and a second function (added later, copied from a sibling, or forgotten) writes the same variable with no guard at all. The compiler does not enforce the annotation. Anyone who finds the unguarded setter can set the value to whatever they like.
Vulnerable pattern
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Treasury {
address public owner;
/// @custom:security write-protection="onlyOwner()"
address public withdrawalTarget;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "not owner");
_;
}
function setWithdrawalTarget(address target) external onlyOwner {
withdrawalTarget = target;
}
// Bug: a second setter added later that forgot the guard.
// Anyone can redirect where the treasury pays out.
function updateWithdrawalTarget(address target) external {
withdrawalTarget = target;
}
}The annotation on withdrawalTarget says it must only be written by a function that goes through onlyOwner(). setWithdrawalTarget does that. updateWithdrawalTarget writes the same slot with no access control, so any address can call it and point the treasury’s payouts at itself. The guarded setter right above it makes the mistake easy to miss in review: the variable looks protected, and one of its two writers is. A lost access-control check usually looks like this, a variable guarded in some places and not others.
The fix
Put the same guard on every function that writes the variable, or delete the writers that should not exist. If a value is only ever meant to change through the owner, there should be exactly one guarded path to change it. When two functions do the same job, keep the guarded one and remove the duplicate, so review does not have to catch a second unguarded path every time.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Treasury {
address public owner;
/// @custom:security write-protection="onlyOwner()"
address public withdrawalTarget;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "not owner");
_;
}
function setWithdrawalTarget(address target) external onlyOwner {
withdrawalTarget = target;
}
function updateWithdrawalTarget(address target) external onlyOwner {
withdrawalTarget = target;
}
}A note on scope
This detector is opt-in: it fires only when the variable carries the write-protection annotation. The annotation tells the tool which slots matter and which guard each one needs; without it, the detector has nothing to check against and stays silent. We confirmed that on solc 0.8.19: the exact vulnerable contract above, with the annotation removed, produces zero findings even though the unguarded setter is still there. It is a consistency check, not a general “every state write needs a modifier” scanner (that would flood you with false positives on variables meant to be freely writable): you declare the guard once, next to the variable, and the tool proves that every function writing that variable goes through it. The habit is worth keeping even without the annotation: decide which variables are sensitive, pick one guard per variable, and apply it to every writer.
Scan your contract for this
pactlint flags protected-vars and other recurring DeFi bug classes in Solidity
source or a verified contract on Ethereum or Base. Pay per call in USDC on Base, Polygon, Solana, BNB Chain or Arbitrum with x402,
or on Tempo with MPP, no signup; the first few scans each day are free.
A state variable marked write-protection that is written by a function which does not
pass through the named guard is reported by Slither’s stock protected-vars detector, rated
high impact at high confidence, so pactlint surfaces it at high severity, unchanged
from the detector’s rating. We confirmed both halves on solc 0.8.19: the vulnerable contract
above fires the detector at high impact and high confidence, and adding onlyOwner to the second
setter clears it to zero findings. Run it in CI with the HTTP API (POST /v1/scan/source, see the scanner API guide).
Heuristic and educational, not an audit.