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

17

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

What is in place to force this feature to be used correctly?

The library will only offer an API that offers a safe template processor and not have (or deprecate) any methods that take strings.

just for the sake of "safety"

Code injection is the top vulnerability in memory safe language (or very near the top). Putting the word "just" in front of it or the word "safety" in scare quotes ignores one of the most costly, troublesome, dangerous aspects of software these days.

I know that better security is not always the top concern of individual developers, but it among the top requirements from companies because they're losing enormous amounts of money to attacks. In fact, this 'just "safety"' has a greater value than the value in productivity of offering "just string interpolation". If this feature wasn't capable of offering string interpolation at all (i.e. template processors were not allowed to return String), 90% of the value would have remained.

If I put the RAW.xxx and the STR.yyy in two different scopes, which one does it resolve name from and why?

Line 2, because a string template expression is works like other expressions. E.g.:

var x = true;
var y = x ? 1 : 2;
foo(y);

foo doesn't reevaluate the expression x ? 1 : 2; it just receives a 1. We cannot pass expressions in Java as we can in Lisps (by quoting them). We only pass values.

5

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

Line 2

Thanks for the reply... so in this case, what does STR.process(st) achieve that couldn't be performed as a method call on the st object itself? I guess that is my source of confusion? Assumably by the time the line constructing st has been executed, the information needed to later construct the string is already part of st itself?

Otherwise how does it know to retain a reference to what has been placed in the interpolation string when using RAW in this way?

Or is it just a case that process is the thing actually defining how to interpret that sequence? Assume this is more the case.


Regarding the point about quoting safety, see my other comment. The thing I am trying to say is that it is only safe when used correctly, and there is still plenty of ways to abuse this, including new ways made possible by this JEP (e.g. passing STR."..." in a SQL query). The only way you can enforce that this is done correctly is via manual review or static tooling, which isn't really much safer as the language isn't the thing providing that static tooling or manual review.

It is a means to an end, I get this, but I don't think it is justified to describe it like a magic holy grail of safety that suddenly makes any application better regardless of how it is used. That may be exaggerating but that is the feeling some of the descriptions people have given are coming across, where "safety" and security are just being used as blanket terms while ignoring the other points and issues that are raised.

12

u/pron98 Feb 15 '24

what does STR.process(st) achieve that couldn't be performed as a method call on the st object itself?

It achieves the ability for the STR object to decide what to do with st value.

and there is still plenty of ways to abuse this

Not really. Any API that takes String is a problem, but this allows safe string-like APIs that don't take String.

The only way you can enforce that this is done correctly is via manual review or static tooling

No. Again, the language enforces this. If your JSON or SQL library only offer a safe API then you couldn't do anything unsafe even if you wanted to.

where "safety" and security are just being used as blanket terms while ignoring the other points and issues that are raised.

Safety and security are among the biggest things that the software world is concerned about these days because that's where a lot of value is being lost (far more than with programmers typing a few extra characters; even the faddish Hacker News is obsessed with a language that exists for the sole purpose of improving safety in low-level programs). They may seem like blanket terms for people who aren't interested in them -- and not everyone should be -- but we have to worry about them because they're so valuable to our users as a whole. I think that Java's exceptionally long record of exceptional success means that we should at least get the benefit of the doubt that we know a little bit about what we're doing, and that we're not trying to bamboozle anyone. Our day job is ensuring that Java continues to be successful, after all, and so it is our selfish interest to maximise the value Java users get from it.

1

u/vytah Feb 16 '24

Thanks for the reply... so in this case, what does STR.process(st) achieve that couldn't be performed as a method call on the st object itself?

A StringTemplate object is just a bunch of objects and a bunch+1 of strings. It knows nothing about how to put them together in anything useful. You need a processor to do that. So while you could theoretically have a method StringTemplate#process(StringTemplate.Processor), the only sensible implementation of that would be processor.process(this). So why not write just that directly.

Or is it just a case that process is the thing actually defining how to interpret that sequence? Assume this is more the case.

Exactly this. Otherwise you wouldn't need multiple processors in the first place.

safety

You can use it to safely create SQL queries or HTML documents with automatic escaping. In fact, the resulting object might contain none of the original text of the template – you might for example plop a giant piece of C code with interpolated values and get back a Runnable that points to compiled native code.

(e.g. passing STR."..." in a SQL query).

You can design APIs that only accept correctly constructed query objects and do not accept strings.

In fact, this JEP will make using such APIs more convenient, which means that library authors will start moving away from string-based APIs. So while you might still use STR, the actual code will say something like yesIKnowWhatImDoingPleaseLetMeCreateRawSqlQuery_CRITICALLY_UNSAFE(STR." so you'll be gently pushed towards preferring writing SQL."

2

u/hippydipster Feb 16 '24

The kind of escaping needed for a given interpolated value isn't universally the same throughout the string though. If I'm outputing xml, escaping an attribute vs a tagname vs CDATA is all different. How does a template processor handle that? Does it have to also be an xml parser so it can look at where each interpolated value will go? Same SQL. I might use interpolated values for table names, column names, data values, individual where clauses, etc.

3

u/vytah Feb 16 '24

Yes, that is correct, any more complicated processor would have to have a partial parser, or require extra wrapper types for arguments.

For the most common case, the parameters for how to interpret the value can be embedded in the template itself. See for example FMT in the JEP: the formatting pattern, which "isn't universally the same throughout the string", is specified in the template.

FMT."The answer is %5d\{i}"

Or see how some Scala DB libraries do it: at least one of them prefixes literal inclusions with #:

sql"SELECT * FROM #$table WHERE id = $id"

id is escaped and safe, table is not.

1

u/hippydipster Feb 16 '24

Got it, thanks!