-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
Create LaravelInertia.js #436
Open
GoldraK
wants to merge
2
commits into
zaproxy:main
Choose a base branch
from
GoldraK:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+139
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* A script to provide authentication for Laravel InertiaJS apps. | ||
* | ||
* First it makes a GET request and obtains the XSRF-TOKEN and Cookie Session from the response body. | ||
* | ||
* Then it makes a POST request with a body which contains username, password and X-XSRF-TOKEN. | ||
* | ||
* A successful login will result in a 302 redirect. If this happens, a GET request is made to the redirect URL. | ||
* | ||
* Every request made by this script is logged separately to the History tab. | ||
*/ | ||
|
||
|
||
function authenticate(helper, paramsValues, credentials) { | ||
|
||
var AuthenticationHelper = Java.type('org.zaproxy.zap.authentication.AuthenticationHelper'); | ||
var HttpRequestHeader = Java.type("org.parosproxy.paros.network.HttpRequestHeader"); | ||
var HttpHeader = Java.type("org.parosproxy.paros.network.HttpHeader"); | ||
var URI = Java.type("org.apache.commons.httpclient.URI"); | ||
|
||
var targetURL = paramsValues.get("Target URL"); | ||
var baseURL = targetURL.match(/^(.+?[^\/:](?=[?\/]|$))/i)[1]; | ||
|
||
// | ||
// First, make a GET request to the login page to get and extract the | ||
// csrfmiddlewaretoken from it. | ||
// | ||
|
||
// Build message. | ||
var firstRequestURI = new URI(targetURL, false); | ||
var firstRequestMethod = HttpRequestHeader.GET; | ||
var firstRequestMainHeader = new HttpRequestHeader(firstRequestMethod, firstRequestURI, HttpHeader.HTTP11); | ||
var firstMsg = helper.prepareMessage(); | ||
firstMsg.setRequestHeader(firstRequestMainHeader); | ||
|
||
|
||
// Send message. | ||
helper.sendAndReceive(firstMsg, false); | ||
|
||
// Add message to ZAP history. | ||
AuthenticationHelper.addAuthMessageToHistory(firstMsg); | ||
|
||
|
||
// Get the csrf token from the response. | ||
var csrfTokenValueRegEx = /XSRF-TOKEN=([A-Za-z0-9]*%3D)/i; | ||
|
||
var csrfTokenValue = firstMsg.getResponseHeader().toString().match(csrfTokenValueRegEx)[1]; | ||
|
||
var cookieName = paramsValues.get("Session Cookie name") | ||
// Get the csrf token from the response. | ||
var cookieSessionRegEx = /osdo_session=([A-Za-z0-9]*%3D)/i; | ||
var cookieSessionValue = firstMsg.getResponseHeader().toString().match(cookieSessionRegEx)[1]; | ||
|
||
|
||
// Get Inertia version | ||
var dataPageRegEx = /<div id=\"app\" data-page=\"([^\"]+)\"/i | ||
var dataPageValue = firstMsg.getResponseBody().toString().match(dataPageRegEx)[1]; | ||
|
||
var dataPageJsonString = dataPageValue.replace(/"/g, '"'); | ||
var dataPageObject = JSON.parse(dataPageJsonString); | ||
|
||
if (dataPageObject) { | ||
var inertiaVersion = dataPageObject.version; | ||
} | ||
|
||
// Now, make a POST request to the login page with user credentials and | ||
|
||
var secondRequestURI = new URI(targetURL, false); | ||
var secondRequestMethod = HttpRequestHeader.POST; | ||
var secondRequestHeader = new HttpRequestHeader(secondRequestMethod, secondRequestURI, HttpHeader.HTTP11); | ||
|
||
var secondMsg = helper.prepareMessage(); | ||
secondMsg.setRequestHeader(secondRequestHeader); | ||
|
||
// add headers | ||
secondMsg.getRequestHeader().setHeader("X-XSRF-TOKEN", decodeURIComponent(csrfTokenValue)); | ||
secondMsg.getRequestHeader().setHeader("Content-Type", "application/json"); | ||
secondMsg.getRequestHeader().setHeader("X-Requested-With", "XMLHttpRequest"); | ||
secondMsg.getRequestHeader().setHeader("Referer", targetURL); | ||
secondMsg.getRequestHeader().setHeader("X-Inertia", 'true'); | ||
secondMsg.getRequestHeader().setHeader("X-Inertia-Version", inertiaVersion); | ||
secondMsg.getRequestHeader().setHeader("Accept", "text/html, application/xhtml+xml"); | ||
|
||
// Send cookies | ||
secondMsg.getRequestHeader().setHeader(HttpHeader.COOKIE, "XSRF-TOKEN=" + csrfTokenValue + "; osdo_session=" + cookieSessionValue); | ||
|
||
// Build body credentials | ||
var postData = { | ||
paramsValues.get("Username field"): credentials.getParam("Username"), | ||
paramsValues.get("PPassword field") : credentials.getParam("Password"), | ||
"remember": "" | ||
}; | ||
|
||
|
||
secondMsg.setRequestBody(JSON.stringify(postData)); | ||
|
||
|
||
secondMsg.getRequestHeader().setContentLength(secondMsg.getRequestBody().length()); | ||
|
||
helper.sendAndReceive(secondMsg, false); | ||
|
||
// Get the status code of the response. | ||
var secondResponseStatusCode = secondMsg.getResponseHeader().getStatusCode(); | ||
|
||
// | ||
// If the login is successful, the login page will respond with a 302 | ||
// redirect. If it does, follow that redirect. | ||
// | ||
if (secondResponseStatusCode == "302" && secondMsg.getResponseHeader().getHeader('Location') != targetURL) { | ||
// Add secondMsg to ZAP history | ||
AuthenticationHelper.addAuthMessageToHistory(secondMsg); | ||
|
||
// Build the URL to redirect to. | ||
var redirectURL = secondMsg.getResponseHeader().getHeader('Location'); | ||
|
||
// Build message. | ||
var thirdRequestURI = new URI(redirectURL, false); | ||
var thirdRequestMethod = HttpRequestHeader.GET; | ||
var thirdRequestMainHeader = new HttpRequestHeader(thirdRequestMethod, thirdRequestURI, HttpHeader.HTTP11); | ||
var thirdMsg = helper.prepareMessage(); | ||
thirdMsg.setRequestHeader(thirdRequestMainHeader); | ||
|
||
helper.sendAndReceive(thirdMsg, false); | ||
|
||
return thirdMsg; | ||
} else { | ||
return secondMsg; | ||
} | ||
|
||
} | ||
|
||
|
||
function getRequiredParamsNames() { | ||
return ["Target URL", "Username field", "Password field", "Session Cookie name"]; | ||
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. Some of these are not used. |
||
} | ||
|
||
function getCredentialsParamsNames() { | ||
return ["Username", "Password"]; | ||
} |
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.
I'd have expected the URLs to be properly encoded.
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.
I don't understand the problem, it's how I saw it being done here
https://github.com/GoldraK/community-scripts/blob/main/authentication/DjangoAuthentication.js#L29