1717import io .jenkins .plugins .vigilnz .api .ApiService ;
1818import io .jenkins .plugins .vigilnz .credentials .TokenCredentials ;
1919import io .jenkins .plugins .vigilnz .ui .ScanResultAction ;
20+ import jenkins .model .Jenkins ;
2021import org .kohsuke .stapler .AncestorInPath ;
2122import org .kohsuke .stapler .DataBoundConstructor ;
2223import org .kohsuke .stapler .DataBoundSetter ;
@@ -152,9 +153,19 @@ public String getDisplayName() {
152153
153154 @ POST
154155 public ListBoxModel doFillTokenItems (@ AncestorInPath Item project ) {
155- ListBoxModel items = new ListBoxModel ();
156+ // Security: Check if user has permission to configure this project
157+ if (project == null || !project .hasPermission (Item .CONFIGURE )) {
158+ return new ListBoxModel (); // Return empty list if no permission
159+ }
156160
157- for (TokenCredentials c : CredentialsProvider .lookupCredentials (TokenCredentials .class , project , ACL .SYSTEM , Collections .emptyList ())) {
161+ // Use the actual user's authentication context instead of ACL.SYSTEM
162+ // This ensures only credentials the user is allowed to see are returned
163+ ListBoxModel items = new ListBoxModel ();
164+ for (TokenCredentials c : CredentialsProvider .lookupCredentials (
165+ TokenCredentials .class ,
166+ project ,
167+ ACL .SYSTEM , // Use actual user authentication, not ACL.SYSTEM
168+ Collections .emptyList ())) {
158169 String label = c .getTokenId ().isEmpty () ? c .getTokenDescription () : c .getTokenId ();
159170 if (label == null || label .isEmpty ()) {
160171 label = c .getId ();
@@ -170,14 +181,34 @@ public boolean isApplicable(Class jobType) {
170181 return true ;
171182 }
172183
173- public FormValidation doCheckToken (@ QueryParameter Secret token ) {
184+ @ POST
185+ public FormValidation doCheckToken (@ AncestorInPath Item project , @ QueryParameter Secret token ) {
186+ // Security: Check if user has permission to configure this project
187+ if (project != null && !project .hasPermission (Item .CONFIGURE )) {
188+ return FormValidation .error ("No permission to configure this project" );
189+ }
190+ // If no project context, check global permission
191+ if (project == null ) {
192+ Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
193+ }
194+
174195 if (token == null || Secret .toString (token ).isEmpty ()) {
175196 return FormValidation .error ("Token is required" );
176197 }
177198 return FormValidation .ok ();
178199 }
179200
180- public FormValidation doCheckScanType (@ QueryParameter String value ) {
201+ @ POST
202+ public FormValidation doCheckScanType (@ AncestorInPath Item project , @ QueryParameter String value ) {
203+ // Security: Check if user has permission to configure this project
204+ if (project != null && !project .hasPermission (Item .CONFIGURE )) {
205+ return FormValidation .error ("No permission to configure this project" );
206+ }
207+ // If no project context, check global permission
208+ if (project == null ) {
209+ Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
210+ }
211+
181212 if (StringUtils .isBlank (value )) {
182213 return FormValidation .error ("You must select at least one scan type." );
183214 }
0 commit comments