State Variable Default Visibility (SWC-108)

Description
Visibility in programming influences how easily a variable or function may be accessed from different areas of the code. To specify which sections of the code may access a variable, it can be annotated with a visibility label when it is declared. Languages like JavaScript may do this labelling implicitly or explicitly, as in the case of languages like Solidity.
It is simpler to spot false assumptions about who may use a variable or function when the accessibility of those objects is explicitly labelled. A variable, for instance, can only be accessed by the contract that declared it if it is specified as private. The compiler will produce an error if another contract or an outside user tries to access the variable.
Clarifying the intended purpose and scope of a variable by explicitly labelling its visibility improves both the readability and comprehension of the code. Even though the default behaviour is adequate, it is generally a good programming practise to label the visibility of all variables and functions in order to minimise misunderstanding and potential security flaws.
Восстановление
Solidity, state variables can be declared with one of three visibility modifiers: public, internal, or private.
The public modifier makes a state variable accessible from outside the contract, and also creates an automatically generated getter function to read the value. This means that anyone can read the value of the variable from the blockchain, which can be a security vulnerability if sensitive data is stored in the variable.
В public modifier, on the other hand, makes a state variable only accessible from within the contract. This means that the value cannot be read or modified from outside the contract. However, this does not protect against malicious actors within the contract.
В internal modifier makes a state variable only accessible from within the contract and its derived contracts. This provides a level of protection against malicious actors within the contract and allows derived contracts to access the value.
Contract Samples
Code with a vulnerability
pragma solidity 0.4.24;
contract TestStorage {
uint storeduint1 = 15;
uint constant constuint = 16;
uint32 investmentsDeadlineTimeStamp = uint32(now);
bytes16 string1 = "test1";
bytes32 private string2 = "test1236";
string public string3 = "lets string something";
mapping (address => uint) public uints1;
mapping (address => DeviceData) structs1;
uint[] uintarray;
DeviceData[] deviceDataArray;
struct DeviceData {
string deviceBrand;
string deviceYear;
string batteryWearLevel;
}
function testStorage() public {
address address1 = 0xbccc714d56bc0da0fd33d96d2a87b680dd6d0df6;
address address2 = 0xaee905fdd3ed851e48d22059575b9f4245a82b04;
uints1[address1] = 88;
uints1[address2] = 99;
DeviceData memory dev1 = DeviceData("deviceBrand", "deviceYear", "wearLevel");
structs1[address1] = dev1;
uintarray.push(8000);
uintarray.push(9000);
deviceDataArray.push(dev1);
}
}
Tools for scaning SWC-108
1. Mythril: It is a popular security analysis tool for Ethereum smart contracts. It has a rule to check for missing visibility specifiers.
2. Solhint: It is a linter for Solidity that provides security and style guide validations. It has a rule to check for missing visibility specifiers.
3. Slither: It is a static analysis tool for Solidity that can detect a wide range of security vulnerabilities, including SWC-108.
4. Remix: It is an online IDE for Solidity that provides a static analysis feature to scan for security vulnerabilities, including SWC-108.
Общее перечисление слабых мест (CWE)
CWE-710: Improper Adherence to Coding Standards
Mitigation for SWC-108
1. Explicitly label the visibility of all state variables: As recommended by SWC-108, always specify the visibility of state variables as either public, private, or internal. This will ensure that access to the variables is limited only to the intended parties.
2. Use require statements to check for access control: You can use require statements to check for access control before allowing functions to execute. This can help prevent unauthorized access to state variables.
3. Use access control contracts: You can use access control contracts to manage access to state variables. This involves creating a contract with functions that define the access control rules for state variables, and then inheriting this contract in your main contract.
4. Use libraries: You can also use libraries to manage access control. Libraries are reusable pieces of code that can be deployed and used across multiple contracts. By encapsulating access control logic in a library, you can ensure that it is consistent across multiple contracts.
5. Follow the principle of least privilege: Follow the principle of least privilege when assigning roles and permissions. Only grant the minimum level of access necessary for each role or user.
Заключение
SWC-108 highlights the importance of explicitly defining the visibility of state variables in Solidity smart contracts. By doing so, developers can avoid making incorrect assumptions about who can access the variable, which can lead to security vulnerabilities. To mitigate this issue, developers should explicitly define the visibility of all state variables in their smart contracts, using the public, internal, or private keywords. Additionally, they can use tools such as Mythril and Slither to scan their contracts for potential issues related to variable visibility. By following these best practices, developers can create more secure smart contracts that are less vulnerable to attacks.
Другие Услуги
Insomnia Security Scanner
AI-powered web application security scanner by CQR. Automated vulnerability discovery, exploit verification, and detailed reporting for modern applications.
Узнать большеЗащита Инфраструктуры CRYEYE
Аудит безопасности с помощью CryEye обеспечивает информационную безопасность предприятия, защищая всю инфраструктуру.
Узнать большеТестирование на проникновение
Найдите уязвимости во всей инфраструктуре вашего бизнеса раньше, чем это сделают хакеры! В рамках консалтинга по тестированию на проникновение мы подберем методы пентестов.
Узнать большеСоциальная инженерия
Simulate real-world phishing, vishing, and pretexting attacks to measure and improve your team's security awareness and response capabilities.
Узнать большеНагрузочное Тестирование
All kinds of load and performance testing of your system from the CQR online security company.
Узнать большеAI-Powered Vulnerability Assessment
Leverage artificial intelligence to discover, prioritize, and remediate vulnerabilities across your digital assets faster and more accurately than traditional scanners.
Узнать большеCloud Security Audit (AWS / GCP / Azure)
Comprehensive security review of your cloud environments — IAM policies, network controls, data exposure, and misconfigurations across all major cloud platforms.
Узнать большеDevSecOps Integration
Embed security into every stage of your CI/CD pipeline. Automated SAST, DAST, SCA, and secret scanning so vulnerabilities are caught before they reach production.
Узнать большеAPI Security Testing
In-depth testing of REST, GraphQL, and SOAP APIs for authentication flaws, authorization bypasses, injection vulnerabilities, and data leakage risks.
Узнать большеMobile Application Penetration Testing
Manual and automated security testing for iOS and Android applications — reverse engineering, runtime analysis, traffic interception, and backend API assessment.
Узнать большеIoT Security Assessment
Evaluate firmware, communication protocols, cloud backends, and physical interfaces of IoT devices to identify vulnerabilities before attackers do.
Узнать большеBlockchain & Smart Contract Audit
Formal verification and manual code review of smart contracts on Ethereum, Solana, and other chains. Detect reentrancy, overflow, and logic flaws before deployment.
Узнать большеRed Team Operations
Advanced adversary simulation using real attacker TTPs (MITRE ATT&CK) to test your detection, response, and overall security posture under realistic conditions.
Узнать большеThreat Intelligence & Monitoring
Continuous monitoring of threat feeds, dark web, and attacker infrastructure to provide actionable intelligence specific to your organization and industry.
Узнать большеZero Trust Architecture Review
Assess and design your Zero Trust security model — identity verification, micro-segmentation, least-privilege access, and continuous validation controls.
Узнать большеCompliance Consulting (PCI DSS / SOC 2 / GDPR)
Expert guidance to achieve and maintain compliance with major security frameworks. Gap analysis, remediation roadmaps, and audit-readiness support.
Узнать большеDark Web Monitoring
Continuous surveillance of dark web forums, marketplaces, and breach databases for leaked credentials, sensitive data, or mentions of your organization.
Узнать большеPhishing Simulation & Awareness Training
Controlled phishing campaigns combined with interactive security awareness training to build a human firewall across your entire organization.
Узнать большеSupply Chain Security Audit
Assess third-party vendor risks, open-source dependencies, and software supply chain integrity to prevent attacks like SolarWinds and Log4Shell.
Узнать большеContainer & Kubernetes Security
Security review of Docker images, Kubernetes clusters, RBAC policies, network policies, and runtime configurations to harden your container infrastructure.
Узнать большеWeb Application Firewall (WAF) Deployment
Professional WAF setup, rule tuning, and ongoing management to block SQL injection, XSS, CSRF, and other OWASP Top 10 threats in real time.
Узнать большеBug Bounty Program Management
Full lifecycle management of your bug bounty program — scope definition, researcher coordination, triage, validation, and remediation tracking.
Узнать большеOSINT Investigation Services
Open-source intelligence gathering on individuals, organizations, and infrastructure. Ideal for pre-engagement recon, fraud investigation, and competitive analysis.
Узнать большеDigital Forensics & Incident Response
Rapid response to security breaches — evidence collection, malware analysis, attacker timeline reconstruction, and actionable remediation recommendations.
Узнать больше