Skip to content

Commit

Permalink
fix: fixes missed cases to validate user scope when downloading, stre…
Browse files Browse the repository at this point in the history
…aming and actions (#3460)
  • Loading branch information
amir20 authored Dec 14, 2024
1 parent f2e56f7 commit bd0a81f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
15 changes: 15 additions & 0 deletions assets/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,21 @@ declare global {
// @ts-ignore
export type { Component, ComponentPublicInstance, ComputedRef, DirectiveBinding, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, MaybeRef, MaybeRefOrGetter, VNode, WritableComputedRef } from 'vue'
import('vue')
// @ts-ignore
export type { DrawerWidth } from './composable/drawer'
import('./composable/drawer')
// @ts-ignore
export type { LogStreamSource } from './composable/eventStreams'
import('./composable/eventStreams')
// @ts-ignore
export type { Config, Profile } from './stores/config'
import('./stores/config')
// @ts-ignore
export type { Host } from './stores/hosts'
import('./stores/hosts')
// @ts-ignore
export type { Settings } from './stores/settings'
import('./stores/settings')
}

// for vue template auto import
Expand Down
11 changes: 11 additions & 0 deletions internal/web/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ func (h *handler) containerActions(w http.ResponseWriter, r *http.Request) {
action := chi.URLParam(r, "action")
id := chi.URLParam(r, "id")

validIdMap, err := h.validContainerIDsForHost(r, hostKey(r))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if _, ok := validIdMap[id]; !ok {
http.Error(w, "container not found", http.StatusUnauthorized)
return
}

containerService, err := h.multiHostService.FindContainer(hostKey(r), id)
if err != nil {
log.Error().Err(err).Msg("error while trying to find container")
Expand Down
2 changes: 1 addition & 1 deletion internal/web/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Test_handler_containerActions_unknown_container(t *testing.T) {

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, 404, rr.Code)
assert.Equal(t, 401, rr.Code)
}

func Test_handler_containerActions_start(t *testing.T) {
Expand Down
57 changes: 55 additions & 2 deletions internal/web/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,46 @@ import (
"github.com/docker/docker/pkg/stdcopy"
"github.com/dustin/go-humanize"
"github.com/go-chi/chi/v5"
"github.com/samber/lo"

"github.com/rs/zerolog/log"
)

func (h *handler) validContainerIDsForHost(r *http.Request, host string) (map[string]docker.Container, error) {
usersFilter := h.config.Filter
if h.config.Authorization.Provider != NONE {
user := auth.UserFromContext(r.Context())
if user.ContainerFilter.Exists() {
usersFilter = user.ContainerFilter
}
}

validContainers, err := h.multiHostService.ListContainersForHost(host, usersFilter)
if err != nil {
return nil, err
}

validIdMap := lo.KeyBy(validContainers, func(item docker.Container) string {
return item.ID
})

return validIdMap, nil
}

func (h *handler) downloadLogs(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")

validIdMap, err := h.validContainerIDsForHost(r, hostKey(r))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if _, ok := validIdMap[id]; !ok {
http.Error(w, "container not found", http.StatusUnauthorized)
return
}

containerService, err := h.multiHostService.FindContainer(hostKey(r), id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down Expand Up @@ -103,6 +137,17 @@ func (h *handler) fetchLogsBetweenDates(w http.ResponseWriter, r *http.Request)
return
}

validIdMap, err := h.validContainerIDsForHost(r, hostKey(r))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if _, ok := validIdMap[id]; !ok {
http.Error(w, "container not found", http.StatusUnauthorized)
return
}

containerService, err := h.multiHostService.FindContainer(hostKey(r), id)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
Expand Down Expand Up @@ -420,8 +465,16 @@ loop:
}
sseWriter.Message(logEvent)
case container := <-newContainers:
events <- &docker.ContainerEvent{ActorID: container.ID, Name: "container-started", Host: container.Host}
go streamLogs(container)
validIdMap, err := h.validContainerIDsForHost(r, container.Host)
if err != nil {
log.Error().Err(err).Msg("error fetching valid container IDs")
continue
}

if _, ok := validIdMap[container.ID]; ok {
events <- &docker.ContainerEvent{ActorID: container.ID, Name: "container-started", Host: container.Host}
go streamLogs(container)
}

case event := <-events:
log.Debug().Str("event", event.Name).Str("container", event.ActorID).Msg("received event")
Expand Down

0 comments on commit bd0a81f

Please sign in to comment.