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

x-pack/filebeat/input/entityanalytics: fix encoding of client_secret #41393

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

chrisberkhout
Copy link
Contributor

@chrisberkhout chrisberkhout commented Oct 23, 2024

Proposed commit message

x-pack/filebeat/input/entityanalytics: fix encoding of client_secret (#)

In the Azure Active Directory provider, only encode the value of
`client_secret` once.

Discussion

This bug would corrupt any client secret with a character affected by URL encoding (via url.QueryEscape).

The Entra/AzureAD documentation does say:

The client secret must be URL-encoded before being sent.

but that means that the value should be encoded like normal for application/x-www-form-urlencoded data, not that the value should be URL-encoded before being encoded again as form data.

Here's an example of single vs double encoding in the Go Playground.

In microsoft-authentication-library-for-go,
the client secret is added to a url.Values{} and passed to doTokenResp,
then in doTokenResp it's passed to URLFormCall,
and URLFormCall encodes it once as form data.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

@elasticmachine
Copy link
Collaborator

Pinging @elastic/security-service-integrations (Team:Security-Service Integrations)

@botelastic botelastic bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels Oct 23, 2024
Copy link
Contributor

mergify bot commented Oct 23, 2024

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @chrisberkhout? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-8./d is the label to automatically backport to the 8./d branch. /d is the digit

Copy link
Contributor

mergify bot commented Oct 23, 2024

backport-8.x has been added to help with the transition to the new branch 8.x.
If you don't need it please use backport-skip label and remove the backport-8.x label.

@mergify mergify bot added the backport-8.x Automated backport to the 8.x branch with mergify label Oct 23, 2024
@chrisberkhout chrisberkhout added backport-8.15 Automated backport to the 8.15 branch with mergify Filebeat Filebeat labels Oct 23, 2024
@chrisberkhout chrisberkhout force-pushed the fix-double-encoding-of-client-secret branch from 455f382 to 1fda687 Compare October 23, 2024 09:16
@pierrehilbert pierrehilbert added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Oct 23, 2024
@elasticmachine
Copy link
Collaborator

Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane)

@pierrehilbert pierrehilbert requested review from rdner and removed request for khushijain21 October 23, 2024 10:43
@mauri870
Copy link
Member

Is it possible to add a testcase to TestRenew to make sure this bug never comes back? Thanks.

@chrisberkhout
Copy link
Contributor Author

Test assertion added.

Passes on new code. Fails on old code, like this:

--- FAIL: TestRenew (0.00s)
    --- FAIL: TestRenew/new-token (0.00s)
        oauth2_test.go:31:
                Error Trace:    /home/chrisberkhout/Code/beats/x-pack/filebeat/input/entityanalytics/provider/azuread/authenticator/oauth2/oauth2_test.go:31
                                                        /home/chrisberkhout/.gvm/versions/go1.22.8.linux.amd64/src/net/http/server.go:2171
                                                        /home/chrisberkhout/.gvm/versions/go1.22.8.linux.amd64/src/net/http/server.go:3142
                                                        /home/chrisberkhout/.gvm/versions/go1.22.8.linux.amd64/src/net/http/server.go:2044
                                                        /home/chrisberkhout/.gvm/versions/go1.22.8.linux.amd64/src/runtime/asm_amd64.s:1695
                Error:          Not equal:
                                expected: "value&chars=to|escape"
                                actual  : "value%26chars%3Dto%7Cescape"

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -value&chars=to|escape
                                +value%26chars%3Dto%7Cescape
                Test:           TestRenew/new-token
        oauth2_test.go:85:
                Error Trace:    /home/chrisberkhout/Code/beats/x-pack/filebeat/input/entityanalytics/provider/azuread/authenticator/oauth2/oauth2_test.go:85
                Error:          Received unexpected error:
                                failed to renew token: auth token request failed: Post "http://127.0.0.1:40129/tenant-id/oauth2/v2.0/token": EOF
                Test:           TestRenew/new-token
FAIL
exit status 1
FAIL    github.com/elastic/beats/v7/x-pack/filebeat/input/entityanalytics/provider/azuread/authenticator/oauth2 0.036s

@@ -62,12 +63,13 @@ func TestRenew(t *testing.T) {
value := "test-value"
expiresIn := 1000

srv := testSetupServer(t, value, expiresIn)
clientSecret := "value&chars=to|escape"
Copy link
Member

Choose a reason for hiding this comment

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

Looks like you have to fix this linter issue as well. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I would have just changed the name of the label here. From the docs:

The use of hard-coded passwords increases the possibility of password guessing tremendously. This plugin test looks for all string literals and checks the following conditions:

Variables are considered to look like a password if they have match any one of:

  • “password”
  • “pass”
  • “passwd”
  • “pwd”
  • “secret”
  • “token”

The next line is telling, "Note: this can be noisy and may generate false positives." This linter rule should probably not exist.

Copy link
Member

@mauri870 mauri870 left a comment

Choose a reason for hiding this comment

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

LGTM, tentatively approving. It is only missing the linter fix.

Copy link
Contributor

mergify bot commented Oct 23, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b fix-double-encoding-of-client-secret upstream/fix-double-encoding-of-client-secret
git merge upstream/main
git push upstream fix-double-encoding-of-client-secret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8.x Automated backport to the 8.x branch with mergify backport-8.15 Automated backport to the 8.15 branch with mergify bugfix Filebeat Filebeat Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team Team:Security-Service Integrations Security Service Integrations Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants