Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cel: fix validation of expression result type #3526

Merged
merged 3 commits into from
Jun 30, 2020
Merged

Conversation

mohammed90
Copy link
Member

This Caddy JSON config is rejected with the error loading http app module: provision http: server srv0: setting up route handlers: route 0: loading handler modules: position 0: loading module 'subroute': provision http.handlers.subroute: setting up subroutes: route 1: loading matcher modules: module name 'not': provision http.matchers.not: loading matcher sets: module name 'expression': provision http.matchers.expression: CEL request matcher expects return type of bool, not primitive:BOOL:

{
	"apps": {
		"http": {
			"servers": {
				"srv0": {
					"listen": [
						":443"
					],
					"routes": [
						{
							"match": [
								{
									"expression": "{http.request.uri.query} != ''"
								}
							],
							"handle": [
								{
									"handler": "static_response",
									"body": "Hello"
								}
							]
						}
					]
				}
			}
		}
	}
}

Checking the types with a few log lines prints (manually prettified):

&expr.Type{
	state:impl.MessageState{
		NoUnkeyedLiterals:pragma.NoUnkeyedLiterals{}, 
		DoNotCompare:pragma.DoNotCompare{}, 
		DoNotCopy:pragma.DoNotCopy{}, 
		atomicMessageInfo:(*impl.MessageInfo)(0xc000114138)
	}, 
	sizeCache:0, 
	unknownFields:[]uint8(nil), 
	TypeKind:(*expr.Type_Primitive)(0xc00036f400)
}

&expr.Type{
	state:impl.MessageState{
		NoUnkeyedLiterals:pragma.NoUnkeyedLiterals{}, 
		DoNotCompare:pragma.DoNotCompare{}, 
		DoNotCopy:pragma.DoNotCopy{}, 
		atomicMessageInfo:(*impl.MessageInfo)(0xc000114138)
	},
	sizeCache:0, 
	unknownFields:[]uint8(nil), 
	TypeKind:(*expr.Type_Primitive)(0xc00036f400)
}

The types appear to be exactly the same. However, the proto.Equal() method prints:
proto: don't know how to compare []

The earlier code used the proto.Equals from github.com/gogo/protobuf, which failed to compare two messages of the same type for some reason. Switching to proto.Equal from the canonical github.com/golang/protobuf fixes the issue.

Reported in the forum: https://caddy.community/t/example-configure-wordpress-with-a-static-cache/8215/7

The earlier code used the proto.Equals from github.com/gogo/protobuf, which failed to compare two messages of the same type for some reason. Switching to proto.Equal from the canonical github.com/golang/protobuf fixes the issue.
Comment on lines 28 to 29
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the package github.com/golang/protobuf/proto is deprecated. We're instructed to use the "google.golang.org/protobuf/proto" package instead. I didn't want to change it now to avoid intrusive changes whose consequences are, currently, unknown.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should, though, since deprecated packages spells lack of updates and fixes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. For a second I was worried this will break the code because cel-go uses the deprecated package github.com/golang/protobuf/ptypes/timestamp. I checked the code in github.com/golang/protobuf/ptypes/timestamp and found it uses type aliases, so it will all work fine. Kinda cool the type alias works perfectly as intended and argued for.

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this. Pretty disappointed in that dependency, but what can we do...

Let me know what you think about changing to the non-deprecated package path. That seems like the right thing to do, since we already have something that's broken, maybe we should be proactive in avoiding future breakages.

Also if you haven't yet, let's run go mod tidy.

Comment on lines 28 to 29
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should, though, since deprecated packages spells lack of updates and fixes?

@mholt mholt added the under review 🧐 Review is pending before merging label Jun 29, 2020
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thank you @mohammed90 !

@mholt mholt removed the under review 🧐 Review is pending before merging label Jun 30, 2020
@mholt mholt merged commit d7dbf85 into master Jun 30, 2020
@mholt mholt deleted the fix-expr-matcher branch June 30, 2020 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error: proto: don't know how to compare [], CEL request matcher expects return type of bool pb.proto warning
3 participants