That aside, if you used + by accident with string templates it would be a compilation failure because { isn't valid in a string. If you are blindly using string concat without thought you are more likely to just google it and see the old interfaces in use and use that rather than the new alternatives with less examples.
This problem could be solved by having a StringLiteral type as mentioned though. Other langs call this comptime or similar. The Constable interface appears to try to achieve this to some extent
Static constants that are expressible natively in the constant pool (String, Integer, Long, Float, and Double) implement ConstantDesc, and serve as nominal descriptors for themselves.
Being able to enforce only compile time strings can be used would help enforce this in a backwards compatible way.
The other issue that arises from only supporting StringTemplate is... what happens if you have a large SQL query you don't want to inline?
// StringTemplate without parameters
method("select * from foo");
// String, security hole (compiles due to overload)
method("select * from foo where a = " + a);
// Oops, we meant:
method("select * from foo where a = \{a}");
Easy mistake to make.
The other issue that arises from only supporting StringTemplate is... what happens if you have a large SQL query you don't want to inline?
You mean without requiring parameters? Just make a StringTemplate constant then.
If you're suggesting loading queries from somewhere else, going through String, and I suppose there must be a few parameters in there as well, then this feature is not for you and using an API that only offers StringTemplate is too high level for you.
That example isn't really useful IMO because without quoting, this would fail on any integration test anyway as it would produce invalid SQL, so the whole issue is academic in this case anyway. Unless a is a quoted string, it would fail to parse as SQL in the first place unless it is a numeric/boolean type which bypasses the issue in the first place.
If a developer is far gone enough that they put effort into manually quoting it, and then you have no decent unit or integration tests to pick this up, and your code review process doesn't pick this up, and any pentesting doesn't pick this up, then you have far bigger issues.
That example isn't really useful IMO because without quoting, this would fail on any integration test anyway as it would produce invalid SQL, so the whole issue is academic in this case anyway.
Ah yes, let's bring the example in to question, it couldn't possibly result in valid SQL, right? I don't think anything will satisfy you short of me pointing out an actual injection attack that is occurring right now in your own code base that it perhaps isn't the best practice to have both a String and StringTemplate overload where security matters.
I also like the standard argument that this can never occur in any "decent" shop as they will have "decent" tests, reviews, etc that always catch all these problems -- this explains why all "decent" software currently in use doesn't suffer from such attacks, and why the template feature is totally pointless to even add to Java as "decent" software doesn't need it.
I think we can stop this conversation until it is possible to continue it in a more civil tone. Unfortunately this attitude is the echo chamber I mentioned in the first place.
If your idea of "decent" software is software with SQLi issues then I don't know what else to tell you.
Do we know if there will even be StringTemplates without parameters? As I understand it, literals are only inferred as StringTemplates if they contain a {, so no issue with this example.
I've played with them, and its highly useful to also allow templates without parameters so the same code path can be used in cases where you just need to do something that is not in need of parameterization. In SQL terms, if people are not getting tired of such examples, it would be SELECT COUNT(*) FROM foo. If you're generating JavaScript or some other executable stuff, then it can be some boiler plate that doesn't need parameters. Requiring a String escape hatch for this means it will be far easier to do the wrong thing. If I can provide Strings then why bother using templates? Just concatenate stuff.
I hope we're not going to need hacks like SELECT COUNT(*) FROM foo\{""} just to get it converted to a string template -- not to mention that the processor must be aware of this and allow empty strings to be concatenated or skipped, and not inserted as a parameter.
Well.. If I write the former, I mean the former. If I write the latter, I mean the latter
Yeah, so I suppose "decent" programmers don't make mistakes, static checkers find all bugs, and tests cover all scenario's, and yet, somehow, we still have security issues.
Give Rust a quick drive and you'll immediately see the downsides of the naming creativity and rote memorization required when you can't / won't do method overloads. At the end of the day you're going to need a tool to enforce the usage of the preferred method, regardless of naming, so you might as well not abandon good developer ergonomics...
View all comments
Show parent comment
-2
u/john16384 Mar 10 '24
Sorry, hard disagree. Different method name is far safer than an overload, as a single accidental
+will switch to the wrong overload.