Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Objects;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;
import org.eclipse.biscuit.token.DeserializationLimit;

public final class Check {
public enum Kind {
Expand Down Expand Up @@ -62,7 +63,8 @@ public Schema.CheckV2 serialize() {
return b.build();
}

public static Result<Check, Error.FormatError> deserializeV2(Schema.CheckV2 check) {
public static Result<Check, Error.FormatError> deserializeV2(
Schema.CheckV2 check, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
ArrayList<Rule> queries = new ArrayList<>();

Kind kind;
Expand All @@ -81,8 +83,20 @@ public static Result<Check, Error.FormatError> deserializeV2(Schema.CheckV2 chec
break;
}

var allowedKinds = limits.getAllowedCheckKinds();
if (allowedKinds != null && !allowedKinds.contains(kind)) {
return Result.err(
new Error.FormatError.DeserializationLimitError(
Error.FormatError.DeserializationLimitError.Reason.CHECK_KIND_NOT_ALLOWED));
}
if (check.getQueriesCount() > limits.getMaxQueriesPerCheck()) {
return Result.err(
new Error.FormatError.DeserializationLimitError(
Error.FormatError.DeserializationLimitError.Reason.TOO_MANY_QUERIES));
}

for (Schema.RuleV2 query : check.getQueriesList()) {
var res = Rule.deserializeV2(query);
var res = Rule.deserializeV2(query, limits, symbols);
if (res.isErr()) {
return Result.err(res.getErr());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Objects;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;
import org.eclipse.biscuit.token.DeserializationLimit;

public final class Fact {
private final Predicate predicate;
Expand Down Expand Up @@ -56,8 +57,9 @@ public Schema.FactV2 serialize() {
return Schema.FactV2.newBuilder().setPredicate(this.predicate.serialize()).build();
}

public static Result<Fact, Error.FormatError> deserializeV2(Schema.FactV2 fact) {
var res = Predicate.deserializeV2(fact.getPredicate());
public static Result<Fact, Error.FormatError> deserializeV2(
Schema.FactV2 fact, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
var res = Predicate.deserializeV2(fact.getPredicate(), limits, symbols);
if (res.isErr()) {
return Result.err(res.getErr());
} else {
Expand Down
42 changes: 35 additions & 7 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/MapKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,47 @@
import biscuit.format.schema.Schema;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;
import org.eclipse.biscuit.token.DeserializationLimit;

public abstract class MapKey extends Term {
public abstract Schema.MapKey serializeMapKey();

public abstract org.eclipse.biscuit.token.builder.MapKey toMapKey(SymbolTable symbolTable);

public static Result<MapKey, Error.FormatError> deserializeMapKeyEnum(Schema.MapKey mapKey) {
if (mapKey.hasInteger()) {
return Term.Integer.deserializeMapKey(mapKey);
} else if (mapKey.hasString()) {
return Term.Str.deserializeMapKey(mapKey);
} else {
return Result.err(new Error.FormatError.DeserializationError("invalid MapKey kind"));
public static Result<MapKey, Error.FormatError> deserializeMapKeyEnum(
Schema.MapKey mapKey, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
var allowedTypes = limits.getAllowedMapKeyTypes();
var type = mapKeyType(mapKey.getContentCase());
if (allowedTypes != null && type != null && !allowedTypes.contains(type)) {
return Result.err(
new Error.FormatError.DeserializationLimitError(
Error.FormatError.DeserializationLimitError.Reason.MAP_KEY_TYPE_NOT_ALLOWED));
}

switch (mapKey.getContentCase()) {
case INTEGER:
return Term.Integer.deserializeMapKey(mapKey);
case STRING:
var stringError = limits.checkString(symbols, mapKey.getString());
if (stringError != null) {
return Result.err(stringError);
}
return Term.Str.deserializeMapKey(mapKey);
default:
return Result.err(new Error.FormatError.DeserializationError("invalid MapKey kind"));
}
}

private static DeserializationLimit.MapKeyType mapKeyType(Schema.MapKey.ContentCase content) {
switch (content) {
case INTEGER:
return DeserializationLimit.MapKeyType.INTEGER;
case STRING:
return DeserializationLimit.MapKeyType.STRING;
case CONTENT_NOT_SET:
return null;
default:
throw new IllegalStateException("unhandled map key kind " + content);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.stream.Collectors;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;
import org.eclipse.biscuit.token.DeserializationLimit;

public final class Predicate {
private final long name;
Expand Down Expand Up @@ -92,10 +93,21 @@ public Schema.PredicateV2 serialize() {
return builder.build();
}

public static Result<Predicate, Error.FormatError> deserializeV2(Schema.PredicateV2 predicate) {
public static Result<Predicate, Error.FormatError> deserializeV2(
Schema.PredicateV2 predicate, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
if (predicate.getTermsCount() > limits.getMaxTermsPerPredicate()) {
return Result.err(
new Error.FormatError.DeserializationLimitError(
Error.FormatError.DeserializationLimitError.Reason.TOO_MANY_TERMS));
}
var nameError = limits.checkName(symbols, predicate.getName());
if (nameError != null) {
return Result.err(nameError);
}

ArrayList<Term> terms = new ArrayList<>();
for (Schema.TermV2 id : predicate.getTermsList()) {
var res = Term.deserializeEnumV2(id);
var res = Term.deserializeEnumV2(id, limits, symbols);
if (res.isErr()) {
return Result.err(res.getErr());
} else {
Expand Down
23 changes: 18 additions & 5 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.biscuit.datalog.expressions.Expression;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;
import org.eclipse.biscuit.token.DeserializationLimit;

public final class Rule {
private final Predicate head;
Expand Down Expand Up @@ -224,10 +225,22 @@ public Schema.RuleV2 serialize() {
return b.build();
}

public static Result<Rule, Error.FormatError> deserializeV2(Schema.RuleV2 rule) {
public static Result<Rule, Error.FormatError> deserializeV2(
Schema.RuleV2 rule, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
if (rule.getBodyCount() > limits.getMaxPredicatesPerBody()) {
return Result.err(
new Error.FormatError.DeserializationLimitError(
Error.FormatError.DeserializationLimitError.Reason.TOO_MANY_PREDICATES));
}
if (rule.getExpressionsCount() > limits.getMaxExpressionsPerBody()) {
return Result.err(
new Error.FormatError.DeserializationLimitError(
Error.FormatError.DeserializationLimitError.Reason.TOO_MANY_EXPRESSIONS));
}

ArrayList<Predicate> body = new ArrayList<>();
for (Schema.PredicateV2 predicate : rule.getBodyList()) {
var res = Predicate.deserializeV2(predicate);
var res = Predicate.deserializeV2(predicate, limits, symbols);
if (res.isErr()) {
return Result.err(res.getErr());
} else {
Expand All @@ -237,7 +250,7 @@ public static Result<Rule, Error.FormatError> deserializeV2(Schema.RuleV2 rule)

ArrayList<Expression> expressions = new ArrayList<>();
for (Schema.ExpressionV2 expression : rule.getExpressionsList()) {
var res = Expression.deserializeV2(expression);
var res = Expression.deserializeV2(expression, limits, symbols);
if (res.isErr()) {
return Result.err(res.getErr());
} else {
Expand All @@ -247,15 +260,15 @@ public static Result<Rule, Error.FormatError> deserializeV2(Schema.RuleV2 rule)

ArrayList<Scope> scopes = new ArrayList<>();
for (Schema.Scope scope : rule.getScopeList()) {
var res = Scope.deserialize(scope);
var res = Scope.deserialize(scope, limits, symbols);
if (res.isErr()) {
return Result.err(res.getErr());
} else {
scopes.add(res.getOk());
}
}

var res = Predicate.deserializeV2(rule.getHead());
var res = Predicate.deserializeV2(rule.getHead(), limits, symbols);
if (res.isErr()) {
Error.FormatError e = res.getErr();
return Result.err(e);
Expand Down
44 changes: 44 additions & 0 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
package org.eclipse.biscuit.datalog;

import biscuit.format.schema.Schema;
import java.util.Optional;
import org.eclipse.biscuit.crypto.PublicKey;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;
import org.eclipse.biscuit.token.DeserializationLimit;

public final class Scope {
public enum Kind {
Expand Down Expand Up @@ -63,6 +66,47 @@ public Schema.Scope serialize() {
return b.build();
}

public static Result<Scope, Error.FormatError> deserialize(
Schema.Scope scope, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
var res = deserialize(scope);
if (res.isErr()) {
return res;
}

var allowedScopes = limits.getAllowedTrustingScopes();
if (allowedScopes != null) {
DeserializationLimit.TrustedScopeKind kind;
switch (res.getOk().kind()) {
case Authority:
kind = DeserializationLimit.TrustedScopeKind.AUTHORITY;
break;
case Previous:
kind = DeserializationLimit.TrustedScopeKind.PREVIOUS;
break;
case PublicKey:
long index = res.getOk().getPublicKey();
var key =
index >= 0 && index <= java.lang.Integer.MAX_VALUE
? symbols.getPublicKey((int) index)
: Optional.<PublicKey>empty();
if (key.isEmpty()) {
return Result.err(new Error.FormatError.UnknownPublicKey());
}
kind = DeserializationLimit.TrustedScopeKind.publicKey(key.get().getAlgorithm());
break;
default:
// a new kind of scope has to be mapped to a TrustedScopeKind here
throw new IllegalStateException("unhandled scope kind " + res.getOk().kind());
}
if (!allowedScopes.contains(kind)) {
return Result.err(
new Error.FormatError.DeserializationLimitError(
Error.FormatError.DeserializationLimitError.Reason.TRUSTED_SCOPE_NOT_ALLOWED));
}
}
return res;
}

public static Result<Scope, Error.FormatError> deserialize(Schema.Scope scope) {
if (scope.hasPublicKey()) {
long publicKey = scope.getPublicKey();
Expand Down
Loading