fix: harden chat workspace file upload input validation (#30184)
This commit is contained in:
1 parent
81c40579dc
commit
34ef30ee90
10 files changed
+199
-20
No files matched your search
@@ -89,7 +89,7 @@ func (api *API) registerChatAPIRoutes(r chi.Router, apiKeyMiddleware func(http.H
|
||||
r.Post("/title/propose", api.proposeChatTitle)
|
||||
r.Get("/diff", api.getChatDiffContents)
|
||||
r.With(
|
||||
httpmw.RateLimit(api.FilesRateLimit, time.Minute),
|
||||
httpmw.RateLimitPerCaller(api.FilesRateLimit, time.Minute),
|
||||
api.chatWorkspaceUploadMiddleware,
|
||||
).Post("/workspace-files", api.postChatWorkspaceFile)
|
||||
r.Put("/context", api.refreshChatContext)
|
||||
|
||||
+24
-9
@@ -7301,6 +7301,7 @@ const (
|
||||
chatWorkspaceUploadArchivedMessage = "Cannot upload files to an archived chat."
|
||||
chatWorkspaceUploadOwnerOnlyMessage = "Only the chat owner may upload files to a chat's workspace."
|
||||
chatWorkspaceUploadMissingFilenameMessage = "Filename is required."
|
||||
chatWorkspaceUploadUnusableFilenameMessage = "Filename is unusable."
|
||||
chatWorkspaceUploadNoChatAgentMessage = "No chat-compatible workspace agent found."
|
||||
chatWorkspaceUploadAgentDialTimeout = 30 * time.Second
|
||||
// Transport errors embed the agent's tailnet URL, so they are logged
|
||||
@@ -7426,16 +7427,25 @@ func (api *API) postChatWorkspaceFile(rw http.ResponseWriter, r *http.Request) {
|
||||
filename := chatFilenameFromContentDisposition(r.Header.Get("Content-Disposition"))
|
||||
sanitizedName, err := chatfiles.SanitizeWorkspaceUploadName(filename)
|
||||
if err != nil {
|
||||
if strings.TrimSpace(filename) == "" {
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
||||
Message: chatWorkspaceUploadMissingFilenameMessage,
|
||||
Detail: "Provide a filename via the Content-Disposition header.",
|
||||
})
|
||||
return
|
||||
}
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
||||
Message: chatWorkspaceUploadMissingFilenameMessage,
|
||||
Detail: "Provide a filename via the Content-Disposition header.",
|
||||
Message: chatWorkspaceUploadUnusableFilenameMessage,
|
||||
Detail: "The provided filename has no safe characters left after sanitization or is a reserved device name.",
|
||||
})
|
||||
return
|
||||
}
|
||||
name = sanitizedName
|
||||
|
||||
contentType := chatfiles.BaseMediaType(r.Header.Get("Content-Type"))
|
||||
if contentType == "" {
|
||||
// The client echoes media_type back as workspace_file_media_type, so
|
||||
// it must pass the same strict parse the message endpoint applies.
|
||||
contentType, err := chatfiles.ParseBaseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
|
||||
@@ -7865,11 +7875,16 @@ func createChatInputFromParts(
|
||||
Detail: fmt.Sprintf("%s[%d].workspace_file_path must reference a file uploaded to this chat.", fieldName, i),
|
||||
}
|
||||
}
|
||||
if strings.ContainsRune(part.WorkspaceFileMediaType, 0) {
|
||||
return nil, nil, &codersdk.Response{
|
||||
Message: "Invalid input part.",
|
||||
Detail: fmt.Sprintf("%s[%d].workspace_file_media_type must not contain NUL bytes.", fieldName, i),
|
||||
var workspaceFileMediaType string
|
||||
if part.WorkspaceFileMediaType != "" {
|
||||
parsed, err := chatfiles.ParseBaseMediaType(part.WorkspaceFileMediaType)
|
||||
if err != nil {
|
||||
return nil, nil, &codersdk.Response{
|
||||
Message: "Invalid input part.",
|
||||
Detail: fmt.Sprintf("%s[%d].workspace_file_media_type must be a valid media type of at most %d bytes without control characters.", fieldName, i, chatfiles.MaxMediaTypeBytes),
|
||||
}
|
||||
}
|
||||
workspaceFileMediaType = parsed
|
||||
}
|
||||
// The referenced bytes live in one specific workspace's
|
||||
// filesystem. Reject references from a workspace other
|
||||
@@ -7892,7 +7907,7 @@ func createChatInputFromParts(
|
||||
workspaceFilePath,
|
||||
part.WorkspaceFileName,
|
||||
part.WorkspaceFileSize,
|
||||
chatfiles.BaseMediaType(part.WorkspaceFileMediaType),
|
||||
workspaceFileMediaType,
|
||||
))
|
||||
// chatd loads the skill body as the chat owner when the message
|
||||
// is sent; only the shape is checked here.
|
||||
|
||||
@@ -378,7 +378,7 @@ func TestCreateChatMessageWorkspaceFileWorkspaceValidation(t *testing.T) {
|
||||
ctx := testutil.Context(t, testutil.WaitLong)
|
||||
client, chat := newBoundChat(ctx, t)
|
||||
|
||||
for _, name := range []string{" data.csv", "da\u202eta.csv", "da|ta.csv"} {
|
||||
for _, name := range []string{" data.csv", "da\u202eta.csv", "da|ta.csv", "a$(b);c'd.txt"} {
|
||||
part := validPart(chat.ID)
|
||||
part.WorkspaceFileWorkspaceID = *chat.WorkspaceID
|
||||
part.WorkspaceFileName = name
|
||||
@@ -391,6 +391,29 @@ func TestCreateChatMessageWorkspaceFileWorkspaceValidation(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidMediaType", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitLong)
|
||||
client, chat := newBoundChat(ctx, t)
|
||||
|
||||
for _, mediaType := range []string{
|
||||
"garbage",
|
||||
"text/plain\x00",
|
||||
"text/plain; charset=\"\x01\"",
|
||||
"text/plain; x=" + strings.Repeat("a", 255),
|
||||
} {
|
||||
part := validPart(chat.ID)
|
||||
part.WorkspaceFileWorkspaceID = *chat.WorkspaceID
|
||||
part.WorkspaceFileMediaType = mediaType
|
||||
_, err := client.CreateChatMessage(ctx, chat.ID, codersdk.CreateChatMessageRequest{
|
||||
Content: []codersdk.ChatInputPart{part},
|
||||
})
|
||||
sdkErr := requireSDKError(t, err, http.StatusBadRequest)
|
||||
require.Equal(t, "content[0].workspace_file_media_type must be a valid media type of at most 255 bytes without control characters.", sdkErr.Detail, "media type %q", mediaType)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("PersistsNormalizedReference", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -697,6 +720,28 @@ func TestPostChatWorkspaceFile(t *testing.T) {
|
||||
require.Equal(t, uploaded.Name, filepath.Base(uploaded.Path))
|
||||
})
|
||||
|
||||
t.Run("InvalidContentType", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitLong)
|
||||
client, db := newChatClientWithDatabase(t)
|
||||
firstUser := coderdtest.CreateFirstUser(t, client.Client)
|
||||
_ = createChatModel(t, client)
|
||||
|
||||
workspaceBuild := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
|
||||
OrganizationID: firstUser.OrganizationID,
|
||||
OwnerID: firstUser.UserID,
|
||||
}).WithAgent().Do()
|
||||
_ = startChatUploadAgent(t, client, workspaceBuild)
|
||||
chat := createBoundChat(ctx, t, client, firstUser.OrganizationID, workspaceBuild.Workspace.ID)
|
||||
|
||||
// The client sends the returned media type back as
|
||||
// workspace_file_media_type, so it must pass message validation.
|
||||
resp, err := client.UploadChatWorkspaceFile(ctx, chat.ID, "text/plain; bogus", "notes.txt", bytes.NewReader([]byte("hi")))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "application/octet-stream", resp.MediaType)
|
||||
})
|
||||
|
||||
t.Run("MissingFilename", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -711,10 +756,19 @@ func TestPostChatWorkspaceFile(t *testing.T) {
|
||||
}).WithAgent().Do()
|
||||
chat := createBoundChat(ctx, t, client, firstUser.OrganizationID, workspaceBuild.Workspace.ID)
|
||||
|
||||
res, err := uploadChatWorkspaceFile(ctx, t, client, chat.ID.String(), "", "application/zip", []byte("PK"))
|
||||
require.NoError(t, err)
|
||||
defer res.Body.Close()
|
||||
require.Equal(t, http.StatusBadRequest, res.StatusCode)
|
||||
for _, tc := range []struct {
|
||||
contentDisposition string
|
||||
detail string
|
||||
}{
|
||||
{"", "Provide a filename via the Content-Disposition header."},
|
||||
{`attachment; filename="$()"`, "The provided filename has no safe characters left after sanitization or is a reserved device name."},
|
||||
} {
|
||||
res, err := uploadChatWorkspaceFile(ctx, t, client, chat.ID.String(), tc.contentDisposition, "application/zip", []byte("PK"))
|
||||
require.NoError(t, err)
|
||||
sdkErr := requireSDKError(t, codersdk.ReadBodyAsError(res), http.StatusBadRequest)
|
||||
_ = res.Body.Close()
|
||||
require.Equal(t, tc.detail, sdkErr.Detail, tc.contentDisposition)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NoWorkspace", func(t *testing.T) {
|
||||
@@ -756,9 +810,19 @@ func TestPostChatWorkspaceFile(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
otherChat, err := client.CreateChat(ctx, codersdk.CreateChatRequest{
|
||||
OrganizationID: firstUser.OrganizationID,
|
||||
Content: []codersdk.ChatInputPart{
|
||||
{Type: codersdk.ChatInputPartTypeText, Text: "rate limited too"},
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// The budget is per user, so a different chat ID does not get a
|
||||
// fresh bucket.
|
||||
_, err = client.UploadChatWorkspaceFile(ctx, chat.ID, "application/zip", "data.zip", bytes.NewReader([]byte("PK")))
|
||||
requireSDKError(t, err, http.StatusConflict)
|
||||
_, err = client.UploadChatWorkspaceFile(ctx, chat.ID, "application/zip", "data.zip", bytes.NewReader([]byte("PK")))
|
||||
_, err = client.UploadChatWorkspaceFile(ctx, otherChat.ID, "application/zip", "data.zip", bytes.NewReader([]byte("PK")))
|
||||
requireSDKError(t, err, http.StatusTooManyRequests)
|
||||
})
|
||||
|
||||
|
||||
@@ -25,6 +25,19 @@ func RateLimit(count int, window time.Duration) func(http.Handler) http.Handler
|
||||
return rateLimitWithEndpointKey(count, window, keyByNormalizedEndpoint, nil)
|
||||
}
|
||||
|
||||
// RateLimitPerCaller returns a handler that limits requests per-minute
|
||||
// based on IP and user ID (if available), ignoring the request path. Every
|
||||
// route mounted behind one instance shares one budget per caller, so path
|
||||
// parameters such as a chat ID cannot be varied to get a fresh bucket.
|
||||
func RateLimitPerCaller(count int, window time.Duration) func(http.Handler) http.Handler {
|
||||
return rateLimitWithEndpointKey(count, window,
|
||||
func(*http.Request) (string, error) {
|
||||
return "", nil
|
||||
},
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
||||
// RateLimitOAuth2 returns a handler that limits requests per-minute based on
|
||||
// the caller. Mount it on each route, since every call is a separate bucket.
|
||||
//
|
||||
|
||||
@@ -102,6 +102,28 @@ func TestRateLimit(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("PerCallerSharesBucketAcrossPaths", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
rtr := chi.NewRouter()
|
||||
rtr.With(httpmw.RateLimitPerCaller(1, time.Second)).Post("/chats/{chat}/workspace-files", func(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
remoteAddr := randRemoteAddr()
|
||||
for i, p := range []string{
|
||||
"/chats/00000000-0000-0000-0000-000000000001/workspace-files",
|
||||
"/chats/00000000-0000-0000-0000-000000000002/workspace-files",
|
||||
} {
|
||||
req := httptest.NewRequest("POST", p, nil)
|
||||
req.RemoteAddr = remoteAddr
|
||||
rec := httptest.NewRecorder()
|
||||
rtr.ServeHTTP(rec, req)
|
||||
resp := rec.Result()
|
||||
_ = resp.Body.Close()
|
||||
require.Equal(t, i != 0, resp.StatusCode == http.StatusTooManyRequests, "request %d (%s)", i, p)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("RandomIPs", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
rtr := chi.NewRouter()
|
||||
|
||||
@@ -19,6 +19,10 @@ import (
|
||||
|
||||
const MaxStoredFileNameBytes = 255
|
||||
|
||||
// MaxMediaTypeBytes bounds client-supplied media types, parameters
|
||||
// included.
|
||||
const MaxMediaTypeBytes = 255
|
||||
|
||||
var (
|
||||
// ErrStoredFileNameRequired indicates that a durable file name is empty
|
||||
// after normalization.
|
||||
@@ -57,6 +61,28 @@ func BaseMediaType(mediaType string) string {
|
||||
return mediaType
|
||||
}
|
||||
|
||||
// ParseBaseMediaType strictly parses a client-supplied media type and
|
||||
// returns its lowercased type/subtype without parameters. Unlike
|
||||
// BaseMediaType, it rejects values that do not parse instead of
|
||||
// returning them unchanged.
|
||||
func ParseBaseMediaType(mediaType string) (string, error) {
|
||||
if len(mediaType) > MaxMediaTypeBytes {
|
||||
return "", xerrors.Errorf("media type exceeds %d bytes", MaxMediaTypeBytes)
|
||||
}
|
||||
if strings.ContainsFunc(mediaType, unicode.IsControl) {
|
||||
return "", xerrors.New("media type contains control characters")
|
||||
}
|
||||
parsed, _, err := mime.ParseMediaType(mediaType)
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("parse media type: %w", err)
|
||||
}
|
||||
// mime.ParseMediaType also accepts bare Content-Disposition tokens.
|
||||
if !strings.Contains(parsed, "/") {
|
||||
return "", xerrors.New("media type must be type/subtype")
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
// AllowedPromptInputMediaTypesString returns the supported prompt input media
|
||||
// types as a comma-separated list.
|
||||
func AllowedPromptInputMediaTypesString() string {
|
||||
|
||||
@@ -343,6 +343,38 @@ func TestIsAllowedPromptInputMediaType(t *testing.T) {
|
||||
require.False(t, chatfiles.IsAllowedPromptInputMediaType("application/zip"))
|
||||
}
|
||||
|
||||
func TestParseBaseMediaType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "plain", input: "text/csv", want: "text/csv"},
|
||||
{name: "parameters_lowercased", input: "Text/Plain; charset=utf-8", want: "text/plain"},
|
||||
{name: "no_subtype", input: "garbage", wantErr: true},
|
||||
{name: "invalid_parameter", input: "text/plain; bogus", wantErr: true},
|
||||
{name: "control_in_quoted_parameter", input: "text/plain; charset=\"\x01\"", wantErr: true},
|
||||
{name: "nul", input: "text/plain\x00", wantErr: true},
|
||||
{name: "too_long", input: "text/plain; x=" + strings.Repeat("a", chatfiles.MaxMediaTypeBytes), wantErr: true},
|
||||
{name: "empty", input: "", wantErr: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := chatfiles.ParseBaseMediaType(tt.input)
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsInlineRenderableStoredMediaType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -28,8 +28,9 @@ const (
|
||||
var ErrWorkspaceUploadNameRequired = xerrors.New("workspace upload file name is required")
|
||||
|
||||
// SanitizeWorkspaceUploadName extracts a basename, normalizes unsafe
|
||||
// whitespace, control characters, and Windows-invalid punctuation to
|
||||
// underscores, strips edge dots, and truncates the result to
|
||||
// whitespace, control characters, Windows-invalid punctuation, and
|
||||
// shell, quote, and bracket characters to underscores, strips edge dots,
|
||||
// and truncates the result to
|
||||
// MaxWorkspaceUploadFileNameBytes. It returns
|
||||
// ErrWorkspaceUploadNameRequired when no safe name remains.
|
||||
func SanitizeWorkspaceUploadName(name string) (string, error) {
|
||||
@@ -70,7 +71,10 @@ func isUnsafeWorkspaceUploadRune(r rune) bool {
|
||||
return true
|
||||
}
|
||||
switch r {
|
||||
case '<', '>', ':', '"', '|', '?', '*':
|
||||
// With the basename step resolving `/` and `\`, this covers the frontend
|
||||
// sanitizeChatFileName set so API callers cannot bypass the UI rewrite.
|
||||
case '<', '>', ':', '"', '|', '?', '*',
|
||||
'(', ')', '[', ']', '{', '}', '\'', '`', ';', ',', '&', '#', '$':
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
||||
@@ -28,6 +28,9 @@ func TestSanitizeWorkspaceUploadName(t *testing.T) {
|
||||
{name: "strip_control_chars", input: "foo\x00\x01\x1fbar.zip", want: "foo_bar.zip"},
|
||||
{name: "strip_unicode_control_chars", input: "foo\u202ebar.zip", want: "foo_bar.zip"},
|
||||
{name: "replace_windows_invalid_chars", input: `bad:name*?.zip`, want: "bad_name_.zip"},
|
||||
{name: "replace_shell_quote_bracket_chars", input: "a$(b);c'd.txt", want: "a_b_c_d.txt"},
|
||||
{name: "replace_remaining_frontend_unsafe_chars", input: "x`y[z]{w},v&u#t", want: "x_y_z_w_v_u_t"},
|
||||
{name: "only_shell_chars", input: "$()", wantErr: true},
|
||||
{name: "trim_leading_dots", input: "....bashrc", want: "bashrc"},
|
||||
{name: "trim_trailing_dots", input: "report...", want: "report"},
|
||||
{name: "unicode_passes_through", input: "résumé.pdf", want: "résumé.pdf"},
|
||||
|
||||
@@ -266,7 +266,7 @@ const unsafeChatFileNameChars = /[()[\]{}<>'"`;,:*?|&#$\\/\s\p{Cc}]/gu;
|
||||
* LLM prompts, audit logs, path interpolation) with underscores. Keeps
|
||||
* dots, dashes, underscores, ASCII alphanumerics, and non-ASCII letters
|
||||
* so localized names remain readable. The server still applies its own
|
||||
* normalization (control-char strip plus 255-byte truncate) on top of this.
|
||||
* normalization on top of this.
|
||||
*
|
||||
* If the sanitized name is empty after trimming leading or trailing `_`,
|
||||
* `.`, or whitespace, falls back to `"file"` so the server's
|
||||
|
||||
Reference in new issue
Block a user