Skip to content
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

Client Script Based Auth (Ajax Spider) #6112

Merged
merged 1 commit into from
Jan 28, 2025

Conversation

kingthorin
Copy link
Member

@kingthorin kingthorin commented Jan 22, 2025

Overview

Allow the Ajax Spider to use Client Side scripts to authenticate, when used in conjunction with the Auth Helper add-on.

  • Update change logs.
  • Update build files with necessary config/dependencies.
  • Auth Helper:
    • Expand visibility of findSessionTokenSource in AuthUtils.
    • Add new authentication method type, ClientScriptBasedAuthenticationMethodType.
    • ExtensionAuthhelperClient updated to facilitate the new functionality.
    • Added intertnal.ZestAuthRunner which will also be used by the Client Spider in the future. This ensures that browsers are launched as expected and auth handled before other actions.
    • ClientScriptBasedAuthHandler handler for the new auth method type, responsible for enabling/disabling auth methods for Users, and providing the necessary BrowserHook.
    • ExtensionAuthhelperAjax updated to 'hook' and use the new auth method.
    • Help updates/additions.
    • Messages.properties added necessary KVPs to support the functionality.
  • Automation Framework
    • Add handling to AuthenticationData.
    • Help updates/additions.
  • Zest
    • Minor changes to ZestAuthenticationRunner to facilitate the new functionality.

Related Issues

n/a

Checklist

  • Update help
  • Update changelog
  • Run ./gradlew spotlessApply for code formatting
  • Write tests
  • Check code coverage
  • Sign-off commits
  • Squash commits
  • Use a descriptive title

@psiinon

This comment was marked as off-topic.

@kingthorin kingthorin force-pushed the auth/client-script branch 2 times, most recently from 7e2f645 to 4323379 Compare January 22, 2025 17:09
@kingthorin kingthorin changed the title Client Script Based Auth (WIP) Client Script Based Auth Jan 22, 2025
@kingthorin kingthorin changed the title Client Script Based Auth Client Script Based Auth (Ajax Spider) Jan 23, 2025
@kingthorin
Copy link
Member Author

I believe this is ready for review.

There is an outstanding issue where one of the browser windows may not close. I'm investigating.

Copy link
Member

@psiinon psiinon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one minor comment. I'm sure theres a load of dup code from core here so at some point it would be really good to rationalise it. But maybe not today ;)

@kingthorin
Copy link
Member Author

Thanks for reviewing, I figured things would need to move around and that dependencies might be wrong, some of that I blindly trusted and some of it I just wasn't confident about the changes myself.

@kingthorin kingthorin force-pushed the auth/client-script branch 3 times, most recently from f841f32 to d02f036 Compare January 24, 2025 14:09
@kingthorin
Copy link
Member Author

kingthorin commented Jan 24, 2025

At d02f036 it's failing to load the script when running via automation:

904848 [ZAP-Automation] ERROR org.zaproxy.zap.ZAP.UncaughtExceptionLogger - Exception in thread "ZAP-Automation"
java.lang.NoClassDefFoundError: org/zaproxy/zest/core/v1/ZestScript
	at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[?:?]
	at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3402) ~[?:?]
	at java.base/java.lang.Class.getMethodsRecursive(Class.java:3543) ~[?:?]
	at java.base/java.lang.Class.getMethod0(Class.java:3529) ~[?:?]
	at java.base/java.lang.Class.getMethod(Class.java:2225) ~[?:?]
	at org.apache.commons.lang3.reflect.MethodUtils.getMethodObject(MethodUtils.java:439) ~[commons-lang3-3.17.0.jar:3.17.0]
	at org.apache.commons.lang3.reflect.MethodUtils.getMatchingAccessibleMethod(MethodUtils.java:323) ~[commons-lang3-3.17.0.jar:3.17.0]
	at org.apache.commons.lang3.reflect.MethodUtils.invokeMethod(MethodUtils.java:843) ~[commons-lang3-3.17.0.jar:3.17.0]
	at org.apache.commons.lang3.reflect.MethodUtils.invokeMethod(MethodUtils.java:931) ~[commons-lang3-3.17.0.jar:3.17.0]
	at org.apache.commons.lang3.reflect.MethodUtils.invokeMethod(MethodUtils.java:905) ~[commons-lang3-3.17.0.jar:3.17.0]
	at org.zaproxy.addon.automation.AuthenticationData.initContextAuthentication(AuthenticationData.java:351) ~[?:?]
	at org.zaproxy.addon.automation.ContextWrapper.createContext(ContextWrapper.java:329) ~[?:?]
	at org.zaproxy.addon.automation.AutomationEnvironment.create(AutomationEnvironment.java:174) ~[?:?]
	at org.zaproxy.addon.automation.ExtensionAutomation.runPlan(ExtensionAutomation.java:369) ~[?:?]
	at org.zaproxy.addon.automation.ExtensionAutomation.lambda$runPlanAsync$4(ExtensionAutomation.java:437) ~[?:?]
	at java.base/java.lang.Thread.run(Thread.java:840) [?:?]
Caused by: java.lang.ClassNotFoundException
	at org.zaproxy.zap.control.AddOnClassLoader.findClass(AddOnClassLoader.java:330) ~[main/:?]
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:592) ~[?:?]
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525) ~[?:?]
	... 16 more

Edit: 13c993a should be the same.

@kingthorin kingthorin force-pushed the auth/client-script branch 2 times, most recently from 8d1964e to 13c993a Compare January 24, 2025 14:18
@kingthorin
Copy link
Member Author

Here's the plan and script to re-create, you'll have to edit the script path to be correct for your system.

Zest Test Script
{
  "about": "This is a Zest script. For more details about Zest visit https://github.com/zaproxy/zest/",
  "zestVersion": "0.8",
  "title": "RecordedTestAuth",
  "description": "",
  "prefix": "",
  "type": "StandAlone",
  "parameters": {
    "tokenStart": "{{",
    "tokenEnd": "}}",
    "tokens": {},
    "elementType": "ZestVariables"
  },
  "statements": [
    {
      "windowHandle": "windowHandle1",
      "browserType": "firefox",
      "url": "http://localhost:9091/auth/simple-json-cookie/",
      "capabilities": "",
      "headless": false,
      "profilePath": "",
      "index": 1,
      "enabled": true,
      "elementType": "ZestClientLaunch"
    },
    {
      "windowHandle": "windowHandle1",
      "type": "id",
      "element": "user",
      "index": 2,
      "enabled": true,
      "elementType": "ZestClientElementClick"
    },
    {
      "value": "[email protected]",
      "windowHandle": "windowHandle1",
      "type": "id",
      "element": "user",
      "index": 3,
      "enabled": true,
      "elementType": "ZestClientElementSendKeys"
    },
    {
      "value": "password123",
      "windowHandle": "windowHandle1",
      "type": "id",
      "element": "password",
      "index": 4,
      "enabled": true,
      "elementType": "ZestClientElementSendKeys"
    },
    {
      "windowHandle": "windowHandle1",
      "type": "id",
      "element": "login",
      "index": 5,
      "enabled": true,
      "elementType": "ZestClientElementClick"
    }
  ],
  "authentication": [],
  "index": 0,
  "enabled": true,
  "elementType": "ZestScript"
}
AF Test Plan
env:
  contexts:
  - name: AjaxSpiderAuthTest
    urls:
    - http://localhost:9091/auth/simple-json-cookie
    includePaths:
    - http://localhost:9091/auth/simple-json-cookie.*
    authentication:
      method: client
      parameters:
        script: /some/path/RecordedTestAuth.zst
        scriptEngine: Mozilla Zest
      verification:
        method: poll
        loggedInRegex: \Q 200 OK\E
        loggedOutRegex: \Q 403 Forbidden\E
        pollFrequency: 60
        pollUnits: seconds
        pollUrl: http://localhost:9091/auth/simple-json-cookie/user
        pollPostData: ""
    sessionManagement:
      method: headers
      parameters:
        Cookie: "sid={%cookie:sid%}; _random=blahblah"
    technology: {}
    structure: {}
    users:
    - name: test
      credentials:
        Username: [email protected]
        Password: password123
  parameters: {}
jobs:
- type: spiderAjax
  parameters:
    context: AjaxSpiderAuthTest
    user: test
    browserId: firefox
  tests:
  - name: At least 30 URLs found
    type: stats
    onFail: INFO
    statistic: spiderAjax.urls.added
    operator: '>='
    value: 30

@thc202
Copy link
Member

thc202 commented Jan 25, 2025

#6112 (comment)

I didn't double check but that seems to be because of #6112 (comment) since the add-on itself doesn't have access to Zest.

Edit: further edits, I think the first version was/is correct as it's trying to instantiate the other class, which is the one that needs access to but we'll see.

@kingthorin
Copy link
Member Author

Should be functional now, with the build file changes.

@kingthorin kingthorin force-pushed the auth/client-script branch 7 times, most recently from 756075e to da7a849 Compare January 27, 2025 18:08
@psiinon
Copy link
Member

psiinon commented Jan 28, 2025

Should also update the AF docs, e.g. method on https://www.zaproxy.org/docs/desktop/addons/automation-framework/environment/
Happy for that to be in another PR though - we'll need more docs once we "announce" client script auth support

@kingthorin
Copy link
Member Author

Added help content.

@kingthorin
Copy link
Member Author

Now with updated Auth Helper help too 😀

@kingthorin kingthorin force-pushed the auth/client-script branch 2 times, most recently from 1fb0025 to 451f52b Compare January 28, 2025 14:18
Comment on lines +68 to +70
myVarTwo: ${myVarOne}.VarTwo # Can refer other vars
parameters:
failOnError: true # If set exit on an error
failOnError: true # If set exit on an error
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes these white spaces changes are unrelated but it looked horrible, could we just go with it 😀

@kingthorin
Copy link
Member Author

Tweaked

@kingthorin
Copy link
Member Author

Got those.

@kingthorin
Copy link
Member Author

Got all those now too 🤞

@kingthorin
Copy link
Member Author

Tweaked again

@thc202
Copy link
Member

thc202 commented Jan 28, 2025

Thank you!

@thc202 thc202 enabled auto-merge January 28, 2025 15:47
@kingthorin
Copy link
Member Author

Yay 🥳

@thc202 thc202 merged commit 3304419 into zaproxy:main Jan 28, 2025
9 of 10 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 28, 2025
@kingthorin kingthorin deleted the auth/client-script branch January 28, 2025 16:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants