-
-
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* 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]; | ||
|
||
|
||
// 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; | ||
|
||
dataPageObject = JSON.parse(dataPageJsonString); | ||
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. 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", "https://app.opensecdevops.com/login"); | ||
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. I'd expect this to use 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. Yes this a big error of the testing sorry |
||
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); | ||
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. Could use |
||
|
||
// Build body credentials | ||
var postData = { | ||
"email": credentials.getParam("Username"), | ||
"password": 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. | ||
// Aquí puedes verificar el código de estado de la respuesta para confirmar si la autenticación fue exitosa | ||
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. Change to English? 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. Yes no problem |
||
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 getOptionalParamsNames() { | ||
return ["Extra POST data"]; | ||
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. Not used. 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. right, go to delete. |
||
} | ||
|
||
|
||
function getCredentialsParamsNames() { | ||
return ["Username", "Password"]; | ||
} |
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