Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate column names of sort properties #8022

Closed
wants to merge 2 commits into from

Conversation

glefloch
Copy link
Member

close #1120

Throws an IllegalArgumenException if column names used in Sort expression are not valid.

@@ -41,6 +42,8 @@
}

public class Column {

private final Pattern COLUMN_PATTERN = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9_]*$");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final Pattern COLUMN_PATTERN = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9_]*$");
private static final Pattern COLUMN_PATTERN = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9_]*$");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this is OK and covering all the possibilities? Where does this pattern come from?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the beginning, I was more thinking about escaping the column names. And the hard part is probably to get the dialect at some point to get it to properly escape things.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gsmet, as you said, this pattern may not be valid for all databases.
Can we do a simple quote escape here ? Getting the dialect here seems complicated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree it's probably be a bit difficult, this can cause all sorts of security issues so I would prefer we get this right.

@Sanne any advice on how we could get Hibernate ORM to properly escape this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks suspicious indeed. Why does it need to be validated, couldn't we pass the sort intruction to ORM directly?

Some background: in ORM 5.x there have been various attempts in the Dialets to automagically escape / veto column names, they had all to be rolled back as they would break in various ways - I don't remember the details but yes it's dangeour to impose such a rule without a thoutough analysis.

The proper solution is included in the design of ORM6, so I would suggest to just let the user be responsible for using the right names.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that the sort column is directly appended to the generated HQL so you basically end up with an HQL injection if you're not cautious about it.

It somehow needs to be properly quoted/escaped/whatever makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but is "HQL injection" actually a dangeous thing? We're not sending it to the DB unparsed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, unparsed no, sure. But can't you completely change the meaning of the query just with carefully forged sort column?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this could be explored - never seen it done. Also I just realized that these columns names are appended into HQL, didn't expect that

@@ -49,6 +52,9 @@ public Column(String name) {
}

public Column(String name, Direction direction) {
if (COLUMN_PATTERN.asPredicate().negate().test(name)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (COLUMN_PATTERN.asPredicate().negate().test(name)) {
if (!COLUMN_PATTERN.matcher(name).matches()) {

@gastaldi
Copy link
Contributor

I think the original issue also asks to escape (quote) the properties used in the SORT BY statement?

@glefloch glefloch force-pushed the fix/1120 branch 2 times, most recently from 6d7fabb to 1dc5588 Compare April 6, 2020 08:52
@gsmet
Copy link
Member

gsmet commented Oct 12, 2020

I'm closing this one as out of date but the issue remains.

@gsmet gsmet closed this Oct 12, 2020
@gsmet gsmet added the triage/out-of-date This issue/PR is no longer valid or relevant label Oct 12, 2020
@glefloch glefloch deleted the fix/1120 branch October 13, 2020 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/panache triage/out-of-date This issue/PR is no longer valid or relevant
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Panache - Sort properties injected in the query should be escaped somehow
4 participants