Skip to content

internal/driver: reject cross-origin requests to saveconfig and deleteconfig - #1052

Open
ubeddulla wants to merge 1 commit into
google:mainfrom
ubeddulla:webui-config-csrf
Open

ubeddulla wants to merge 1 commit into
google:mainfrom
ubeddulla:webui-config-csrf

Conversation

@ubeddulla

Copy link
Copy Markdown

The /saveconfig and /deleteconfig handlers write to the settings file under os.UserConfigDir() but accept any method and never check the origin, so while pprof -http is running any page open in the same browser can add, overwrite or delete saved configs with <img src="http://localhost:PORT/ui/saveconfig?config=x&focus=..."> or a no-cors fetch. The request comes from the user's own browser with Host: localhost, so the local-client check in defaultWebServer does not help. The handlers now only accept the method the UI already sends (POST for save, DELETE for delete) and are wrapped in http.CrossOriginProtection, and TestConfigHandlersRejectCrossOrigin fails without the change.

…econfig

The handlers accepted any method and had no origin check, so any web
page open in the user's browser could add, overwrite or delete saved
configurations while the web UI was running. Only accept the method the
UI uses and apply http.CrossOriginProtection.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces cross-origin request protection and HTTP method validation for the /saveconfig and /deleteconfig endpoints using a new configHandler wrapper, along with corresponding unit tests. However, a compilation error was identified because http.NewCrossOriginProtection() is called on the standard library's net/http package instead of the internal custom http package.

Comment thread internal/driver/webui.go
// that other web sites open in the user's browser cannot change or delete
// saved configurations.
func configHandler(method string, h http.HandlerFunc) http.Handler {
return http.NewCrossOriginProtection().Handler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The code uses http.NewCrossOriginProtection(), but the http package imported in this file is "net/http" from the standard library, which does not contain this function. This will cause a compilation error.\n\nTo fix this, you need to import the custom http package from github.com/google/pprof/internal/http under a different name (e.g., pprofhttp) to avoid conflicting with "net/http", and then use pprofhttp.NewCrossOriginProtection() here.\n\nFor example, add this to your imports:\ngo\npprofhttp \"github.com/google/pprof/internal/http\"\n

\treturn pprofhttp.NewCrossOriginProtection().Handler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This one is a false positive. http.NewCrossOriginProtection is in the standard net/http since Go 1.25, and go.mod already requires 1.26. There is no internal/http package in this repo. The branch builds and TestConfigHandlersRejectCrossOrigin passes.

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.

1 participant