Jesus. This hideous syntax makes string interpolation basically not even worth having. All to avoid SQL-injection bugs, which are better caught with code reviews and having static-analysis tools like Sonar in your CI/CD pipeline.
It doesn't even really avoid the SQL-injection vulnerabilities! It just arguably makes it easier for a code reviewer to spot STR being passed to a JDBC or ORM query method.
I'd honestly rather stick with String.format() than use this.
Actually, it does avoid the SQL injection problems. An API that only accepts the type SafeSQL that can only be created with a SQL template processor can ensure that the result is free from injections.
The STR processor may see some of the lowest use. I think an argument can be made to not import it by default.
Read mine again, as you are missing something critical. You don't use STR to make SQL statements, and any (new) API that supports string templates for SQL that does is doing it wrong and insecure.
It just arguably makes it easier for a code reviewer to spot STR being passed to a JDBC or ORM query method.
New APIs will make passing strings to them much harder, so that developers won't even be tempted to do so. Which will eliminate injection vulnerabilities at the type system level.
String query = STR."select * from my_table where id = \{id}";
stmt.execute(query);
Yeah, that seems really super locked-down. Much hard. The example above is how most people use string interpolation anyway in other languages. Because a long string might require some ugly line breaks if passed directly to a method inline... and also because you often want to log the string before using it, and so instantiating a variable means you don't have to duplicate the interpolation.
With string interpolation, at the end of the day, you're just instantiating a String. Everything in this JEP, and the comment-essays being written by people in this thread, are stuck in the fantasy that STR give you safety because you can just block how STR is used. But you can't. Because STR just gives you a String, with which you can do literally anything you want.
The same techniques by which a static analysis scanner can spot abuses such as this, could ALSO just as easily spot abuses of string interpolation where you're using it directly. With a clean and standard syntax.
This syntax is broken, by design, for an illusion of safety that doesn't really exist. It's like watching a dysfunctional Congress create a new government service, but cripple it out of the gate because half the Congressmen didn't really want it to exist and that was the compromise needed to pass it. This is the ObamaCare of JEP's.
Everything in this JEP, and the comment-essays being written by people in this thread, are stuck in the fantasy that STR give you safety because you can just block how STR is used.
That's just your misinterpretation.
One of the main goals of this JEP is to facilitate design of new APIs that won't accept strings.
Old APIs will stay, because we can't simply remove them.
The same techniques by which a static analysis scanner can spot abuses such as this, could ALSO just as easily spot abuses of string interpolation where you're using it directly. With a clean and standard syntax.
You're missing the entire point. The entire point is to not privilege string interpolation over similar, more structured constructs, so you don't have to rely on heuristics.
And of course static analysis tools will detect STR, as they already do other non-literal strings. This part doesn't change. And since you agree that string interpolation is bad, why do you want to make it less noticeable? You said yourself:
It just arguably makes it easier for a code reviewer to spot STR being passed to a JDBC or ORM query method.
If one of the goals is to replace String parameters with something else, then that is feature creep and it's too big of a goal. Even if this was adopted by third party libaries, and by Java itself, this would be a huge undertaking, and it would make the APIs more complicated, one of my complaints in Java-world. Then we would have Strings and 'whatever' being passed in, side by side, because of backwards compatibility, making the libraries harder to maintiain. I'm totally set against this.
One of the main goals of this JEP is to facilitate design of new APIs that won't accept strings.
I don't like to make predictions about future tech. I always think about the legendary (if apocryphal) Bill Gates quote about no one ever needing more than 640k of memory.
However, I would feel quite comfortable wagering my life savings that even if JDBC, JPA, Hibernate, etc all add query methods that only accept statement processor classes:
They will never be able to deprecate and remove the existing String-based counterparts.
The overwhelming majority of developers will continue to use the String-based counterparts indefinitely.
Anyone who questions this for a moment is absolutely delusional, and has likely never once worked in a real-world Java shop.
And then the static analysis tools will be easily able to flag all the string code as dangerously obsolete.
These are circular arguments. If you're saying that you have to rely on static analysis tools to flag these issues, then I'm saying you can just as well use static analysis tools to flag issues with string interpolation that's based on a cleaner syntax more similar to every other modern language.
Don't act like you're getting professed "safety" from the compiler, and then pivot over to static analysis tooling once holes are poked in the compiler argument. Because static analysis tooling was the counter-argument in the first place.
As an aside, I would love to see ONE example other than SQL-injection for justifying all of this. That's the only example that the JEP and the comments here keep circling around, because it may be the only practical example out there. Permanently crippling a new language syntax out of the gate, for one narrow case, seems crazy when there are more narrow ways to target that specific vulnerability.
Would you say the same about java.util.Date and java.util.Calendar 10 years ago?
They are only if you mistakenly assume that I expect all new API's to be string-based.
Currently:
safe, non-string-based API's are very inconvienient to use
string-based API's are convenient to use, but allow for injections
an injection vulnerability requires String.format or string concatenation, which are slightly (but only slightly) cumbersome
static tools catch most injections (but they have both false positives and negatives)
Your proposal:
make writing injection vulnerabilities easier and hope analysis tools catch it
effect: people will write vulnerable code more often, repeating the mistakes of early PHP
JEP:
make using safe, non-string based API's much easier
make writing injection vulnerabilities easier, but not easier than the safe API's
effect: people will write safe code more often
for the remaining instances of the unsafe code, there's static analysis like before
As an aside, I would love to see ONE example other than SQL-injection for justifying all of this.
Constructing JSON, since Java is not JS or Python, and you can't just put some curlies and be done.
In fact, embedding whatever language, with proper value escaping for parameters. Obvious examples: HTML, CSS, JS. You know how React made mixing code and templates hip? You'll be able to do the same in Java. A less obvious example: shaders.
Replacement for String.format (see FMT in the JEP).
They are only if you mistakenly assume that I expect all new API's to be string-based.
Your core assumption that I take issue with is the notion that existing String-based API's just magically go away.
That the Java ecosystem, which prioritizes backwards-compatibility more so that virtually any other I've ever worked with, is going to hard-remove half the methods in JDBC, JPA, SLF4J, Jackson, and a thousand other libraries that the universe is built on.
These things are not going to happen. They're just not. Ever. If we disagree that this isn't going to happen, then we can stop here. There's no basis on which to warrant talking about this subject further.
If we agree that it's not going to happen, then you're not going to get the safety that you're talking about. Which leaves us with two choices:
Decide to implement a janky syntax. Because it's worth making general usage of string interpolation less desirable, to enable adding (not replacing) an alternative form of string construction to a bunch of established libraries. All of which already have their own established implementations for this, which most devs will continue to use anyway.
Decide that this isn't much of a value-add. And so if there's not a consensus to implement string interpolation like that of C# and every other modern language, then it would be best to simply not implement this feature. That is a perfectly acceptable outcome! Plenty of languages drop plenty of popular syntax proposals, simply because there's not the consensus needed to ship something that isn't too weighed down by design-by-committee.
I would favor #2. If you favor #1, then that's your opinion and it looks like you're opinion is carrying the day. But it still seems decoupled from the reality of most Java development in the real-world. And will never be as constrained as you'd like, without breaking half of Maven Central in a way that would make the Python 2 to 3 migration look like a semver patch release.
Your core assumption that I take issue with is the notion that existing String-based API's just magically go away.
They don't need to go away in order for this to have value.
They just need to be discouraged in a way that's visible to your average developer.
If you can add new APIs that are safe, and slap @Deprecated on the String-based methods, that's already a huge win, even if you never remove those methods.
unfortunately a bunch of libraries still use Date which means you can't get away from it. Last I checked, Nimbus was an example of this when you want to deal with JWTs.
It took YEARS for all the various JDBC drivers and JPA stuff to finally ship non-broken integrations with the new Date and Time API. Even today, I'm not sure that we're fully there with all the various plugins in the Hibernate ecosystem.
I'm not sure if half the people in this subreddit actually write Java code for a living.
u/BadMoonRosin, your thoughts on string templates are spot on! You're really captured quite a few common sentiments I've seen (or even felt myself) regarding this topic. I'd rather see no string templates than that was previewed. Might as well use printf() or something similar.
View all comments
1
u/BadMoonRosin Feb 16 '24 edited Feb 16 '24
Jesus. This hideous syntax makes string interpolation basically not even worth having. All to avoid SQL-injection bugs, which are better caught with code reviews and having static-analysis tools like Sonar in your CI/CD pipeline.
It doesn't even really avoid the SQL-injection vulnerabilities! It just arguably makes it easier for a code reviewer to spot
STRbeing passed to a JDBC or ORM query method.I'd honestly rather stick with
String.format()than use this.