Skip to content

Commit 11dfd43

Browse files
Check the downloaded dashboard image's content type and test that errors keep the share key out (#78)
Review follow-ups on #75: a 200 that is not an image now fails instead of being saved as the JPEG; a test covers download failures (connection refused, HTML page, 403) and asserts the share key never reaches the error; CallTool now wraps CallToolWithImages. Amp-Thread-ID: https://ampcode.com/threads/T-01a2147b-3c2e-76b3-aeaf-0aafbe53d879 Co-authored-by: Amp <amp@ampcode.com>
1 parent fc338ae commit 11dfd43

3 files changed

Lines changed: 37 additions & 10 deletions

File tree

‎cmd/dashboard/image.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,9 @@ func download(cmd *cobra.Command, imageURL string) ([]byte, error) {
129129
if resp.StatusCode != http.StatusOK {
130130
return nil, fmt.Errorf("downloading the dashboard image failed: %s", resp.Status)
131131
}
132+
// A 200 with an HTML error or login page must not be saved as the image.
133+
if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "image/") {
134+
return nil, fmt.Errorf("downloading the dashboard image failed: got %q instead of an image", ct)
135+
}
132136
return io.ReadAll(resp.Body)
133137
}

‎cmd/dashboard/widget_image_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func TestImageSavesInlineJPEGAndHidesShareURL(t *testing.T) {
9090

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

108+
func imageFake(url string) *dunemcptest.Fake {
109+
return &dunemcptest.Fake{Results: map[string]string{"getDashboardImage": `{"dashboardId":5,
110+
"dashboardUrl":"https://dune.com/alice/d","dashboardImageUrl":"` + url + `",
111+
"imageIncluded":false,"withheldVisualizationIds":[]}`}}
112+
}
113+
114+
func TestImageDownloadFailuresKeepTheShareKeyOut(t *testing.T) {
115+
closed := httptest.NewServer(http.NotFoundHandler())
116+
closed.Close()
117+
html := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
118+
w.Header().Set("Content-Type", "text/html")
119+
_, _ = w.Write([]byte("<html>login</html>"))
120+
}))
121+
t.Cleanup(html.Close)
122+
forbidden := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
123+
w.WriteHeader(http.StatusForbidden)
124+
}))
125+
t.Cleanup(forbidden.Close)
126+
127+
for name, base := range map[string]string{"connection refused": closed.URL, "html page": html.URL, "403": forbidden.URL} {
128+
t.Run(name, func(t *testing.T) {
129+
path := filepath.Join(t.TempDir(), "d.jpg")
130+
_, err := runDash(imageFake(base+"/SHAREKEY"), "image", "5", "--file", path)
131+
require.Error(t, err)
132+
assert.NotContains(t, err.Error(), "SHAREKEY")
133+
assert.NoFileExists(t, path)
134+
})
135+
}
136+
}
137+
107138
func TestImageWithheldForLicensedData(t *testing.T) {
108139
path := filepath.Join(t.TempDir(), "d.jpg")
109140
fake := &dunemcptest.Fake{Results: map[string]string{"getDashboardImage": `{"dashboardId":5,

‎cmdutil/mcp.go‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,8 @@ func JSONFlag(cmd *cobra.Command, name string, v any) error {
9494
// CallTool calls a Dune MCP tool and, when view is not nil, decodes the result
9595
// into it. It returns the result as the tool sent it, for JSON output.
9696
func CallTool(cmd *cobra.Command, tool string, args map[string]any, view any) (json.RawMessage, error) {
97-
raw, err := MCPFromCmd(cmd).Call(cmd.Context(), tool, args)
98-
if err != nil {
99-
return nil, err
100-
}
101-
if view != nil {
102-
if err := json.Unmarshal(raw, view); err != nil {
103-
return nil, fmt.Errorf("%s: unexpected result: %w", tool, err)
104-
}
105-
}
106-
return raw, nil
97+
raw, _, err := CallToolWithImages(cmd, tool, args, view)
98+
return raw, err
10799
}
108100

109101
// CallToolWithImages calls a Dune MCP tool like CallTool and also returns the

0 commit comments

Comments
 (0)