Skip to content

Commit 2c031c4

Browse files
[MOD] XQuery: Permissions of standard functions. Closes #2384
1 parent ef093e0 commit 2c031c4

20 files changed

Lines changed: 406 additions & 109 deletions

basex-core/src/main/java/org/basex/query/QueryError.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ public enum QueryError {
5757
/** Error code. */
5858
BASEX_OVERFLOW(BASEX, "overflow", "Stack Overflow: Try tail recursion?"),
5959
/** Error code. */
60-
BASEX_PERMISSION_X(BASEX, "permission", "No % permission."),
61-
/** Error code. */
6260
BASEX_PERMISSION_X_X(BASEX, "permission", "No % permission: %."),
6361
/** Error code. */
6462
BASEX_RESTXQ_X(BASEX, "restxq", "%"),

basex-core/src/main/java/org/basex/query/QueryResources.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,13 @@ public synchronized void addFunctions(final String path, final Value funcs) {
171171
* Opens a new database or returns a reference to an already opened database.
172172
* @param name name of database
173173
* @param user current user
174+
* @param updating updating access
174175
* @param info input info (can be {@code null})
175176
* @return database instance
176177
* @throws QueryException query exception
177178
*/
178-
public synchronized Data database(final String name, final User user, final InputInfo info)
179-
throws QueryException {
179+
public synchronized Data database(final String name, final User user, final boolean updating,
180+
final InputInfo info) throws QueryException {
180181

181182
final boolean mainmem = context.options.get(MainOptions.MAINMEM);
182183

@@ -188,7 +189,8 @@ public synchronized Data database(final String name, final User user, final Inpu
188189
}
189190

190191
// open and register database
191-
if(!user.has(Perm.READ, name)) throw BASEX_PERMISSION_X_X.get(info, Perm.READ, name);
192+
final Perm perm = updating ? Perm.WRITE : Perm.READ;
193+
if(!user.has(perm, name)) throw BASEX_PERMISSION_X_X.get(info, perm, name);
192194
try {
193195
return addData(Open.open(name, context, context.options, true, false));
194196
} catch(final IOException ex) {

basex-core/src/main/java/org/basex/query/expr/ParseExpr.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.*;
88

9+
import org.basex.core.users.*;
910
import org.basex.data.*;
1011
import org.basex.query.*;
1112
import org.basex.query.ann.*;
@@ -214,6 +215,29 @@ protected final void checkAllUp(final Expr... exprs) throws QueryException {
214215
}
215216
}
216217

218+
/**
219+
* Checks if the current user has the given permissions. If negative, an exception is thrown.
220+
* @param qc query context
221+
* @param perm permission
222+
* @throws QueryException query exception
223+
*/
224+
protected void checkPerm(final QueryContext qc, final Perm perm) throws QueryException {
225+
if(!qc.user.has(perm)) throw BASEX_PERMISSION_X_X.get(info, perm, this);
226+
}
227+
228+
/**
229+
* Checks if the current user has the given permissions for the specified database.
230+
* If negative, an exception is thrown.
231+
* @param qc query context
232+
* @param perm permission
233+
* @param name name of resource
234+
* @throws QueryException query exception
235+
*/
236+
protected void checkPerm(final QueryContext qc, final Perm perm, final String name)
237+
throws QueryException {
238+
if(!qc.user.has(perm, name)) throw BASEX_PERMISSION_X_X.get(info(), perm, this);
239+
}
240+
217241
/**
218242
* Returns the current context value or throws an exception if the context value is not set.
219243
* @param qc query context

basex-core/src/main/java/org/basex/query/func/Function.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ ARRAY_ZM, flag(HOF)),
542542
params(QNAME_ZO), NCNAME_ZO),
543543
/** XQuery function. */
544544
PUT(FnPut::new, "put(node,source[,options])",
545-
params(NODE_O, STRING_ZO, ITEM_ZO), EMPTY_SEQUENCE_Z, flag(UPD), FN_URI, Perm.CREATE),
545+
params(NODE_O, STRING_ZO, ITEM_ZO), EMPTY_SEQUENCE_Z, flag(UPD), FN_URI, Perm.ADMIN),
546546
/** XQuery function. */
547547
QNAME(FnQName::new, "QName(uri,qname)",
548548
params(STRING_ZO, STRING_O), QNAME_O),
@@ -1245,7 +1245,7 @@ ANY_URI_O, flag(NDT), CLIENT_URI, Perm.CREATE),
12451245
params(STRING_O, STRING_O), BOOLEAN_O, flag(NDT), DB_URI),
12461246
/** XQuery function. */
12471247
_DB_EXPORT(DbExport::new, "export(database,path[,param])",
1248-
params(STRING_O, STRING_O, ITEM_O), EMPTY_SEQUENCE_Z, flag(NDT), DB_URI, Perm.CREATE),
1248+
params(STRING_O, STRING_O, ITEM_O), EMPTY_SEQUENCE_Z, flag(NDT), DB_URI, Perm.ADMIN),
12491249
/** XQuery function. */
12501250
_DB_FLUSH(DbFlush::new, "flush(database)",
12511251
params(ITEM_O), EMPTY_SEQUENCE_Z, flag(UPD), DB_URI),

basex-core/src/main/java/org/basex/query/func/StandardFunc.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -647,17 +647,7 @@ protected final HashMap<String, Value> toBindings(final Expr expr, final QueryCo
647647
protected final Data toData(final QueryContext qc) throws QueryException {
648648
final Data data = exprType.data();
649649
return data != null ? data : qc.resources.database(
650-
toName(arg(0), false, DB_NAME_X, qc), qc.user, info);
651-
}
652-
653-
/**
654-
* Checks if the current user has given permissions. If negative, an exception is thrown.
655-
* @param qc query context
656-
* @param perm permission
657-
* @throws QueryException query exception
658-
*/
659-
protected void checkPerm(final QueryContext qc, final Perm perm) throws QueryException {
660-
if(perm != Perm.NONE && !qc.user.has(perm)) throw BASEX_PERMISSION_X_X.get(info, perm, this);
650+
toName(arg(0), false, DB_NAME_X, qc), qc.user, definition.has(Flag.UPD), info);
661651
}
662652

663653
/**

basex-core/src/main/java/org/basex/query/func/db/DbAlterBackup.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.basex.query.QueryError.*;
44

5+
import org.basex.core.users.*;
56
import org.basex.query.*;
67
import org.basex.query.up.*;
78
import org.basex.query.up.primitives.name.*;
@@ -23,6 +24,7 @@ public Item item(final QueryContext qc, final InputInfo ii) throws QueryExceptio
2324
final String name = toName(arg(0), false, qc), newname = toName(arg(1), false, qc);
2425
if(name.equals(newname)) throw DB_CONFLICT4_X.get(info, name, newname);
2526

27+
checkPerm(qc, Perm.CREATE, name);
2628
final StringList backups = qc.context.databases.backups(name);
2729
if(backups.isEmpty()) throw DB_NOBACKUP_X.get(info, name);
2830

basex-core/src/main/java/org/basex/query/func/db/DbCopy.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.basex.query.QueryError.*;
44

5+
import org.basex.core.users.*;
56
import org.basex.query.*;
67
import org.basex.query.up.primitives.name.*;
78
import org.basex.query.util.*;
@@ -33,6 +34,7 @@ final void copy(final QueryContext qc, final boolean keep) throws QueryException
3334
if(name.equals(newname)) throw DB_CONFLICT4_X.get(info, name, newname);
3435

3536
// source database does not exist
37+
checkPerm(qc, Perm.CREATE, name);
3638
if(!qc.context.soptions.dbExists(name)) throw DB_OPEN1_X.get(info, name);
3739

3840
qc.updates().add(keep ? new DBCopy(name, newname, qc, info) :

basex-core/src/main/java/org/basex/query/func/db/DbCreate.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.*;
66

7+
import org.basex.core.users.*;
78
import org.basex.query.*;
89
import org.basex.query.iter.*;
910
import org.basex.query.up.primitives.*;
@@ -24,6 +25,7 @@ public final class DbCreate extends DbNew {
2425
@Override
2526
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
2627
final String name = toName(arg(0), false, qc);
28+
checkPerm(qc, Perm.CREATE, name);
2729

2830
final StringList paths = new StringList();
2931
final Iter iter = arg(2).iter(qc);

basex-core/src/main/java/org/basex/query/func/db/DbCreateBackup.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.basex.query.QueryError.*;
44

5+
import org.basex.core.users.*;
56
import org.basex.query.*;
67
import org.basex.query.up.primitives.name.*;
78
import org.basex.query.value.item.*;
@@ -19,6 +20,8 @@ public final class DbCreateBackup extends BackupFn {
1920
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
2021
final String name = toName(arg(0), true, qc);
2122
final CreateBackupOptions options = toOptions(arg(1), new CreateBackupOptions(), qc);
23+
24+
checkPerm(qc, Perm.CREATE, name);
2225
if(!name.isEmpty() && !qc.context.soptions.dbExists(name)) throw DB_OPEN1_X.get(info, name);
2326

2427
final String comment = options.get(CreateBackupOptions.COMMENT);

basex-core/src/main/java/org/basex/query/func/db/DbDrop.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.basex.query.QueryError.*;
44

5+
import org.basex.core.users.*;
56
import org.basex.query.*;
67
import org.basex.query.up.primitives.name.*;
78
import org.basex.query.value.item.*;
@@ -18,7 +19,10 @@ public final class DbDrop extends DbAccessFn {
1819
@Override
1920
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
2021
final String name = toName(arg(0), false, qc);
22+
23+
checkPerm(qc, Perm.CREATE, name);
2124
if(!qc.context.soptions.dbExists(name)) throw DB_OPEN1_X.get(info, name);
25+
2226
qc.updates().add(new DBDrop(name, qc, info), qc);
2327
return Empty.VALUE;
2428
}

0 commit comments

Comments
 (0)