diff --git a/authentication/LaravelInertia.js b/authentication/LaravelInertia.js new file mode 100644 index 00000000..c190fdd2 --- /dev/null +++ b/authentication/LaravelInertia.js @@ -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 = /