Skip to content

[SECURITY] Don't accept any new parameter names from pull request comments#85

Merged
jakub-bochenski merged 1 commit intojenkinsci:masterfrom
proski:parameter-security
Jun 3, 2019
Merged

[SECURITY] Don't accept any new parameter names from pull request comments#85
jakub-bochenski merged 1 commit intojenkinsci:masterfrom
proski:parameter-security

Conversation

@proski
Copy link

@proski proski commented Apr 22, 2019

Parameters extracted from Stash comments should only be allowed to
override the default values of the parameters defined for the job.

Accepting arbitrary parameters from Stash comments can be exploited by a
user that can post comments but not administer the job in Jenkins.

Pass only one copy of the parameter to the job. Remove null parameters
only after the values from the Stash comments have been applied.

@proski
Copy link
Author

proski commented Apr 22, 2019

Suppressed an issue reported by with FindBugs. The fix is in #86, but I don't want to mix it with a security fix.

@proski
Copy link
Author

proski commented Apr 24, 2019

Rebased, added instructions to README.md.

@proski proski changed the title Don't accept any new parameter names from pull request comments [SECURITY] Don't accept any new parameter names from pull request comments May 24, 2019
@proski proski mentioned this pull request May 24, 2019
Copy link

@jakub-bochenski jakub-bochenski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some unit tests would be nice, to make sure we'r not introducing any bugs.
Otherwise LGTM

@jakub-bochenski jakub-bochenski requested a review from jimklimov May 24, 2019 14:05
@proski
Copy link
Author

proski commented May 25, 2019

Some unit tests would be nice, to make sure we'r not introducing any bugs.
Otherwise LGTM

I tried writing unit tests, and it looks like I'm writing throwaway code. It's hard to create an StashBuildTrigger object. But the good thing is that all the code changed by this PR doesn't need to be in StashBuildTrigger. I believe its natural place is in StashRepository.

The first reason is the control flow. The StashRepository object is created by the StashBuildTrigger object (through StashPullRequestsBuilder, which is a small and rather pointless class). Why should StashRepository pass control back to StashBuildTrigger to start the job? All that code doesn't interact with the rest of StashBuildTrigger.

The second reason is the assumption that this.job is not null. That's an essential assumption in the job starting code, but not in other parts of StashBuildTrigger. That's why we have suppressions for FindBugs warnings.

Would it be possible to merge #86 first? Then this PR would deal with StashRepository. I'll be able to mock StashBuildTrigger instead of trying to create a real object.

Sure, tests for StashBuildTrigger are needed too, but not for the code that doesn't belong there.

proski referenced this pull request May 27, 2019
Take the values for the environment variables directly from StashCause.

This fixes warnings about parameters for freestyle projects. Also, the
environment variables are listed at one place now.
@proski
Copy link
Author

proski commented May 28, 2019

I've added the unit tests. It was an interesting exercise. I used JenkinsRule#createProject to create a project object and the used spy on it to modify its behavior as needed. I used start but not run on the trigger. I used Mockito's ArgumentCaptor to capture the arguments passed to scheduleBuild2. I'm surprised it's working!

package stashpullrequestbuilder.stashpullrequestbuilder;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: I would prefer we don't use wildcard imports, most IDEs have the option to expand them

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It's common to use wildcards for matchers in unit tests, but it's not a style requirement, so I'm fine either way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to apply google style guide to this someday, it will be less changes if we don't use wildcards

}

private List<ParameterValue> captureJobParameters() {
when(project.getQuietPeriod()).thenReturn(0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit awkward, doing both expectations and verification in the same method
any chance we could split this without making the code yet more complex?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, the only purpose of verification is to capture the parameter, so it's a bit misleading. But I split that method anyway without making the unit tests any longer.

@jakub-bochenski
Copy link

I'm surprised it's working!

Thanks a lot for figuring it out!
If you could address the comment #85 (comment) it would be great. If not this change is still making the code quality much better than it was before

@proski
Copy link
Author

proski commented Jun 1, 2019

Addressed the PR comments.

Parameters extracted from Stash comments are only allowed to override the
default values of the parameters defined for the job.

Accepting arbitrary parameters from Stash comments can be exploited by a
user who can post comments but not administer the Jenkins job.

Pass only one copy of the parameter to the job. Remove null parameters
only after the values from the Stash comments have been applied.

Suppress a FindBugs warning about this.job being null. That cannot
happen, but FindBugs cannot figure it out.
additionalParameters);
}

private void jobSetup(List<ParameterDefinition> parameterDefinitions) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

varargs would make this even easier to use I think

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Created #98.

@jakub-bochenski jakub-bochenski merged commit 2f1711b into jenkinsci:master Jun 3, 2019
@proski proski deleted the parameter-security branch June 3, 2019 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants