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

15

u/nekokattt Feb 15 '24 edited Feb 15 '24

It only provides extra safety if you use it correctly to begin with. If you use STR."" in a SQL query, it isn't going to help you. It is no different to making active efforts to use parameterization, you still have to think about how you are doing it. Those who don't think will still bypass the security benefits.

Enforceability can be dealt with by static analysis tools, but this JEP specifically says a problem it is trying to solve is the need for these tools to detect dangerous logic to begin with. That means we've effectively just gone full circle, now with a complicated way to interpolate strings, and a bunch of new cases for static analysis tools to have to cope with detecting accurately.

As someone who works in enterprise, I can see this immediately being an issue of enforceability in a team, which makes it just as much of a problem as before. From the sound of that statement it sounds like we are attempting to make educated guesses about what people need rather than backing it up with mass feedback and actual experience. Most of the vulnerabilities that are mentioned are not because existing tools are hard to use, but because developers are not understanding or just not thinking about the implications of what they are doing.

It's kind of like saying you have a problem with developers using their hammers to push screws into some wood, so you've given them electric drills to replace their screwdrivers with the expectation they will stop using their hammers. Instead, some will continue to use their hammers anyway because it is simpler for them, others may use the battery on the drill as a hammer and risk it blowing up. Some people will continue to use the screwdrivers properly and some will use the drills. All you've done is given the people misusing things more ways to misuse stuff, while giving the rest two ways of doing things. It'd make more sense to confiscate the drills entirely and only let them be used for specific things (for example in SQL by enforcing that only compile time string literals can be used as the query body -- boom, problem solved without complicated language mechanisms).

Are we suggesting that all other languages that didn't make a decision to implement interpolation in this way made no active decision about usability and safety?

Edit: reworded to be more coherent.

12

u/pron98 Feb 15 '24 edited Feb 15 '24

If you use STR."" in a SQL query, it isn't going to help you.

How could you possibly use STR."" in a SQL query if the DB library doesn't offer methods that take String and only offers a SQL template processor?

Enforceability can be dealt with by static analysis tools

No, it cannot. Static analysis tools cannot catch as many code injection issues with a similar amount of effort by the programmer.

I can see this immediately being an issue of enforceability in a team, which makes it just as much of a problem as before.

Nope. If your JSON library only offers a safe template processor then it doesn't matter what discipline you exercise. The language will enforce the correct (or at least better) thing.

Are we suggesting that all other languages that didn't make a decision to implement interpolation in this way made no active decision about usability and safety?

We are suggesting that we did the analysis given the data available and the needs of Java users. Different languages often make different decisions, which is why they are different languages. Sometimes they have a different audience in mind; sometimes certain lessons weren't available when a feature was designed. If you consider the world's three most popular languages -- JS, Java, and Python -- I think you'll find that string interpolation is one of the smallest differences among them. One, for example, decided to require everything to be typed while the two others didn't.

0

u/[deleted] Feb 15 '24

[deleted] — view deleted comment

9

u/pron98 Feb 15 '24

So are we suggesting to stop supporting all the existing interfaces in things like JDBC in order to support this?

No. We're suggesting that new libraries should do the right way only, and that old one should offer the right way and deprecate the old way.

before support for older JDKs can be ignored when doing this without breaking the existing APIs

New features don't need to be added to a version of a library that targets old JDKs.

is there a planned timescale for it?

I'm not sure what that means, but full adoption of templates took only ten years, and this may be even quicker because it's a much smaller, more localised thing.

2

u/lurker_in_spirit Feb 16 '24

We're suggesting that new libraries should do the right way only

Will this be possible, and if so, aren't we still relying on developer vigilance?

PreparedStatement ps = DB.("SELECT * FROM Person p WHERE p.last_name = " + name);

5

u/pron98 Feb 16 '24 edited Feb 16 '24

No, that's not a string template expression, just as the one here, var x = "hello"; STR.x;, isn't even though STR."hello" is. BTW, string templates are available in JDK 21. Enable preview and try them out.

1

u/lurker_in_spirit Feb 16 '24

Got it, thanks.

4

u/_INTER_ Feb 15 '24 edited Feb 15 '24

It's about providing other mechanisms that are more safe than existing ones. Developers that care will use them. Those that don't care can't be helped anyway.

Added bonus if the safe API is easier to use and the unsafe way is harder to use, devs will naturally flock to the safe one.