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
12 changes: 12 additions & 0 deletions api/v1alpha1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,24 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Error"
"403":
description: Forbidden
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"404":
description: NotFound
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500":
description: Internal error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/api/v1/sources/{id}/image:
head:
tags:
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/spec.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions internal/api/client/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/api/server/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions internal/handlers/v1alpha1/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,26 @@ func (s *ServiceHandler) HeadImage(ctx context.Context, request server.HeadImage

// (GET /api/v1/sources/{id}/image-url)
func (s *ServiceHandler) GetSourceDownloadURL(ctx context.Context, request server.GetSourceDownloadURLRequestObject) (server.GetSourceDownloadURLResponseObject, error) {
url, expireAt, err := s.sourceSrv.GetSourceDownloadURL(ctx, request.Id)
source, err := s.sourceSrv.GetSource(ctx, request.Id)
if err != nil {
switch err.(type) {
case *service.ErrResourceNotFound:
return server.GetSourceDownloadURL404JSONResponse{Message: err.Error()}, nil
default:
return server.GetSourceDownloadURL400JSONResponse{}, nil // FIX: should be 500
Comment thread
ronenav marked this conversation as resolved.
return server.GetSourceDownloadURL500JSONResponse{Message: fmt.Sprintf("failed to load source %s: %v", request.Id, err)}, nil
}
}

user := auth.MustHaveUser(ctx)
if user.Username != source.Username || user.Organization != source.OrgID {
message := fmt.Sprintf("forbidden to access source %s by user with org_id %s", request.Id, user.Organization)
return server.GetSourceDownloadURL403JSONResponse{Message: message}, nil
}

url, expireAt, err := s.sourceSrv.GetSourceDownloadURL(ctx, request.Id)
if err != nil {
return server.GetSourceDownloadURL500JSONResponse{Message: fmt.Sprintf("failed to get download URL for source %s: %v", request.Id, err)}, nil
}
Comment thread
ronenav marked this conversation as resolved.
return server.GetSourceDownloadURL200JSONResponse{Url: url, ExpiresAt: &expireAt}, nil
}

Expand Down
121 changes: 121 additions & 0 deletions internal/handlers/v1alpha1/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,4 +1091,125 @@ var _ = Describe("source handler", Ordered, func() {
gormdb.Exec("DELETE FROM sources;")
})
})

Context("GetSourceDownloadURL", func() {
It("successfully returns image URL for owned source", func() {
sourceID := uuid.New()
tx := gormdb.Exec(fmt.Sprintf(insertSourceWithUsernameStm, sourceID, "admin", "admin"))
Expect(tx.Error).To(BeNil())

// Insert image_infra record (required for URL generation)
insertImageInfraStm := `INSERT INTO image_infras (source_id) VALUES ('%s');`
tx = gormdb.Exec(fmt.Sprintf(insertImageInfraStm, sourceID))
Expect(tx.Error).To(BeNil())

user := auth.User{
Username: "admin",
Organization: "admin",
EmailDomain: "admin.example.com",
}
ctx := auth.NewTokenContext(context.TODO(), user)

srv := handlers.NewServiceHandler(service.NewSourceService(s, nil), service.NewAssessmentService(s, nil, nil), nil, service.NewSizerService(nil, s), nil, nil, nil)
resp, err := srv.GetSourceDownloadURL(ctx, server.GetSourceDownloadURLRequestObject{Id: sourceID})
Expect(err).To(BeNil())
Expect(reflect.TypeOf(resp).String()).To(Equal(reflect.TypeOf(server.GetSourceDownloadURL200JSONResponse{}).String()))

result := resp.(server.GetSourceDownloadURL200JSONResponse)
Expect(result.Url).NotTo(BeEmpty())
Expect(result.ExpiresAt).NotTo(BeNil())
})
Comment thread
ronenav marked this conversation as resolved.

It("returns 403 when trying to access another org's source", func() {
// Create source owned by "batman" org
victimSourceID := uuid.New()
tx := gormdb.Exec(fmt.Sprintf(insertSourceWithUsernameStm, victimSourceID, "batman", "batman"))
Expect(tx.Error).To(BeNil())

insertImageInfraStm := `INSERT INTO image_infras (source_id) VALUES ('%s');`
tx = gormdb.Exec(fmt.Sprintf(insertImageInfraStm, victimSourceID))
Expect(tx.Error).To(BeNil())

// Attempt to access with "joker" credentials
attackerUser := auth.User{
Username: "joker",
Organization: "joker",
EmailDomain: "joker.example.com",
}
ctx := auth.NewTokenContext(context.TODO(), attackerUser)

srv := handlers.NewServiceHandler(service.NewSourceService(s, nil), service.NewAssessmentService(s, nil, nil), nil, service.NewSizerService(nil, s), nil, nil, nil)
resp, err := srv.GetSourceDownloadURL(ctx, server.GetSourceDownloadURLRequestObject{Id: victimSourceID})

Expect(err).To(BeNil())
Expect(reflect.TypeOf(resp).String()).To(Equal(reflect.TypeOf(server.GetSourceDownloadURL403JSONResponse{}).String()))
})

It("returns 403 when accessing with same username but different org", func() {
// Create source owned by "batman" user in "batman-org"
victimSourceID := uuid.New()
tx := gormdb.Exec(fmt.Sprintf(insertSourceWithUsernameStm, victimSourceID, "batman", "batman-org"))
Expect(tx.Error).To(BeNil())

insertImageInfraStm := `INSERT INTO image_infras (source_id) VALUES ('%s');`
tx = gormdb.Exec(fmt.Sprintf(insertImageInfraStm, victimSourceID))
Expect(tx.Error).To(BeNil())

// Attempt to access with same username but different organization
attackerUser := auth.User{
Username: "batman",
Organization: "evil-org",
EmailDomain: "evil.example.com",
}
ctx := auth.NewTokenContext(context.TODO(), attackerUser)

srv := handlers.NewServiceHandler(service.NewSourceService(s, nil), service.NewAssessmentService(s, nil, nil), nil, service.NewSizerService(nil, s), nil, nil, nil)
resp, err := srv.GetSourceDownloadURL(ctx, server.GetSourceDownloadURLRequestObject{Id: victimSourceID})

Expect(err).To(BeNil())
Expect(reflect.TypeOf(resp).String()).To(Equal(reflect.TypeOf(server.GetSourceDownloadURL403JSONResponse{}).String()))
})

It("returns 404 for non-existent source", func() {
user := auth.User{
Username: "admin",
Organization: "admin",
EmailDomain: "admin.example.com",
}
ctx := auth.NewTokenContext(context.TODO(), user)

srv := handlers.NewServiceHandler(service.NewSourceService(s, nil), service.NewAssessmentService(s, nil, nil), nil, service.NewSizerService(nil, s), nil, nil, nil)
resp, err := srv.GetSourceDownloadURL(ctx, server.GetSourceDownloadURLRequestObject{Id: uuid.New()})

Expect(err).To(BeNil())
Expect(reflect.TypeOf(resp).String()).To(Equal(reflect.TypeOf(server.GetSourceDownloadURL404JSONResponse{}).String()))
})

It("returns 500 when URL generation fails after auth succeeds", func() {
sourceID := uuid.New()
tx := gormdb.Exec(fmt.Sprintf(insertSourceWithUsernameStm, sourceID, "admin", "admin"))
Expect(tx.Error).To(BeNil())

// NOTE: Deliberately NOT inserting image_infra record
// This passes auth (user owns source) but fails during URL generation

user := auth.User{
Username: "admin",
Organization: "admin",
EmailDomain: "admin.example.com",
}
ctx := auth.NewTokenContext(context.TODO(), user)

srv := handlers.NewServiceHandler(service.NewSourceService(s, nil), service.NewAssessmentService(s, nil, nil), nil, service.NewSizerService(nil, s), nil, nil, nil)
resp, err := srv.GetSourceDownloadURL(ctx, server.GetSourceDownloadURLRequestObject{Id: sourceID})

Expect(err).To(BeNil())
Expect(reflect.TypeOf(resp).String()).To(Equal(reflect.TypeOf(server.GetSourceDownloadURL500JSONResponse{}).String()))
})

AfterEach(func() {
gormdb.Exec("DELETE FROM image_infras;")
gormdb.Exec("DELETE FROM sources;")
})
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
})
Loading