| name | cwe-501-trust-boundary-violation |
| description | Use this skill when you need to remediate CWE-501 (Trust Boundary Violation) vulnerabilities in Java code. Triggers on SAST findings, security reviews, or when fixing trust boundary violation issues. |
| version | 1.0.0 |
| license | MIT |
| tags | ["security","java","cwe-501","remediation","sast"] |
CWE-501 Trust Boundary Violation
Description
Trust Boundary Violation
Reference:
https://cwe.mitre.org/data/definitions/501.html
OWASP Category: A04:2021 – Insecure Design
Vulnerable Pattern
❌ Example 1: Vulnerable Pattern
String role = request.getParameter("role");
session.setAttribute("userRole", role);
User user = deserializeUser(request.getInputStream());
session.setAttribute("currentUser", user);
Why it's vulnerable: This pattern is vulnerable to Trust Boundary Violation
Deterministic Fix
✅ Secure Implementation: Secure Implementation
String role = request.getParameter("role");
Set<String> allowedRoles = Set.of("user", "viewer", "editor");
if (!allowedRoles.contains(role)) {
throw new SecurityException("Invalid role: " + role);
}
session.setAttribute("userRole", role);
String userId = request.getParameter("userId");
User user = userRepository.findById(userId)
.orElseThrow(() -> new AuthenticationException("User not found"));
if (!isAuthenticated(user, request)) {
throw new AuthenticationException("Not authenticated");
}
session.setAttribute("currentUser", user);
Why it's secure: Implements proper protection against Trust Boundary Violation
Detection Pattern
Look for these patterns in your codebase:
grep -rn "session.setAttribute" --include="*.java" -B5 | grep "getParameter"
Remediation Steps
-
Never store unvalidated user input in session
-
Validate data against allowlist before trusting
-
Fetch user data from database, not from client
-
Authenticate/authorize before promoting to session
Key Imports
import javax.servlet.http.HttpSession;
Verification
After remediation:
-
Run SAST scanner to confirm vulnerability is resolved
-
Review all instances of the vulnerable pattern
-
Add unit tests that verify the secure implementation
-
Check for similar patterns in related code
Trigger Examples
Fix CWE-501 vulnerability
Resolve Trust Boundary Violation issue
Secure this Java code against trust boundary violation
SAST reports CWE-501
Common Vulnerable Locations
| Controller | *Controller.java | User input handling |
| Service | *Service.java | Business logic |
| Repository | *Repository.java | Data access |
References
Source: Generated by Java CWE Security Skills Generator
Last Updated: 2026-03-07