Skip to content

Commit

Permalink
topdown: Add base64.is_valid builtin
Browse files Browse the repository at this point in the history
Adds a builtin to check if a string is valid base64

Fixes: #2690

Signed-off-by: Calle Pettersson <calle@cape.nu>
  • Loading branch information
carlpett authored and tsandall committed Sep 22, 2020
1 parent 7f0399b commit 36781fb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ast/builtins.go
Expand Up @@ -130,6 +130,7 @@ var DefaultBuiltins = [...]*Builtin{
JSONUnmarshal,
Base64Encode,
Base64Decode,
Base64IsValid,
Base64UrlEncode,
Base64UrlDecode,
URLQueryDecode,
Expand Down Expand Up @@ -1231,6 +1232,15 @@ var Base64Decode = &Builtin{
),
}

// Base64IsValid verifies the input string is base64 encoded.
var Base64IsValid = &Builtin{
Name: "base64.is_valid",
Decl: types.NewFunction(
types.Args(types.S),
types.B,
),
}

// Base64UrlEncode serializes the input string into base64url encoding.
var Base64UrlEncode = &Builtin{
Name: "base64url.encode",
Expand Down
14 changes: 14 additions & 0 deletions capabilities.json
Expand Up @@ -195,6 +195,20 @@
"type": "function"
}
},
{
"name": "base64.is_valid",
"decl": {
"args": [
{
"type": "string"
}
],
"result": {
"type": "boolean"
},
"type": "function"
}
},
{
"name": "base64url.decode",
"decl": {
Expand Down
25 changes: 25 additions & 0 deletions test/cases/testdata/base64builtins/test-base64builtins-0935.yaml
@@ -0,0 +1,25 @@
cases:
- data: {}
modules:
- |
package generated
p = x {
base64.is_valid("aGVsbG8=", x)
}
note: base64builtins/is_valid-true
query: data.generated.p = x
want_result:
- x: true
- data: {}
modules:
- |
package generated
p = x {
base64.is_valid("{'not':'base64'}", x)
}
note: base64builtins/is_valid-false
query: data.generated.p = x
want_result:
- x: false
11 changes: 11 additions & 0 deletions topdown/encoding.go
Expand Up @@ -69,6 +69,16 @@ func builtinBase64Decode(a ast.Value) (ast.Value, error) {
return ast.String(result), err
}

func builtinBase64IsValid(a ast.Value) (ast.Value, error) {
str, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
}

_, err = base64.StdEncoding.DecodeString(string(str))
return ast.Boolean(err == nil), nil
}

func builtinBase64UrlEncode(a ast.Value) (ast.Value, error) {
str, err := builtins.StringOperand(a, 1)
if err != nil {
Expand Down Expand Up @@ -230,6 +240,7 @@ func init() {
RegisterFunctionalBuiltin1(ast.JSONUnmarshal.Name, builtinJSONUnmarshal)
RegisterFunctionalBuiltin1(ast.Base64Encode.Name, builtinBase64Encode)
RegisterFunctionalBuiltin1(ast.Base64Decode.Name, builtinBase64Decode)
RegisterFunctionalBuiltin1(ast.Base64IsValid.Name, builtinBase64IsValid)
RegisterFunctionalBuiltin1(ast.Base64UrlEncode.Name, builtinBase64UrlEncode)
RegisterFunctionalBuiltin1(ast.Base64UrlDecode.Name, builtinBase64UrlDecode)
RegisterFunctionalBuiltin1(ast.URLQueryDecode.Name, builtinURLQueryDecode)
Expand Down

0 comments on commit 36781fb

Please sign in to comment.