Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/dashboard/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,9 @@ func download(cmd *cobra.Command, imageURL string) ([]byte, error) {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("downloading the dashboard image failed: %s", resp.Status)
}
// A 200 with an HTML error or login page must not be saved as the image.
if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "image/") {
return nil, fmt.Errorf("downloading the dashboard image failed: got %q instead of an image", ct)
}
return io.ReadAll(resp.Body)
}
31 changes: 31 additions & 0 deletions cmd/dashboard/widget_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestImageSavesInlineJPEGAndHidesShareURL(t *testing.T) {

func TestImageDownloadsWhenTooLargeToInline(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "image/jpeg")
_, _ = w.Write([]byte("big-jpeg"))
}))
t.Cleanup(srv.Close)
Expand All @@ -104,6 +105,36 @@ func TestImageDownloadsWhenTooLargeToInline(t *testing.T) {
assert.NotContains(t, out, "KEY")
}

func imageFake(url string) *dunemcptest.Fake {
return &dunemcptest.Fake{Results: map[string]string{"getDashboardImage": `{"dashboardId":5,
"dashboardUrl":"https://dune.com/alice/d","dashboardImageUrl":"` + url + `",
"imageIncluded":false,"withheldVisualizationIds":[]}`}}
}

func TestImageDownloadFailuresKeepTheShareKeyOut(t *testing.T) {
closed := httptest.NewServer(http.NotFoundHandler())
closed.Close()
html := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/html")
_, _ = w.Write([]byte("<html>login</html>"))
}))
t.Cleanup(html.Close)
forbidden := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusForbidden)
}))
t.Cleanup(forbidden.Close)

for name, base := range map[string]string{"connection refused": closed.URL, "html page": html.URL, "403": forbidden.URL} {
t.Run(name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "d.jpg")
_, err := runDash(imageFake(base+"/SHAREKEY"), "image", "5", "--file", path)
require.Error(t, err)
assert.NotContains(t, err.Error(), "SHAREKEY")
assert.NoFileExists(t, path)
})
}
}

func TestImageWithheldForLicensedData(t *testing.T) {
path := filepath.Join(t.TempDir(), "d.jpg")
fake := &dunemcptest.Fake{Results: map[string]string{"getDashboardImage": `{"dashboardId":5,
Expand Down
12 changes: 2 additions & 10 deletions cmdutil/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,8 @@ func JSONFlag(cmd *cobra.Command, name string, v any) error {
// CallTool calls a Dune MCP tool and, when view is not nil, decodes the result
// into it. It returns the result as the tool sent it, for JSON output.
func CallTool(cmd *cobra.Command, tool string, args map[string]any, view any) (json.RawMessage, error) {
raw, err := MCPFromCmd(cmd).Call(cmd.Context(), tool, args)
if err != nil {
return nil, err
}
if view != nil {
if err := json.Unmarshal(raw, view); err != nil {
return nil, fmt.Errorf("%s: unexpected result: %w", tool, err)
}
}
return raw, nil
raw, _, err := CallToolWithImages(cmd, tool, args, view)
return raw, err
}

// CallToolWithImages calls a Dune MCP tool like CallTool and also returns the
Expand Down
Loading