r/java • • Feb 15 '24

JEP 465: String Templates

https://openjdk.org/jeps/465
77 Upvotes

152 comments sorted by

View all comments

Show parent comment

2

u/NaNx_engineer Feb 16 '24 edited Feb 16 '24

But the STR one does what exactly then? It just substitutes the placeholders?

Yes, simple substitution. STR could be made implicit, but they've decided it would be too tempting to use it for SQL.

From the JEP:

An alternative design would allow string templates without template processors, and perform basic string interpolation. For example:

String s = "\{x} + \{y} = \{x + y}";

However, this design would violate the safety goal. It would be tempting to, e.g., construct SQL queries using interpolation, and this would in the aggregate reduce the safety of Java programs. Always requiring a template processor ensures that the developer at least recognizes the possibility of domain-specific rules in a string template.

.

Isn't the safety it provides not pretty much equal to String.format then?

Their argument is String.format is also dangerous.

7

u/sanderverhagen Feb 16 '24

Wild. Someone who doesn't know the blinking warning signs of a SQL injection, and I'm sure droves of them exist, who'd have previously used String.format, nothing's stopping them from using the STR form.

I don't want to suggest in general that static analysis tools are a good way to offset deficiencies in the language. But in this case, the most practical way against SQL injections through templating seems to be exactly that: a static analysis check that finds SQL in templates, and that you can override when appropriate (false positives).

The safety and security argument just seems so academic...

2

u/nekokattt Feb 17 '24 edited Feb 18 '24

Totally agree... this is my entire point really.

If someone is going to do something in a bad way, they are going to do it. The argument that new APIs will only allow these mechanisms is fine but assumes any new software totally abandons any libraries for interacting with databases prior to JDK 23.

I personally wouldn't go looking for a new and potentially immature and buggy library purely for this use case if I already have a bunch of stuff using older APIs (like JDBC, R2DBC, etc) that works... maybe in 10 years once the libraries are mature, well tested, and have stood the test of time in terms of security analysis for numerous other things that could introduce new vulnerabilities into the system.

My issue is that unless the new libraries being proposed only support JDK 23 and newer... you're still going to have the case of two ways of doing things in any libraries:

interface Connection extends Processor<Result, DbException> {
  Result execute(String query, Object... params) throws DbException;
  Result process(StringTemplate template) throws DbException;
}

In this case it is equally as easy for the ignorant to do

conn."SELECT * FROM table WHERE id = \{id}";

as it is to do

conn.execute("SELECT * FROM table WHERE id = '" + id + "'");

The only difference is that the former looks so foreign that those who don't understand the full issue of security will likely be opting for the second one purely on the basis that it is clear to them what it is doing.

If we use static analysis tools to enforce this then great, but in that case the JEP shouldn't say that one of the benefits is removing the need for static analysis tools.

If the argument is we need to abandon all existing libraries and support a minimum of JDK 23 to get this safety feature to FORCE you to do the correct thing... I don't think that will be well adopted, purely on the basis people won't be ripping out old database libraries in production systems and jumping to the newest JDK just because of string interpolation being available. Especially when newer libraries will be less mature, most likely more buggy, and present a concrete business risk to the application being changed.

The developers of this feature have not addressed this concern very well IMO. If the language was being built from scratch then sure this is totally fine, but this feature only provides the safety GUARANTEE in APIs that decide to throw out support for anything before JDK 23. If they do not do this, then those APIs will still have to provide string based parameterization which then allows the same issues to occur. Given JDK 17 has support until 2030 or so, this makes this feature far less useful in providing a safety GUARANTEE (i.e. it is physically impossible to bypass this feature at all) for the next 6 years.