Skip to content

Commit

Permalink
String length counts the number of bytes in UTF-8 representation (#105)
Browse files Browse the repository at this point in the history
This aligns with the spec change from biscuit-auth/biscuit#167

Co-authored-by: Clément Delafargue <clement@delafargue.name>
  • Loading branch information
Geal and divarvel authored Jul 5, 2024
1 parent 942fbb9 commit 518d6ef
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ public Option<Map<Long, Term>> check_expressions(List<Expression> expressions, S
for(Expression e: expressions) {
Term term = e.evaluate(variables, new TemporarySymbolTable(symbols));

if(!term.equals(new Term.Bool(true))) {
if(! (term instanceof Term.Bool)) {
throw new Error.InvalidType();
}
if(!term.equals(new Term.Bool(true))) {
return Option.none();
}
}

return Option.some(variables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.vavr.control.Either;
import io.vavr.control.Option;

import java.io.UnsupportedEncodingException;
import java.util.*;

import static io.vavr.API.Left;
Expand Down Expand Up @@ -136,7 +137,11 @@ public void evaluate(Deque<Term> stack, Map<Long, Term> variables, TemporarySymb
if(s.isEmpty()) {
throw new Error.Execution("string not found in symbols for id"+value);
} else {
stack.push(new Term.Integer(s.get().length()));
try {
stack.push(new Term.Integer(s.get().getBytes("UTF-8").length));
} catch (UnsupportedEncodingException e) {
throw new Error.Execution("cannot calculate string length: "+e.toString());
}
}
} else if (value instanceof Term.Bytes) {
stack.push(new Term.Integer(((Term.Bytes) value).value().length));
Expand Down
8 changes: 6 additions & 2 deletions src/test/resources/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ result: `Err(FailedLogic(Unauthorized { policy: Allow(0), checks: [Block(FailedB
### token

authority:
symbols: ["hello world", "hello", "world", "aaabde", "a*c?.e", "abd", "aaa", "b", "de", "abcD12", "abc", "def"]
symbols: ["hello world", "hello", "world", "aaabde", "a*c?.e", "abd", "aaa", "b", "de", "abcD12", "é", "abc", "def"]

public keys: []

Expand All @@ -1239,6 +1239,8 @@ check if "aaabde".matches("a*c?.e");
check if "aaabde".contains("abd");
check if "aaabde" == "aaa" + "b" + "de";
check if "abcD12" == "abcD12";
check if "abcD12".length() == 6;
check if "é".length() == 2;
check if 2019-12-04T09:46:41Z < 2020-12-04T09:46:41Z;
check if 2020-12-04T09:46:41Z > 2019-12-04T09:46:41Z;
check if 2019-12-04T09:46:41Z <= 2020-12-04T09:46:41Z;
Expand Down Expand Up @@ -1268,7 +1270,7 @@ allow if true;
```

revocation ids:
- `f61b4cb4fc58777fec6c8d39fe62259dc3c78511868236c391e9f67ffd03a3a8b8e3042d4bacce0d5756d053f5afccd4c5e4df0597af44b36bdfab492e5fe50e`
- `3d5b23b502b3dd920bfb68b9039164d1563bb8927210166fa5c17f41b76b31bb957bc2ed3318452958f658baa2d398fe4cf25c58a27e6c8bc42c9702c8aa1b0c`

authorizer world:
```
Expand All @@ -1287,7 +1289,9 @@ World {
"check if \"aaabde\".contains(\"abd\")",
"check if \"aaabde\".matches(\"a*c?.e\")",
"check if \"abcD12\" == \"abcD12\"",
"check if \"abcD12\".length() == 6",
"check if \"hello world\".starts_with(\"hello\") && \"hello world\".ends_with(\"world\")",
"check if \"é\".length() == 2",
"check if (true || false) && true",
"check if 1 + 2 * 3 - 4 / 2 == 5",
"check if 1 < 2",
Expand Down
7 changes: 5 additions & 2 deletions src/test/resources/samples/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -1239,12 +1239,13 @@
"b",
"de",
"abcD12",
"é",
"abc",
"def"
],
"public_keys": [],
"external_key": null,
"code": "check if true;\ncheck if !false;\ncheck if !false && true;\ncheck if false || true;\ncheck if (true || false) && true;\ncheck if true == true;\ncheck if false == false;\ncheck if 1 < 2;\ncheck if 2 > 1;\ncheck if 1 <= 2;\ncheck if 1 <= 1;\ncheck if 2 >= 1;\ncheck if 2 >= 2;\ncheck if 3 == 3;\ncheck if 1 + 2 * 3 - 4 / 2 == 5;\ncheck if \"hello world\".starts_with(\"hello\") && \"hello world\".ends_with(\"world\");\ncheck if \"aaabde\".matches(\"a*c?.e\");\ncheck if \"aaabde\".contains(\"abd\");\ncheck if \"aaabde\" == \"aaa\" + \"b\" + \"de\";\ncheck if \"abcD12\" == \"abcD12\";\ncheck if 2019-12-04T09:46:41Z < 2020-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z > 2019-12-04T09:46:41Z;\ncheck if 2019-12-04T09:46:41Z <= 2020-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z >= 2020-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z >= 2019-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z >= 2020-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z == 2020-12-04T09:46:41Z;\ncheck if hex:12ab == hex:12ab;\ncheck if [1, 2].contains(2);\ncheck if [2019-12-04T09:46:41Z, 2020-12-04T09:46:41Z].contains(2020-12-04T09:46:41Z);\ncheck if [false, true].contains(true);\ncheck if [\"abc\", \"def\"].contains(\"abc\");\ncheck if [hex:12ab, hex:34de].contains(hex:34de);\ncheck if [1, 2].contains([2]);\ncheck if [1, 2] == [1, 2];\ncheck if [1, 2].intersection([2, 3]) == [2];\ncheck if [1, 2].union([2, 3]) == [1, 2, 3];\ncheck if [1, 2, 3].intersection([1, 2]).contains(1);\ncheck if [1, 2, 3].intersection([1, 2]).length() == 2;\n"
"code": "check if true;\ncheck if !false;\ncheck if !false && true;\ncheck if false || true;\ncheck if (true || false) && true;\ncheck if true == true;\ncheck if false == false;\ncheck if 1 < 2;\ncheck if 2 > 1;\ncheck if 1 <= 2;\ncheck if 1 <= 1;\ncheck if 2 >= 1;\ncheck if 2 >= 2;\ncheck if 3 == 3;\ncheck if 1 + 2 * 3 - 4 / 2 == 5;\ncheck if \"hello world\".starts_with(\"hello\") && \"hello world\".ends_with(\"world\");\ncheck if \"aaabde\".matches(\"a*c?.e\");\ncheck if \"aaabde\".contains(\"abd\");\ncheck if \"aaabde\" == \"aaa\" + \"b\" + \"de\";\ncheck if \"abcD12\" == \"abcD12\";\ncheck if \"abcD12\".length() == 6;\ncheck if \"é\".length() == 2;\ncheck if 2019-12-04T09:46:41Z < 2020-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z > 2019-12-04T09:46:41Z;\ncheck if 2019-12-04T09:46:41Z <= 2020-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z >= 2020-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z >= 2019-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z >= 2020-12-04T09:46:41Z;\ncheck if 2020-12-04T09:46:41Z == 2020-12-04T09:46:41Z;\ncheck if hex:12ab == hex:12ab;\ncheck if [1, 2].contains(2);\ncheck if [2019-12-04T09:46:41Z, 2020-12-04T09:46:41Z].contains(2020-12-04T09:46:41Z);\ncheck if [false, true].contains(true);\ncheck if [\"abc\", \"def\"].contains(\"abc\");\ncheck if [hex:12ab, hex:34de].contains(hex:34de);\ncheck if [1, 2].contains([2]);\ncheck if [1, 2] == [1, 2];\ncheck if [1, 2].intersection([2, 3]) == [2];\ncheck if [1, 2].union([2, 3]) == [1, 2, 3];\ncheck if [1, 2, 3].intersection([1, 2]).contains(1);\ncheck if [1, 2, 3].intersection([1, 2]).length() == 2;\n"
}
],
"validations": {
Expand All @@ -1262,7 +1263,9 @@
"check if \"aaabde\".contains(\"abd\")",
"check if \"aaabde\".matches(\"a*c?.e\")",
"check if \"abcD12\" == \"abcD12\"",
"check if \"abcD12\".length() == 6",
"check if \"hello world\".starts_with(\"hello\") && \"hello world\".ends_with(\"world\")",
"check if \"é\".length() == 2",
"check if (true || false) && true",
"check if 1 + 2 * 3 - 4 / 2 == 5",
"check if 1 < 2",
Expand Down Expand Up @@ -1307,7 +1310,7 @@
},
"authorizer_code": "allow if true;\n",
"revocation_ids": [
"f61b4cb4fc58777fec6c8d39fe62259dc3c78511868236c391e9f67ffd03a3a8b8e3042d4bacce0d5756d053f5afccd4c5e4df0597af44b36bdfab492e5fe50e"
"3d5b23b502b3dd920bfb68b9039164d1563bb8927210166fa5c17f41b76b31bb957bc2ed3318452958f658baa2d398fe4cf25c58a27e6c8bc42c9702c8aa1b0c"
]
}
}
Expand Down
Binary file modified src/test/resources/samples/test017_expressions.bc
Binary file not shown.

0 comments on commit 518d6ef

Please sign in to comment.