Skip to content

[JENKINS-57154] Fix configureSecurity HTTP 403 err #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2019

Conversation

samrocketman
Copy link
Member

If an admin visits the configureSecurity page in Jenkins, then every user queried will attempt to be impersonated as part of determining if they're a user. However, it's possible for some users to revoke the OAuth app or no longer have access so impersonation is not possible due to an invalid token.

The GithubAuthenticationToken class did not properly surface an error when a token authentication was not valid.

See also:

  • JENKINS-57154 Regression in github-oauth-plugin 0.32 breaks /configureSecurity page

@samrocketman
Copy link
Member Author

@Wadeck @agentgonzo I've tested this locally in a Jenkins instance after replicating the bug JENKINS-57154 and verified the fix is good. I'm not sure how to test this automatically as code, though.

@samrocketman samrocketman requested a review from Wadeck August 4, 2019 02:26
@@ -458,7 +458,7 @@ private String getAccessToken(@Nonnull String code) throws IOException {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpost = new HttpPost(githubWebUri
+ "/login/oauth/access_token?" + "client_id=" + clientID + "&"
+ "client_secret=" + clientSecret + "&" + "code=" + code);
+ "client_secret=" + clientSecret.getPlainText() + "&" + "code=" + code);
Copy link
Member Author

Choose a reason for hiding this comment

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

Unrelated to JENKINS-57154. This is to fix the following warning.

WARNING: Use of toString() on hudson.util.Secret from java.lang.String.valueOf(String.java:2994). Prefer getPlainText() or getEncryptedValue() depending your needs. see https://jenkins.io/redirect/hudson.util.Secret/

@samrocketman
Copy link
Member Author

I'll leave this open for a day before merging. We could always write tests later after the fact as well.

@samrocketman samrocketman force-pushed the bugfix-JENKINS-57154 branch 4 times, most recently from a27d922 to 82ca634 Compare August 4, 2019 02:41
@samrocketman
Copy link
Member Author

I still get

HTTP ERROR 403

Problem accessing /configureSecurity/configure. Reason:

    No valid crumb was included in the request

When attempting to save the security page so this isn't fixed...

@samrocketman
Copy link
Member Author

Every time I visit http://localhost:8080/configureSecurity/ then http://localhost:8080/crumbIssuer/api/json?pretty=true changes... this is different from what I recall

@samrocketman
Copy link
Member Author

Every time I visit http://localhost:8080/configureSecurity/ then http://localhost:8080/crumbIssuer/api/json?pretty=true changes... this is different from what I recall

Replicates the same behavior as logging out and logging back in... I think my the security realm searching the matrix auth is to blame because it basically seems like a complete re-auth.

@samrocketman
Copy link
Member Author

Well this isn't a complete fix because the CSRF token keeps getting regenerated when visiting the security page.

Copy link
Contributor

@Wadeck Wadeck left a comment

Choose a reason for hiding this comment

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

Not tested manually but nothing seems wrong :)

@samrocketman samrocketman force-pushed the bugfix-JENKINS-57154 branch from 82ca634 to 279dcf9 Compare August 6, 2019 01:44
} catch (IOException e) {
throw new UserMayOrMayNotExistException("Could not connect to GitHub API server, target URL = " + getGithubApiUri(), e);
}
SecurityContextHolder.getContext().setAuthentication(token);
Copy link
Member Author

@samrocketman samrocketman Aug 6, 2019

Choose a reason for hiding this comment

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

SecurityContextHolder.getContext().setAuthentication(token); is what kept throwing the CSRF errors because global security page would generate a new CSRF token for the authenticated user every time the page was visited. So the form on the page would have an out-of-date CSRF token since doCheck would generate a new one for the logged in user (admin).

If an admin visits the `configureSecurity` page in Jenkins, then every
user queried will attempt to be impersonated as part of determining if
they're a user.  However, it's possible for some users to revoke the
OAuth app or no longer have access so impersonation is not possible due
to an invalid token.

The `GithubAuthenticationToken` class did not properly surface an error
when a token authentication was not valid.

See also:

- [JENKINS-57154][JENKINS-57154] Regression in github-oauth-plugin 0.32
  breaks /configureSecurity page

[JENKINS-57154]: https://issues.jenkins-ci.org/browse/JENKINS-57154
@samrocketman samrocketman force-pushed the bugfix-JENKINS-57154 branch from 279dcf9 to 65f0d3d Compare August 6, 2019 02:03
@samrocketman
Copy link
Member Author

  • ✔️ Tested impersonation.
  • ✔️ Tested CSRF state vulnerability fix.
  • ✔️ Tested resolving users and groups in global security configuration page when matrix authorization is configured.
  • ✔️ Tested updating settings on the global security configuration page when matrix authorization is configured. I did not receive any CSRF form errors.
Impersonation script console code (click to expand)
import jenkins.model.Jenkins
import org.acegisecurity.context.SecurityContextHolder

Jenkins.instance.ACL.as(User.get('samrocketman')).with { ctx ->
    try {
        // access Jenkins job only user has access
        println "Can delete? ${(Jenkins.instance.getItemByFullName('test')?.hasPermission(Item.DELETE))? "Yes" : "No"}."
        println "Granted Authorities"
        println "    ${SecurityContextHolder.getContext().getAuthentication().authorities.join('\n    ')}"
    } finally {
        ctx.close()
    }
}

@samrocketman samrocketman merged commit bb9d1ef into master Aug 6, 2019
@samrocketman samrocketman deleted the bugfix-JENKINS-57154 branch August 6, 2019 03:42
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