feat: add safe CarrierNode media deletion
- Validate media paths before sending delete requests - Treat 404 as an already-missing file - Test removal, errors, and path validation
This commit is contained in:
1 parent
fb4274bd5c
commit
61d8da5fb3
2 files changed
+135
No files matched your search
@@ -467,6 +467,47 @@ func (c *Client) DeleteMountFile(ctx context.Context, project, mountType, relati
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteMedia removes one library file from the primary CarrierNode. carrierNodePath is the
|
||||||
|
// "/media/..." request path the caller probed, so the delete targets the same file the probe
|
||||||
|
// found. It reports whether CarrierNode removed a file: a 404 is a clean "not there" and returns
|
||||||
|
// false with a nil error, so a caller can tell a removal apart from a file that was already gone.
|
||||||
|
func (c *Client) DeleteMedia(ctx context.Context, carrierNodePath string) (bool, error) {
|
||||||
|
if !validMediaRequestPath(carrierNodePath) {
|
||||||
|
return false, fmt.Errorf("invalid media path")
|
||||||
|
}
|
||||||
|
request, err := c.newRequest(ctx, c.primaryBaseURL(ctx), http.MethodDelete, carrierNodePath, nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
response, err := httpClient.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
switch {
|
||||||
|
case response.StatusCode == http.StatusNotFound:
|
||||||
|
return false, nil
|
||||||
|
case response.StatusCode >= 400:
|
||||||
|
return false, fmt.Errorf("carriernode media delete failed: %s", response.Status)
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// validMediaRequestPath accepts only a /media/ request path with no ".." segment after
|
||||||
|
// unescaping, so a delete cannot name a path outside the media route.
|
||||||
|
func validMediaRequestPath(carrierNodePath string) bool {
|
||||||
|
if !strings.HasPrefix(carrierNodePath, "/media/") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, segment := range strings.Split(carrierNodePath, "/") {
|
||||||
|
decoded, err := url.PathUnescape(segment)
|
||||||
|
if err != nil || decoded == ".." {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// ThumbnailMountType is the mount type ProjectConfigurator configures for a project's
|
// ThumbnailMountType is the mount type ProjectConfigurator configures for a project's
|
||||||
// generated thumbnails. Each thumbnail is one "<id>.jpg" file under that mount.
|
// generated thumbnails. Each thumbnail is one "<id>.jpg" file under that mount.
|
||||||
const ThumbnailMountType = "thumbnails"
|
const ThumbnailMountType = "thumbnails"
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package carriernodeclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"slices"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// mediaDeleteServer answers DELETE /media/... with the status for each path (default 404) and records
|
||||||
|
// every request so a test can see what reached CarrierNode. A path answered with 200 is removed by that
|
||||||
|
// delete, so a later request for it gets 404, as it would from a real CarrierNode.
|
||||||
|
type mediaDeleteServer struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
statuses map[string]int
|
||||||
|
requests []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *mediaDeleteServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
path := r.URL.EscapedPath()
|
||||||
|
n.mu.Lock()
|
||||||
|
n.requests = append(n.requests, r.Method+" "+path)
|
||||||
|
status, ok := n.statuses[path]
|
||||||
|
if !ok {
|
||||||
|
status = http.StatusNotFound
|
||||||
|
}
|
||||||
|
if r.Method == http.MethodDelete && status == http.StatusOK {
|
||||||
|
n.statuses[path] = http.StatusNotFound
|
||||||
|
}
|
||||||
|
n.mu.Unlock()
|
||||||
|
w.WriteHeader(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *mediaDeleteServer) seen() []string {
|
||||||
|
n.mu.Lock()
|
||||||
|
defer n.mu.Unlock()
|
||||||
|
return append([]string(nil), n.requests...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMediaDeleteClient(t *testing.T, statuses map[string]int) (*Client, *mediaDeleteServer) {
|
||||||
|
t.Helper()
|
||||||
|
node := &mediaDeleteServer{statuses: statuses}
|
||||||
|
server := httptest.NewServer(node)
|
||||||
|
t.Cleanup(server.Close)
|
||||||
|
return New("cn-secret", func(context.Context) string { return server.URL }), node
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteMediaReportsRemovalAndAlreadyMissing(t *testing.T) {
|
||||||
|
client, node := newMediaDeleteClient(t, map[string]int{"/media/Videos/clip.mp4": http.StatusOK})
|
||||||
|
|
||||||
|
removed, err := client.DeleteMedia(context.Background(), "/media/Videos/clip.mp4")
|
||||||
|
if err != nil || !removed {
|
||||||
|
t.Fatalf("first delete = (%v, %v), want (true, nil)", removed, err)
|
||||||
|
}
|
||||||
|
removed, err = client.DeleteMedia(context.Background(), "/media/Videos/clip.mp4")
|
||||||
|
if err != nil || removed {
|
||||||
|
t.Fatalf("second delete = (%v, %v), want (false, nil) for a file already gone", removed, err)
|
||||||
|
}
|
||||||
|
want := []string{"DELETE /media/Videos/clip.mp4", "DELETE /media/Videos/clip.mp4"}
|
||||||
|
if got := node.seen(); !slices.Equal(got, want) {
|
||||||
|
t.Fatalf("requests = %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteMediaReportsServerErrors(t *testing.T) {
|
||||||
|
client, _ := newMediaDeleteClient(t, map[string]int{"/media/Videos/clip.mp4": http.StatusInternalServerError})
|
||||||
|
|
||||||
|
removed, err := client.DeleteMedia(context.Background(), "/media/Videos/clip.mp4")
|
||||||
|
if err == nil || removed {
|
||||||
|
t.Fatalf("delete = (%v, %v), want (false, error) for a 500", removed, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteMediaRefusesPathsOutsideTheMediaRoute(t *testing.T) {
|
||||||
|
client, node := newMediaDeleteClient(t, nil)
|
||||||
|
|
||||||
|
for _, path := range []string{
|
||||||
|
"/api/media/Videos/clip.mp4",
|
||||||
|
"/mount/media/Videos/clip.mp4",
|
||||||
|
"media/Videos/clip.mp4",
|
||||||
|
"/media/../secret.mp4",
|
||||||
|
"/media/Videos/%2e%2e/%2e%2e/secret.mp4",
|
||||||
|
"",
|
||||||
|
} {
|
||||||
|
if removed, err := client.DeleteMedia(context.Background(), path); err == nil || removed {
|
||||||
|
t.Fatalf("DeleteMedia(%q) = (%v, %v), want (false, error)", path, removed, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if got := node.seen(); len(got) != 0 {
|
||||||
|
t.Fatalf("refused paths still reached CarrierNode: %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user