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

Add support for injecting a banner in HTML responses #35

Merged
merged 13 commits into from
Aug 21, 2019

Conversation

ojarjur
Copy link
Collaborator

@ojarjur ojarjur commented Aug 2, 2019

This can be useful for adding functionality to a web-app without having to change the backend server.

@@ -70,10 +72,18 @@ func hostProxy(ctx context.Context, host, shimPath string, injectShimCode bool)
Host: host,
Copy link
Contributor

Choose a reason for hiding this comment

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

I realize this wasn't changed here, but is the backend necessarily http?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good point.

Currently, every user of this I know of is just connecting to a server on localhost, so they are always http, but there is no reason to require that.

File a tracking issue for that here

return false
}
contentType := resp.Header.Get(contentTypeHeader)
return strings.Contains(contentType, "html")
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 be more strict in the content-type headers we accept?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

var frameWrapperTmpl = template.Must(template.New("frame-wrapper").Parse(frameWrapperTemplate))

func isHTMLRequest(r *http.Request) bool {
if r.Method != http.MethodGet {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we ever want to proxy and banner-ize other request types (e.g., POSTs)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought through this a bit and came to the conclusion that we should not inject the banner into responses for POSTS.

My concern is that an app might send a POST request and not be resilient to the response being modified.

As such, I think we should err on the side of not injecting the banner.

I've added comments to the code explaining this.

}
responseRecorder := httptest.NewRecorder()
wrapped.ServeHTTP(responseRecorder, r)
result := responseRecorder.Result()
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems a bit weird to use httptest for this purpose. Could we somehow inspect/transform the results in-line? How do the other proxy layers do this (or is the answer that they effectively return all responses unchanged)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fair enough. I've changed it to modify the response in-line.

Copy link
Contributor

@bsidhom bsidhom left a comment

Choose a reason for hiding this comment

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

LGTM

@ojarjur ojarjur merged commit 46dc98b into master Aug 21, 2019
@ojarjur ojarjur deleted the ojarjur/inject-banner branch November 18, 2019 18:20
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.

2 participants