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

fix: validate proto messages before converting them to anypb.Any #4499

Merged
merged 15 commits into from
Oct 29, 2024

Conversation

zhaohuabing
Copy link
Member

@zhaohuabing zhaohuabing commented Oct 23, 2024

The validate and validateAll method of a proto message don't validate a member field of anypb.Any type as it doesn't have a validate method itself. Therefore, all proto messages must be validated before been converting to anypb.Any.

Fix #4504

Release note: No

@zhaohuabing zhaohuabing requested a review from a team as a code owner October 23, 2024 03:33
@zhaohuabing zhaohuabing marked this pull request as draft October 23, 2024 03:33
@zhaohuabing zhaohuabing changed the title chore: validate proto message before converting to any chore: validate proto messages before converting them to any Oct 23, 2024
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
@zhaohuabing zhaohuabing changed the title chore: validate proto messages before converting them to any fix: validate proto messages before converting them to any Oct 23, 2024
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
@@ -13,7 +13,8 @@ accesslog:
protocol: "%PROTOCOL%"
response_code: "%RESPONSE_CODE%"
als:
- destination:
- name: als
Copy link
Member Author

Choose a reason for hiding this comment

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

--- FAIL: TestTranslateXds (0.00s)
    --- FAIL: TestTranslateXds/accesslog (0.00s)
        translator_test.go:142: accesslog
        translator_test.go:143: 
                Error Trace:    /home/ubuntu/gateway/internal/xds/translator/translator_test.go:143
                Error:          Received unexpected error:
                                invalid HttpGrpcAccessLogConfig.CommonConfig: embedded message failed validation | caused by: invalid CommonGrpcAccessLogConfig.LogName: value length must be at least 1 runes
                Test:           TestTranslateXds/accesslog

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
@@ -11,7 +11,8 @@ accesslog:
protocol: "%PROTOCOL%"
response_code: "%RESPONSE_CODE%"
als:
- destination:
- name: als
Copy link
Member Author

Choose a reason for hiding this comment

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

go test -timeout 30s github.com/envoyproxy/gateway/internal/xds/translator --override-testdata=true
--- FAIL: TestTranslateXds (0.42s)
    --- FAIL: TestTranslateXds/accesslog-without-format (0.00s)
        translator_test.go:142: accesslog-without-format
        translator_test.go:143: 
                Error Trace:    /home/ubuntu/gateway/internal/xds/translator/translator_test.go:143
                Error:          Received unexpected error:
                                invalid HttpGrpcAccessLogConfig.CommonConfig: embedded message failed validation | caused by: invalid CommonGrpcAccessLogConfig.LogName: value length must be at least 1 runes

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
@@ -44,7 +44,7 @@ http:
isIPv6: false
maskLen: 24
jwt:
issuer: https://one.example.com
provider: https://one.example.com
Copy link
Member Author

Choose a reason for hiding this comment

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

--- FAIL: TestTranslateXds (0.00s)
    --- FAIL: TestTranslateXds/authorization-multiple-principals (0.00s)
        translator_test.go:142: authorization-multiple-principals
        translator_test.go:143: 
                Error Trace:    /home/ubuntu/gateway/internal/xds/translator/translator_test.go:143
                Error:          Received unexpected error:
                                invalid DynamicMetadataInput.Path[0]: embedded message failed validation | caused by: invalid DynamicMetadataInput_PathSegment.Key: value length must be at least 1 runes
                Test:           TestTranslateXds/authorization-multiple-principals

Copy link

codecov bot commented Oct 24, 2024

Codecov Report

Attention: Patch coverage is 49.59350% with 62 lines in your changes missing coverage. Please review.

Project coverage is 65.54%. Comparing base (9353be2) to head (0e272b1).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
internal/xds/translator/listener.go 58.13% 12 Missing and 6 partials ⚠️
internal/xds/translator/accesslog.go 34.78% 10 Missing and 5 partials ⚠️
internal/xds/translator/translator.go 15.38% 7 Missing and 4 partials ⚠️
internal/xds/translator/authorization.go 20.00% 0 Missing and 8 partials ⚠️
internal/utils/protocov/protocov.go 71.42% 3 Missing and 1 partial ⚠️
internal/xds/translator/custom_response.go 20.00% 0 Missing and 4 partials ⚠️
internal/xds/translator/basicauth.go 0.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4499      +/-   ##
==========================================
- Coverage   65.60%   65.54%   -0.07%     
==========================================
  Files         210      210              
  Lines       31612    31649      +37     
==========================================
+ Hits        20739    20744       +5     
- Misses       9670     9690      +20     
- Partials     1203     1215      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

func ToAny(msg proto.Message) *anypb.Any {
res, err := ToAnyWithValidation(msg)
if err != nil {
return nil
Copy link
Member Author

@zhaohuabing zhaohuabing Oct 24, 2024

Choose a reason for hiding this comment

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

The validation error should not be ignored.

Leave the ToAny function for now as the impact are a bit large and we're approaching the rc1. I will raise a follow-up PR to clean up all the refrences to it after v1.2.0 release.

Copy link
Contributor

@zirain zirain Oct 26, 2024

Choose a reason for hiding this comment

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

For cases like the following, 'ToAny' still makes sense, but you really should use ToAnyWithValidation in most cases.

https://github.com/envoyproxy/gateway/blob/988d4ed0be5a5f9dd39a784d52c3bb5c51ecc2aa/internal/xds/filters/wellknown.go

Copy link
Contributor

Choose a reason for hiding this comment

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

should we change the comment?

Copy link
Member Author

@zhaohuabing zhaohuabing Oct 29, 2024

Choose a reason for hiding this comment

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

I prefer removing this function at all, but let's discuss and decide in a follow-up PR. Tracked in #4554

@zhaohuabing zhaohuabing marked this pull request as ready for review October 24, 2024 03:08
@zhaohuabing zhaohuabing requested review from guydc and a team October 24, 2024 03:11
@arkodg arkodg added this to the v1.2.0 milestone Oct 24, 2024
@zhaohuabing zhaohuabing changed the title fix: validate proto messages before converting them to any fix: validate proto messages before converting them to anypb.Any Oct 24, 2024
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
zirain
zirain previously approved these changes Oct 28, 2024
@arkodg
Copy link
Contributor

arkodg commented Oct 28, 2024

hey @zhaohuabing can you rebase ?

@zirain
Copy link
Contributor

zirain commented Oct 29, 2024

this's worth to backport if possible.

@zhaohuabing zhaohuabing added the cherrypick/release-v1.1 cherrypick to release/v1.1 label Oct 29, 2024
Copy link
Contributor

@arkodg arkodg left a comment

Choose a reason for hiding this comment

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

LGTM thanks for fixing it across the board

@arkodg arkodg merged commit 05817fc into envoyproxy:main Oct 29, 2024
20 of 21 checks passed
@zhaohuabing zhaohuabing deleted the proto-validation branch October 29, 2024 01:26
zhaohuabing added a commit to zhaohuabing/gateway that referenced this pull request Oct 29, 2024
…oyproxy#4499)

* validate proto message before converting to any

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
(cherry picked from commit 05817fc)
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
zhaohuabing added a commit to zhaohuabing/gateway that referenced this pull request Oct 29, 2024
…oyproxy#4499)

* validate proto message before converting to any

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
(cherry picked from commit 05817fc)
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
zhaohuabing added a commit to zhaohuabing/gateway that referenced this pull request Oct 29, 2024
…oyproxy#4499)

* validate proto message before converting to any

Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
(cherry picked from commit 05817fc)
Signed-off-by: Huabing Zhao <zhaohuabing@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some XDS resources are not validated
3 participants