-
-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XWIKI-22726: Allow customizing the validation of HQL queries through …
…configuration (#3747) (#3763) * introduce an (internal) extensible validation system HQL queries * introduce a new validator based on configurable regex of safe/unsafe regex patterns (cherry picked from commit 620256c)
- Loading branch information
1 parent
a2b4898
commit 0b26038
Showing
11 changed files
with
591 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...src/main/java/org/xwiki/query/hql/internal/ConfigurableHQLCompleteStatementValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.xwiki.query.hql.internal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.annotation.Priority; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
import org.xwiki.component.annotation.Component; | ||
import org.xwiki.component.descriptor.ComponentDescriptor; | ||
import org.xwiki.component.phase.Initializable; | ||
import org.xwiki.component.phase.InitializationException; | ||
import org.xwiki.configuration.ConfigurationSource; | ||
|
||
/** | ||
* A validator which can be configured. This validator is executer before the default one using component priorities | ||
* | ||
* @version $Id$ | ||
* @since 17.0.0RC1 | ||
* @since 16.10.2 | ||
* @since 15.10.16 | ||
* @since 16.4.6 | ||
*/ | ||
@Component | ||
@Singleton | ||
@Named("configurable") | ||
@Priority(ComponentDescriptor.DEFAULT_PRIORITY - 100) | ||
public class ConfigurableHQLCompleteStatementValidator implements HQLCompleteStatementValidator, Initializable | ||
{ | ||
@Inject | ||
@Named("xwikiproperties") | ||
private ConfigurationSource configuration; | ||
|
||
private List<Pattern> unsafe; | ||
|
||
private List<Pattern> safe; | ||
|
||
@Override | ||
public void initialize() throws InitializationException | ||
{ | ||
this.unsafe = getPatterns("query.hql.unsafe"); | ||
this.safe = getPatterns("query.hql.safe"); | ||
} | ||
|
||
private List<Pattern> getPatterns(String key) throws InitializationException | ||
{ | ||
List<String> patternStrings = this.configuration.getProperty(key, List.class); | ||
|
||
List<Pattern> patterns; | ||
if (patternStrings != null) { | ||
patterns = new ArrayList<>(patternStrings.size()); | ||
for (String patternString : patternStrings) { | ||
try { | ||
patterns.add(Pattern.compile(patternString)); | ||
} catch (Exception e) { | ||
throw new InitializationException( | ||
String.format("Failed to parse pattern [%s] for configuration [%s]", patternString, key), e); | ||
} | ||
} | ||
} else { | ||
patterns = List.of(); | ||
} | ||
|
||
return patterns; | ||
} | ||
|
||
@Override | ||
public Optional<Boolean> isSafe(String statement) | ||
{ | ||
for (Pattern pattern : this.unsafe) { | ||
if (pattern.matcher(statement).matches()) { | ||
return Optional.of(Boolean.FALSE); | ||
} | ||
} | ||
|
||
for (Pattern pattern : this.safe) { | ||
if (pattern.matcher(statement).matches()) { | ||
return Optional.of(Boolean.TRUE); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
} |
Oops, something went wrong.