-
Notifications
You must be signed in to change notification settings - Fork 612
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
Added GitHub signature checking feature + tests #77
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c4f2f78
Checking "X-Hub-Signature" on GitHub hook POST reception. Added test …
dd33f21
FIX: Moved signature checking to GhprbRepository. Added tests
e36d52c
FIX: added UTF-8 encoding to project reporting in pom.xml
cc899f3
FIX: pulled latests changes from master
4cb28e3
Merge branch 'master' of https://github.com/jenkinsci/ghprb-plugin in…
67cdc1c
Merge branch 'master' of https://github.com/jenkinsci/ghprb-plugin in…
c3b851b
Merge branch 'master' of https://github.com/jenkinsci/ghprb-plugin in…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.logging.Level; | ||
|
@@ -47,27 +50,31 @@ public String getUrlName() { | |
|
||
public void doIndex(StaplerRequest req, StaplerResponse resp) { | ||
String event = req.getHeader("X-GitHub-Event"); | ||
String signature = req.getHeader("X-Hub-Signature"); | ||
String type = req.getContentType(); | ||
String payload = null; | ||
String body = null; | ||
|
||
if ("application/json".equals(type)) { | ||
BufferedReader br = null; | ||
try { | ||
br = req.getReader(); | ||
payload = IOUtils.toString(req.getReader()); | ||
} catch (IOException e) { | ||
body = extractRequestBody(req); | ||
if (body == null) { | ||
logger.log(Level.SEVERE, "Can't get request body for application/json."); | ||
return; | ||
} finally { | ||
IOUtils.closeQuietly(br); | ||
} | ||
payload = body; | ||
} else if ("application/x-www-form-urlencoded".equals(type)) { | ||
payload = req.getParameter("payload"); | ||
if (payload == null) { | ||
body = extractRequestBody(req); | ||
if (body == null || body.length() <= 8) { | ||
logger.log(Level.SEVERE, "Request doesn't contain payload. " | ||
+ "You're sending url encoded request, so you should pass github payload through 'payload' request parameter"); | ||
return; | ||
} | ||
try { | ||
payload = URLDecoder.decode(body.substring(8), req.getCharacterEncoding()); | ||
} catch (UnsupportedEncodingException e) { | ||
logger.log(Level.SEVERE, "Error while trying to encode the payload"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be decode, not encode |
||
return; | ||
} | ||
} | ||
|
||
if (payload == null) { | ||
|
@@ -79,13 +86,28 @@ public void doIndex(StaplerRequest req, StaplerResponse resp) { | |
|
||
for (GhprbWebHook webHook : getWebHooks()) { | ||
try { | ||
webHook.handleWebHook(event, payload); | ||
webHook.handleWebHook(event, payload, body, signature); | ||
} catch (Exception e) { | ||
logger.log(Level.SEVERE, "Unable to process web hook for: " + webHook.getProjectName(), e); | ||
} | ||
} | ||
|
||
} | ||
|
||
private String extractRequestBody(StaplerRequest req) { | ||
String body = null; | ||
BufferedReader br = null; | ||
try { | ||
br = req.getReader(); | ||
body = IOUtils.toString(br); | ||
} catch (IOException e) { | ||
body = null; | ||
} finally { | ||
IOUtils.closeQuietly(br); | ||
} | ||
return body; | ||
} | ||
|
||
|
||
private Set<GhprbWebHook> getWebHooks() { | ||
final Set<GhprbWebHook> webHooks = new HashSet<GhprbWebHook>(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the payload is no longer directly taken "as is". Instead, the full body is taken. If the POST is a json then payload == actual json == body, but if it is an encoded URL then the body is something like:
payload=
The reason we discard the payload and we take the body is because the signature has been made by github using the full body, so now I extract the body, and from there I extract the payload.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed anyway. Now it has the old checking, just in case.