@@ -88,11 +88,57 @@ func (q *querier) GetAuditLogsOffset(ctx context.Context, arg database.GetAuditL
88
88
}
89
89
90
90
func (q * querier ) GetFileByHashAndCreator (ctx context.Context , arg database.GetFileByHashAndCreatorParams ) (database.File , error ) {
91
- return fetch (q .log , q .auth , q .db .GetFileByHashAndCreator )(ctx , arg )
91
+ file , err := q .db .GetFileByHashAndCreator (ctx , arg )
92
+ if err != nil {
93
+ return database.File {}, err
94
+ }
95
+ err = q .authorizeContext (ctx , rbac .ActionRead , file )
96
+ if err != nil {
97
+ // Check the user's access to the file's templates.
98
+ if q .authorizeUpdateFileTemplate (ctx , file ) != nil {
99
+ return database.File {}, err
100
+ }
101
+ }
102
+
103
+ return file , nil
92
104
}
93
105
94
106
func (q * querier ) GetFileByID (ctx context.Context , id uuid.UUID ) (database.File , error ) {
95
- return fetch (q .log , q .auth , q .db .GetFileByID )(ctx , id )
107
+ file , err := q .db .GetFileByID (ctx , id )
108
+ if err != nil {
109
+ return database.File {}, err
110
+ }
111
+ err = q .authorizeContext (ctx , rbac .ActionRead , file )
112
+ if err != nil {
113
+ // Check the user's access to the file's templates.
114
+ if q .authorizeUpdateFileTemplate (ctx , file ) != nil {
115
+ return database.File {}, err
116
+ }
117
+ }
118
+
119
+ return file , nil
120
+ }
121
+
122
+ // authorizeReadFile is a hotfix for the fact that file permissions are
123
+ // independent of template permissions. This function checks if the user has
124
+ // update access to any of the file's templates.
125
+ func (q * querier ) authorizeUpdateFileTemplate (ctx context.Context , file database.File ) error {
126
+ tpls , err := q .db .GetFileTemplates (ctx , file .ID )
127
+ if err != nil {
128
+ return err
129
+ }
130
+ // There __should__ only be 1 template per file, but there can be more than
131
+ // 1, so check them all.
132
+ for _ , tpl := range tpls {
133
+ // If the user has update access to any template, they have read access to the file.
134
+ if err := q .authorizeContext (ctx , rbac .ActionUpdate , tpl ); err == nil {
135
+ return nil
136
+ }
137
+ }
138
+
139
+ return NotAuthorizedError {
140
+ Err : xerrors .Errorf ("not authorized to read file %s" , file .ID ),
141
+ }
96
142
}
97
143
98
144
func (q * querier ) InsertFile (ctx context.Context , arg database.InsertFileParams ) (database.File , error ) {
@@ -859,11 +905,22 @@ func (q *querier) UpdateTemplateScheduleByID(ctx context.Context, arg database.U
859
905
}
860
906
861
907
func (q * querier ) UpdateTemplateVersionByID (ctx context.Context , arg database.UpdateTemplateVersionByIDParams ) (database.TemplateVersion , error ) {
862
- template , err := q .db .GetTemplateByID (ctx , arg .TemplateID .UUID )
908
+ // An actor is allowed to update the template version if they are authorized to update the template.
909
+ tv , err := q .db .GetTemplateVersionByID (ctx , arg .ID )
863
910
if err != nil {
864
911
return database.TemplateVersion {}, err
865
912
}
866
- if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
913
+ var obj rbac.Objecter
914
+ if ! tv .TemplateID .Valid {
915
+ obj = rbac .ResourceTemplate .InOrg (tv .OrganizationID )
916
+ } else {
917
+ tpl , err := q .db .GetTemplateByID (ctx , tv .TemplateID .UUID )
918
+ if err != nil {
919
+ return database.TemplateVersion {}, err
920
+ }
921
+ obj = tpl
922
+ }
923
+ if err := q .authorizeContext (ctx , rbac .ActionUpdate , obj ); err != nil {
867
924
return database.TemplateVersion {}, err
868
925
}
869
926
return q .db .UpdateTemplateVersionByID (ctx , arg )
0 commit comments