Jvm security pitfalls

From makifbaysal/tasktrooper · View interactive version

Jvm security pitfalls is an AI agent skill from the GitHub repository makifbaysal/tasktrooper. Use when reviewing Java or Kotlin server changes on Spring or Quarkus - security config and method security, JPA/JPQL and JdbcTemplate queries, request binding, deserialization, XML parsers, SpEL, templates, Actuator, JNDI and reflection

How to use this skill

Skill file

JVM Security Pitfalls (Java, Kotlin — Spring, Quarkus)

Overview

On the JVM the dangerous code is often configuration: a matcher order, a missing @EnableMethodSecurity, a Jackson typing setting, an Actuator exposure list. Read config and annotation diffs with the same care as handler code.

1. Spring Security Configuration (CWE-285, CWE-863)

// ❌ the admin rule is never reached for /api/admin/**
http.authorizeHttpRequests {
    it.requestMatchers("/api/**").permitAll()
    it.requestMatchers("/api/admin/**").hasRole("ADMIN")
}
// ✅ most specific first, deny by default
http.authorizeHttpRequests {
    it.requestMatchers("/api/admin/**").hasRole("ADMIN")
    it.requestMatchers("/api/public/**").permitAll()
    it.anyRequest().authenticated()
}

Quarkus: a resource method without @RolesAllowed/@Authenticated where its siblings have one; @PermitAll added; quarkus.http.auth.permission.<name>.policy=permit widened to a broader paths=; quarkus.security.jaxrs.deny-unannotated-endpoints turned off.

2. Queries (CWE-89)

// ❌ JPQL injection
em.createQuery("SELECT u FROM User u WHERE u.email = '" + email + "'", User.class);
// ✅
em.createQuery("SELECT u FROM User u WHERE u.email = :email", User.class).setParameter("email", email);

Also: createNativeQuery with concatenation; JdbcTemplate.query("… " + x); @Query built with string concatenation in a custom repository; Spring Data JpaSort.unsafe(userInput) (plain Sort.by validates property names — unsafe does not); Panache find("name = '" + n + "'") instead of find("name", n) or find("name = ?1", n).

3. Request Binding (CWE-915)

// ❌ the JPA entity is the request body: role, ownerId, verified are all client-settable
@PostMapping("/users/{id}") User update(@PathVariable UUID id, @RequestBody User body) { … }
// ✅ a request record with only the editable fields
record UpdateProfile(String displayName, String bio) {}

@ModelAttribute on an entity binds every setter unless @InitBinder restricts setAllowedFields. Returning the entity serialises fields like passwordHash.

4. Deserialization and Reflection (CWE-502, CWE-470)

5. XML Parsers (CWE-611)

// ❌ DTDs and external entities resolved
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// ✅
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
f.setXIncludeAware(false);
f.setExpandEntityReferences(false);

The same applies to SAXParserFactory, XMLInputFactory (SUPPORT_DTD false), TransformerFactory, SchemaFactory, Unmarshaller built on an unconfigured source.

6. Expression and Template Injection (CWE-917, CWE-1336)

// ❌ SpEL from input with a full evaluation context → T(java.lang.Runtime).getRuntime().exec(…)
parser.parseExpression(userFilter).getValue(new StandardEvaluationContext(root));
// ✅ no user-authored expressions; if unavoidable, SimpleEvaluationContext (no type references)
parser.parseExpression(userFilter).getValue(SimpleEvaluationContext.forReadOnlyDataBinding().build(), root);

Thymeleaf: a controller returning a view name built from input ("pages/" + page) enables expression preprocessing (__${…}__); th:utext with user content is XSS. FreeMarker templates authored by users with ?new / ?api available.

7. Operational Exposure

8. Commands, Paths, Randomness, Tokens

Common Mistakes

Red Flags

References (names and links only)

Spring Security reference · Quarkus security · CWE-89, 285, 470, 502, 611, 863, 915, 917, 1336 · OWASP Top 10:2025

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.