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

http provider too strict with application/json content type #15164

Closed
sebastien-prudhomme opened this issue Jun 7, 2017 · 8 comments
Closed
Assignees

Comments

@sebastien-prudhomme
Copy link

Terraform Version

0.9.7

Affected Resource(s)

"http" datasource

Terraform Configuration Files

data "http" "openstack" {
  url = "http://169.254.169.254/openstack/latest/meta_data.json"
}

Actual Behavior

Not working:

Error refreshing state: 1 error(s) occurred:

* data.http.main: 1 error(s) occurred:

* data.http.main: data.http.main: Content-Type is not a text type. Got: application/json; charset=UTF-8

In source code builtin/providers/http/data_source.go:

func isContentTypeAllowed(contentType string) bool {
	allowedContentTypes := []*regexp.Regexp{
		regexp.MustCompile("^text/.+"),
		regexp.MustCompile("^application/json$"),
}

The regexp for "application/json" is too strict, as the charset can also be included by the web server

@apparentlymart
Copy link
Contributor

Thanks for reporting this, @sebastien-prudhomme!

The RFC for the application/json MIME type doesn't define any parameters for it, so technically your server here is misbehaving per the spec, but it seems reasonable for us to be pragmatic about it and trim off any erroneous parameters that are included.

Looking at this again here, I also see that we're not correctly handling the charset argument when it is correctly used on a text/* type, so hopefully we can address both of these things together:

  • If a text/* type is used, check for a charset= argument and attempt to convert the result to UTF-8 so we produce a valid Terraform string.
  • If application/json is used, ignore all arguments and instead obey the JSON spec rules on character encodings, which will likely be to fail if the input isn't UTF-8 since UTF-16 and UTF-32 JSON is not widely used enough to justify that complexity.

@apparentlymart
Copy link
Contributor

apparentlymart commented Jun 7, 2017

Although we're not necessarily reading HTML here, it seems like we could use golang.org/x/net/html/charset to get reasonable, pragmatic handling for the text/* types, using the charset= argument if it's present and content sniffing to distinguish between utf8 and windows-1252 if it isn't.

@apparentlymart apparentlymart self-assigned this Jun 7, 2017
@sebastien-prudhomme
Copy link
Author

Any reference for your RFC?

The RFC for HTTP1/1 "Content-Type" says you can have a charset after the mime type, so i think there is not a problem with the server (which is OpenStack metadata web service):

https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7

@apparentlymart
Copy link
Contributor

apparentlymart commented Jun 13, 2017

The application/json type is registered in RFC7159, section 11, where it says:

   Required parameters:  n/a
   Optional parameters:  n/a
   Encoding considerations:  binary

It also says:

    Note:  No "charset" parameter is defined for this registration.
    Adding one really has no effect on compliant recipients.

...which seems to excuse your server, I suppose!

But as I'd said above, we can be pragmatic about this and just ignore it, since Terraform doesn't employ a team of RFC lawyers. 😀

@nick4fake
Copy link

Is there a way to use http provider with S3? It gives binary/octet-stream for most files.

@atheiman
Copy link

Is there a reason i cannot use zip files with the http data source?

@mcdafydd
Copy link

Is there a way to use http provider with S3? It gives binary/octet-stream for most files.

I just ran into this issue trying to get the Azure JSON list of service tags and public IPs. The content-type is application/octet-stream.

@ghost
Copy link

ghost commented Jul 25, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants