Skip to content

Commit

Permalink
Merge pull request #5272 from psiinon/authhelper/bba-dvwa
Browse files Browse the repository at this point in the history
Authhelper: trad app auth detection improvements
  • Loading branch information
thc202 committed Feb 6, 2024
2 parents fcd0151 + 69f39ee commit ce97c13
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
5 changes: 5 additions & 0 deletions addOns/authhelper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this add-on will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

### Changed
- Handle traditional apps better in authentication detection dialog.
- Make cookies set in auth request available to header based session management.

### Fixed
- Correct HTTP field names shown in diagnostic data.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,19 @@ static WebElement getPasswordField(List<WebElement> inputElements) {
* Authenticate as the given user, by filling in and submitting the login form
*
* @param wd the WebDriver controlling the browser
* @param context the context which is being used for authentication
* @param loginPageUrl the URL of the login page
* @param username the username
* @param password the password
* @return true if the login form was successfully submitted.
*/
public static boolean authenticateAsUser(
WebDriver wd, String loginPageUrl, String username, String password, int waitInSecs) {
WebDriver wd,
Context context,
String loginPageUrl,
String username,
String password,
int waitInSecs) {
wd.get(loginPageUrl);
sleep(50);
if (demoMode) {
Expand Down Expand Up @@ -278,6 +284,15 @@ public static boolean authenticateAsUser(
incStatsCounter(loginPageUrl, AUTH_BROWSER_PASSED_STATS);
AuthUtils.sleep(TimeUnit.SECONDS.toMillis(waitInSecs));

if (context != null) {
if (context.getAuthenticationMethod().getPollUrl() == null) {
// We failed to identify a suitable URL for polling.
// This can happen for more traditional apps - refresh the current one in case
// its a good option.
wd.get(wd.getCurrentUrl());
AuthUtils.sleep(TimeUnit.SECONDS.toMillis(1));
}
}
return true;
}
if (userField == null) {
Expand Down Expand Up @@ -514,6 +529,16 @@ protected static Map<String, SessionToken> getAllTokens(HttpMessage msg) {
p.getName(),
p.getValue())));
// Add Cookies
msg.getRequestHeader()
.getCookieParams()
.forEach(
c ->
addToMap(
tokens,
new SessionToken(
SessionToken.COOKIE_SOURCE,
c.getName(),
c.getValue())));
msg.getResponseHeader()
.getHttpCookies(null)
.forEach(
Expand Down Expand Up @@ -731,12 +756,14 @@ static class AuthenticationBrowserHook implements BrowserHook {

private BrowserBasedAuthenticationMethod bbaMethod;
private UsernamePasswordAuthenticationCredentials userCreds;
private Context context;

AuthenticationBrowserHook(Context context, String userName) {
this(context, getUser(context, userName));
}

AuthenticationBrowserHook(Context context, User user) {
this.context = context;
AuthenticationMethod method = context.getAuthenticationMethod();
if (!(method instanceof BrowserBasedAuthenticationMethod)) {
throw new IllegalStateException("Unsupported method " + method.getType().getName());
Expand All @@ -757,6 +784,7 @@ public void browserLaunched(SeleniumScriptUtils ssutils) {
"AuthenticationBrowserHook - authenticating as {}", userCreds.getUsername());
AuthUtils.authenticateAsUser(
ssutils.getWebDriver(),
context,
bbaMethod.getLoginPageUrl(),
userCreds.getUsername(),
userCreds.getPassword(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ public WebSession authenticate(
.getExtensionLoader()
.getExtension(ExtensionSelenium.class);

Context context = Model.getSingleton().getSession().getContext(user.getContextId());

try {
proxyPort = getProxy(user.getContext()).start(proxyHost, 0);

Expand All @@ -353,6 +355,7 @@ public WebSession authenticate(

if (AuthUtils.authenticateAsUser(
wd,
context,
loginPageUrl,
userCreds.getUsername(),
userCreds.getPassword(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,32 @@ void shouldExtractJsonTokens() throws Exception {
assertThat(tokens.get("header:Content-Type").getValue(), is(equalTo("application/json")));
}

@Test
void shouldExtractCookies() throws Exception {
// Given
HttpMessage msg =
new HttpMessage(
new HttpRequestHeader(
"GET https://example.com/ HTTP/1.1\r\n"
+ "Host: example.com\r\n"
+ "Cookie: aaa=bbb\r\n\r\n"),
new HttpRequestBody("Request Body"),
new HttpResponseHeader(
"HTTP/1.1 200 OK\r\n" + "Set-Cookie: ccc=ddd; HttpOnly; Secure"),
new HttpResponseBody("Response Body"));
// When
Map<String, SessionToken> tokens = AuthUtils.getAllTokens(msg);

// Then
System.out.println(tokens);
assertThat(tokens.size(), is(equalTo(3)));
assertThat(tokens.get("cookie:aaa").getValue(), is(equalTo("bbb")));
assertThat(tokens.get("cookie:ccc").getValue(), is(equalTo("ddd")));
assertThat(
tokens.get("header:Set-Cookie").getValue(),
is(equalTo("ccc=ddd; HttpOnly; Secure")));
}

@Test
void shouldGetEmptyHeaderTokens() throws Exception {
// Given
Expand Down

0 comments on commit ce97c13

Please sign in to comment.