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

Go does not validate metadata keys and values #468

Closed
ejona86 opened this issue Dec 15, 2015 · 6 comments · Fixed by #4886
Closed

Go does not validate metadata keys and values #468

ejona86 opened this issue Dec 15, 2015 · 6 comments · Fixed by #4886

Comments

@ejona86
Copy link
Member

ejona86 commented Dec 15, 2015

HTTP has restrictions on what characters may be used in headers. For example, newline and control characters are strictly prohibited. In addition, gRPC has additional restrictions and by not enforcing them grpc-go makes it much more likely users will create grpc services that can't be used with the other implementations, because they rely on invalid keys or values.

Enforcing the HTTP restrictions must be done, but it would also make sense to enforce gRPC's metadata restrictions.

https://github.com/grpc/grpc-go/blob/master/metadata/metadata.go#L63
https://github.com/grpc/grpc-go/blob/master/transport/http2_client.go#L334

@iamqizhao iamqizhao added this to the 2016/02/15 Milestone milestone Jan 12, 2016
@kelseyhightower
Copy link

@iamqizhao Can I take a shot at this one?

Seems like one or more backwards incompatible changes will be required to the metadata package to enforce gRPC metadata requirements. For example should the metadata.encodeKeyValue function return an error which would then need to propagate through the metadata.New function?

Ideally we could add a new function to validate function to the metadata package:

func Validate(md MD) error

bradfitz added a commit to bradfitz/grpc-go that referenced this issue Jan 31, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 1, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
@iamqizhao
Copy link
Contributor

Kelsey,

Sure. Thanks a lot for the help!. I will schedule a meeting with you and let's talk.

bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 1, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 1, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 4, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 4, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 4, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 5, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 8, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 9, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 9, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 9, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 9, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 10, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 11, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 11, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 11, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
bradfitz added a commit to bradfitz/grpc-go that referenced this issue Feb 12, 2016
This adds new http.Handler-based ServerTransport in the process,
reusing the HTTP/2 server code in x/net/http2 or Go 1.6+.

All end2end tests pass with this new ServerTransport.

Fixes grpc#75

Also:
Updates grpc#495 (lets user fix it with middleware in front)
Updates grpc#468 (x/net/http2 validates)
Updates grpc#147 (possible with x/net/http2)
Updates grpc#104 (x/net/http2 does this)
@menghanl menghanl added the TODO label Jun 16, 2017
@dfawley
Copy link
Member

dfawley commented Jun 19, 2017

Proposal:

Return a gRPC status error (code: Internal) from grpc.Invoke or ServerStream's SetHeader, SendHeader, and SetTrailer methods before transmitting any additional data if the metadata in the request is malformed.

This avoids changing the API and doesn't require a new validation function that has to be called manually. Note that anyone relying upon sending metadata forbidden by the spec but accepted by the current version of grpc-go would be broken by this change.

@easwars
Copy link
Contributor

easwars commented May 31, 2019

@dfawley

Few clarifying questions before I get started on this:

  1. On the client side, I need to make sure that we validate the metadata from grpc.Invoke() and ClientConn.NewStream(). Both of these call newClientStream(). Are there any other code paths I need to be aware of?

  2. Once newClientStream() creates a csAttempt and calls newStream() on it, the underlying transport's method to create a stream is invoked, http2Client.NewStream(). This calls http2Client.createHeaderFields(), and here I see that some header fields added a metadata directly to the transport are also sent in the HEADERS frame. Do we need to validate these?

  3. On the server side, ServerStream.SetHeader ServerStream.SendHeader and ServerStream.SetTrailer are the methods of concern. Are there more?

  4. The new method for validating the headers might look like this:

package metadata

func (md MD) Validate() error { ...}

Is this the complete set of checks to be done, or are there more?

  • Header names contain one or more characters from this set [0-9 a-z _ - .]
  • If the header-name ends with a "-bin" suffix, the header-value could contain an arbitrary octet sequence. So no real validation required here.
  • If header-name does not end with a "-bin" suffix, header-value should only contain one or more characters from the set ( %x20-%x7E ) which includes space and printable ASCII.

Thanks.

@gyuho
Copy link
Contributor

gyuho commented Jan 20, 2020

@dfawley @menghanl @easwars Any updates? Even adding validate method would be great.

@grpc grpc deleted a comment from stale bot Jan 21, 2020
@dfawley
Copy link
Member

dfawley commented Jan 21, 2020

@easwars do you have any plans to work on this soon? If not, please unassign and add a "help wanted" label.

@gyuho, this is semi-low priority for us right now, since it is only intended to protect users from their own mistakes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants