44
44
import org .xwiki .model .reference .PageReference ;
45
45
import org .xwiki .model .reference .SpaceReference ;
46
46
import org .xwiki .model .reference .WikiReference ;
47
+ import org .xwiki .query .hql .internal .HQLStatementValidator ;
47
48
import org .xwiki .rendering .renderer .PrintRendererFactory ;
48
49
import org .xwiki .rendering .syntax .Syntax ;
49
50
import org .xwiki .security .authorization .AuthorizationException ;
58
59
import com .xpn .xwiki .doc .XWikiDocument ;
59
60
import com .xpn .xwiki .internal .XWikiInitializerJob ;
60
61
import com .xpn .xwiki .internal .XWikiInitializerJobStatus ;
62
+ import com .xpn .xwiki .internal .store .hibernate .query .HqlQueryUtils ;
61
63
import com .xpn .xwiki .objects .meta .MetaClass ;
62
64
import com .xpn .xwiki .user .api .XWikiUser ;
63
65
import com .xpn .xwiki .util .Programming ;
@@ -105,6 +107,8 @@ public class XWiki extends Api
105
107
106
108
private ContextualAuthorizationManager contextualAuthorizationManager ;
107
109
110
+ private HQLStatementValidator hqlValidator ;
111
+
108
112
/**
109
113
* XWiki API Constructor
110
114
*
@@ -167,6 +171,15 @@ private DocumentRevisionProvider getDocumentRevisionProvider()
167
171
return this .documentRevisionProvider ;
168
172
}
169
173
174
+ private HQLStatementValidator getHQLStatementValidator ()
175
+ {
176
+ if (this .hqlValidator == null ) {
177
+ this .hqlValidator = Utils .getComponent (HQLStatementValidator .class );
178
+ }
179
+
180
+ return this .hqlValidator ;
181
+ }
182
+
170
183
/**
171
184
* Privileged API allowing to access the underlying main XWiki Object
172
185
*
@@ -710,6 +723,23 @@ public MetaClass getMetaclass()
710
723
return this .xwiki .getMetaclass ();
711
724
}
712
725
726
+ private void checkSearchQueryAllowed (String whereSQL ) throws XWikiException
727
+ {
728
+ if (!hasProgrammingRights ()) {
729
+ try {
730
+ if (!getHQLStatementValidator ()
731
+ .isSafe (HqlQueryUtils .createLegacySQLQuery ("select distinct doc.fullName" , whereSQL ))) {
732
+ throw new XWikiException (XWikiException .MODULE_XWIKI_STORE ,
733
+ XWikiException .ERROR_XWIKI_ACCESS_DENIED ,
734
+ "The query [" + whereSQL + "] requires programming right" );
735
+ }
736
+ } catch (Exception e ) {
737
+ throw new XWikiException (XWikiException .MODULE_XWIKI_STORE , XWikiException .ERROR_XWIKI_ACCESS_DENIED ,
738
+ "Failed to validate the query [" + whereSQL + "], requiring programming right" , e );
739
+ }
740
+ }
741
+ }
742
+
713
743
/**
714
744
* API allowing to search for document names matching a query. Examples:
715
745
* <ul>
@@ -742,6 +772,8 @@ public MetaClass getMetaclass()
742
772
@ Deprecated
743
773
public List <String > searchDocuments (String wheresql ) throws XWikiException
744
774
{
775
+ checkSearchQueryAllowed (wheresql );
776
+
745
777
return this .xwiki .getStore ().searchDocumentsNames (wheresql , getXWikiContext ());
746
778
}
747
779
@@ -760,6 +792,8 @@ public List<String> searchDocuments(String wheresql) throws XWikiException
760
792
@ Deprecated
761
793
public List <String > searchDocuments (String wheresql , int nb , int start ) throws XWikiException
762
794
{
795
+ checkSearchQueryAllowed (wheresql );
796
+
763
797
return this .xwiki .getStore ().searchDocumentsNames (wheresql , nb , start , getXWikiContext ());
764
798
}
765
799
@@ -796,6 +830,8 @@ public List<String> searchDocuments(String wheresql, int nb, int start, String s
796
830
*/
797
831
public List <Document > searchDocuments (String wheresql , boolean distinctbylocale ) throws XWikiException
798
832
{
833
+ checkSearchQueryAllowed (wheresql );
834
+
799
835
return convert (this .xwiki .getStore ().searchDocuments (wheresql , distinctbylocale , getXWikiContext ()));
800
836
}
801
837
@@ -812,6 +848,8 @@ public List<Document> searchDocuments(String wheresql, boolean distinctbylocale)
812
848
public List <Document > searchDocuments (String wheresql , boolean distinctbylocale , int nb , int start )
813
849
throws XWikiException
814
850
{
851
+ checkSearchQueryAllowed (wheresql );
852
+
815
853
return convert (this .xwiki .getStore ().searchDocuments (wheresql , distinctbylocale , nb , start , getXWikiContext ()));
816
854
}
817
855
@@ -845,6 +883,8 @@ public List<Document> searchDocuments(String wheresql, boolean distinctbylocale,
845
883
public List <String > searchDocuments (String parameterizedWhereClause , int maxResults , int startOffset ,
846
884
List <?> parameterValues ) throws XWikiException
847
885
{
886
+ checkSearchQueryAllowed (parameterizedWhereClause );
887
+
848
888
return this .xwiki .getStore ().searchDocumentsNames (parameterizedWhereClause , maxResults , startOffset ,
849
889
parameterValues , getXWikiContext ());
850
890
}
@@ -858,6 +898,8 @@ public List<String> searchDocuments(String parameterizedWhereClause, int maxResu
858
898
@ Deprecated
859
899
public List <String > searchDocuments (String parameterizedWhereClause , List <?> parameterValues ) throws XWikiException
860
900
{
901
+ checkSearchQueryAllowed (parameterizedWhereClause );
902
+
861
903
return this .xwiki .getStore ().searchDocumentsNames (parameterizedWhereClause , parameterValues , getXWikiContext ());
862
904
}
863
905
@@ -885,6 +927,8 @@ public List<String> searchDocumentsNames(String wikiName, String parameterizedWh
885
927
try {
886
928
this .context .setWikiId (wikiName );
887
929
930
+ checkSearchQueryAllowed (parameterizedWhereClause );
931
+
888
932
return searchDocuments (parameterizedWhereClause , maxResults , startOffset , parameterValues );
889
933
} finally {
890
934
this .context .setWikiId (database );
@@ -895,18 +939,20 @@ public List<String> searchDocumentsNames(String wikiName, String parameterizedWh
895
939
* Search spaces by passing HQL where clause values as parameters. See
896
940
* {@link #searchDocuments(String, int, int, List)} for more about parameterized hql clauses.
897
941
*
898
- * @param parametrizedSqlClause the HQL where clause. For example
942
+ * @param parameterizedSqlClause the HQL where clause. For example
899
943
* {@code where doc.fullName <> ?1 and (doc.parent = ?2 or (doc.parent = ?3 and doc.space = ?4))}
900
944
* @param nb the number of rows to return. If 0 then all rows are returned
901
945
* @param start the number of rows to skip. If 0 don't skip any row
902
946
* @param parameterValues the where clause values that replace the question marks (?)
903
947
* @return a list of spaces names.
904
948
* @throws XWikiException in case of error while performing the query
905
949
*/
906
- public List <String > searchSpacesNames (String parametrizedSqlClause , int nb , int start , List <?> parameterValues )
950
+ public List <String > searchSpacesNames (String parameterizedSqlClause , int nb , int start , List <?> parameterValues )
907
951
throws XWikiException
908
952
{
909
- return this .xwiki .getStore ().search ("select distinct doc.space from XWikiDocument doc " + parametrizedSqlClause ,
953
+ checkSearchQueryAllowed (parameterizedSqlClause );
954
+
955
+ return this .xwiki .getStore ().search ("select distinct doc.space from XWikiDocument doc " + parameterizedSqlClause ,
910
956
nb , start , parameterValues , this .context );
911
957
}
912
958
@@ -915,7 +961,7 @@ public List<String> searchSpacesNames(String parametrizedSqlClause, int nb, int
915
961
* {@link #searchDocuments(String, int, int, List)} for more about parameterized hql clauses. You can specify
916
962
* properties of attach (the attachment) or doc (the document it is attached to)
917
963
*
918
- * @param parametrizedSqlClause The HQL where clause. For example
964
+ * @param parameterizedSqlClause The HQL where clause. For example
919
965
* {@code where doc.fullName <> ?1 and (doc.parent = ?2 or (doc.parent = ?3 and doc.space = ?4))}
920
966
* @param nb The number of rows to return. If 0 then all rows are returned
921
967
* @param start The number of rows to skip at the beginning.
@@ -924,27 +970,31 @@ public List<String> searchSpacesNames(String parametrizedSqlClause, int nb, int
924
970
* @throws XWikiException in case of error while performing the query
925
971
* @since 5.0M2
926
972
*/
927
- public List <Attachment > searchAttachments (String parametrizedSqlClause , int nb , int start , List <?> parameterValues )
973
+ public List <Attachment > searchAttachments (String parameterizedSqlClause , int nb , int start , List <?> parameterValues )
928
974
throws XWikiException
929
975
{
976
+ checkSearchQueryAllowed (parameterizedSqlClause );
977
+
930
978
return convertAttachments (
931
- this .xwiki .searchAttachments (parametrizedSqlClause , true , nb , start , parameterValues , this .context ));
979
+ this .xwiki .searchAttachments (parameterizedSqlClause , true , nb , start , parameterValues , this .context ));
932
980
}
933
981
934
982
/**
935
983
* Count attachments returned by a given parameterized query
936
984
*
937
- * @param parametrizedSqlClause Everything which would follow the "WHERE" in HQL see:
985
+ * @param parameterizedSqlClause Everything which would follow the "WHERE" in HQL see:
938
986
* {@link #searchDocuments(String, int, int, List)}
939
987
* @param parameterValues A {@link java.util.List} of the where clause values that replace the question marks (?)
940
988
* @return int number of attachments found.
941
989
* @throws XWikiException
942
990
* @see #searchAttachments(String, int, int, List)
943
991
* @since 5.0M2
944
992
*/
945
- public int countAttachments (String parametrizedSqlClause , List <?> parameterValues ) throws XWikiException
993
+ public int countAttachments (String parameterizedSqlClause , List <?> parameterValues ) throws XWikiException
946
994
{
947
- return this .xwiki .countAttachments (parametrizedSqlClause , parameterValues , this .context );
995
+ checkSearchQueryAllowed (parameterizedSqlClause );
996
+
997
+ return this .xwiki .countAttachments (parameterizedSqlClause , parameterValues , this .context );
948
998
}
949
999
950
1000
/**
0 commit comments