Skip to content

fix(java): recognize SharedSessionContract in hibernate-sqli - #4027

Open
Eljees wants to merge 1 commit into
semgrep:developfrom
Eljees:agent/hibernate-sqli-shared-session
Open

fix(java): recognize SharedSessionContract in hibernate-sqli#4027
Eljees wants to merge 1 commit into
semgrep:developfrom
Eljees:agent/hibernate-sqli-shared-session

Conversation

@Eljees

@Eljees Eljees commented Jul 28, 2026

Copy link
Copy Markdown

Addresses case 1 of #3817.

Problem

hibernate-sqli only tracks a dynamic query when the receiver is typed as org.hibernate.Session. The same HQL executed through a SharedSessionContract is missed:

SharedSessionContract session = sessionFactory.openSession();
String hql = "from UserEntity where name = '" + input + "'";
session.createQuery(hql);   // not flagged before this change

SharedSessionContract is the common supertype of Session and StatelessSession and is a normal declared type for a session handle, so createQuery / createSQLQuery called on it is the same sink.

Change

Adds SharedSessionContract next to Session in the receiver-type alternatives, for both the local-variable and method-parameter shapes. All the existing guards (the "..." + "..." exclusions, the createQuery|createSQLQuery method filter) apply unchanged, so a constant query on a SharedSessionContract is still not flagged.

The StringBuilder case from the issue is intentionally left out: tracking a query assembled via StringBuilder.append(...).toString() needs more than a receiver-type addition (and my first attempt at a pattern-inside for it made the rule fail to parse), so it's better handled as a separate change than bundled in loosely here.

Tests

hibernate-sqli.java gains a testSharedSessionContract method with the reproducer as ruleid and a constant query as ok.

semgrep --test --config java/lang/security/audit/sqli/hibernate-sqli.yaml java/lang/security/audit/sqli/hibernate-sqli.java
1/1: ✓ All tests passed

The rule only tracked a query built on an org.hibernate.Session, so the
same dynamic HQL executed through a SharedSessionContract (the supertype
of Session and StatelessSession, and the declared return type used in
plenty of code) was a false negative.

Add SharedSessionContract next to Session in the receiver-type
alternatives, for both the local-variable and method-parameter shapes.

The StringBuilder case from the issue is left for a follow-up: tracking a
query assembled through StringBuilder.append/toString needs more than a
receiver-type addition and is better handled separately.

Signed-off-by: Eljees <3.14hell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant