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

auth: Add option to allow basic auth for non-TLS requests #674

Merged
merged 1 commit into from
Dec 13, 2024

Conversation

lmars
Copy link
Member

@lmars lmars commented Nov 27, 2024

We internally often want to use the Go SDK in private environments (e.g. local development, or within virtual private networks), where we do not use TLS, but find it convenient to be able to use API keys rather than tokens.

This commit adds an InsecureAllowBasicAuthWithoutTLS option which permits the use of an API key when the NoTLS option is also set.

We only require this in the Go SDK, so there is no intention of adding this option to the feature spec.

Summary by CodeRabbit

  • New Features
    • Introduced a new option allowing basic authentication without TLS for enhanced flexibility.
    • Added a method to easily configure the new authentication option.
  • Bug Fixes
    • Improved security checks for basic authentication over non-TLS connections.
  • Tests
    • Added tests to ensure correct behavior of basic authentication options with and without TLS.

@lmars lmars requested a review from sacOO7 November 27, 2024 00:57
Copy link
Contributor

coderabbitai bot commented Nov 27, 2024

Walkthrough

The changes involve modifications to the authentication logic in the ably/auth.go file and the introduction of a new option in the ably/options.go file. The detectAuthMethod function now includes a refined conditional check for the opts.NoTLS variable, ensuring that basic authentication over non-TLS connections is only permitted if the InsecureAllowBasicAuthWithoutTLS option is explicitly set to true. Additionally, a new boolean field InsecureAllowBasicAuthWithoutTLS is added to the clientOptions struct, along with a corresponding function to set this option.

Changes

File Change Summary
ably/auth.go Modified detectAuthMethod to refine the conditional check for opts.NoTLS.
ably/options.go Added new field InsecureAllowBasicAuthWithoutTLS to clientOptions struct and a function to set it. Updated applyOptionsWithDefaults to include this new option.
ably/options_test.go Added new test function TestOption_NoTLS to verify behavior of basic authentication with and without TLS.

Possibly related PRs

  • [ECO-4550] Fix JWT authentication #669: This PR modifies the ably/auth.go file to enhance JWT authentication, which is directly related to the changes in the main PR that refine authentication method checks.
  • Release/1.2.21 #672: This PR introduces JWT authentication as a significant enhancement, which aligns with the main PR's focus on improving authentication logic in the ably/auth.go file.

Suggested reviewers

  • ttypic
  • umair-ably

Poem

In the fields where bunnies play,
A change has come to brighten the day.
With options new and checks refined,
Security's stronger, peace of mind.
Hop along, let’s celebrate,
For safer paths we now create! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot temporarily deployed to staging/pull/674/features November 27, 2024 00:58 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/674/godoc November 27, 2024 00:58 Inactive
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
ably/auth.go (1)

548-548: LGTM! Consider adding a security warning comment.

The condition correctly implements the opt-in behavior for using basic auth over non-TLS connections. This is a good security practice as it requires explicit acknowledgment of the insecure configuration.

Consider adding a comment to warn about the security implications:

+	// InsecureAllowBasicAuthWithoutTLS explicitly allows basic authentication over non-TLS connections.
+	// WARNING: This is inherently insecure as credentials are sent in base64 encoding. Only use this
+	// option in trusted environments like local development or VPNs.
	if opts.NoTLS && !opts.InsecureAllowBasicAuthWithoutTLS {
ably/options.go (2)

411-414: Enhance security warning in documentation

The documentation should be more explicit about the security implications of using this option.

Consider expanding the documentation to:

-	// InsecureAllowBasicAuthWithoutTLS permits an API key to be used even if the connection
-	// will not use TLS, something which would otherwise not be permitted for security reasons.
+	// InsecureAllowBasicAuthWithoutTLS permits an API key to be used even if the connection
+	// will not use TLS, something which would otherwise not be permitted for security reasons.
+	// WARNING: Using this option is highly discouraged in production environments as it may
+	// expose your API key to attackers through man-in-the-middle attacks. Only use this
+	// option in secure development environments or private networks where the security
+	// implications are fully understood.

1323-1329: Add usage example and enhance documentation

The function would benefit from a usage example and more detailed documentation.

Consider enhancing the documentation:

-// WithInsecureAllowBasicAuthWithoutTLS permits an API key to be used even if the connection
-// will not use TLS, something which would otherwise not be permitted for security reasons.
+// WithInsecureAllowBasicAuthWithoutTLS permits an API key to be used even if the connection
+// will not use TLS, something which would otherwise not be permitted for security reasons.
+//
+// WARNING: Using this option is highly discouraged in production environments as it may
+// expose your API key to attackers through man-in-the-middle attacks. Only use this
+// option in secure development environments or private networks where the security
+// implications are fully understood.
+//
+// Example usage:
+//
+//	client := ably.NewREST(
+//		WithKey("your-api-key"),
+//		WithTLS(false),
+//		WithInsecureAllowBasicAuthWithoutTLS(),
+//	)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a81632d and 0e1c8e8.

📒 Files selected for processing (2)
  • ably/auth.go (1 hunks)
  • ably/options.go (2 hunks)
🔇 Additional comments (1)
ably/options.go (1)

411-414: Implementation aligns well with PR objectives

The implementation successfully adds the requested functionality while:

  • Following established code patterns
  • Including appropriate security warnings
  • Maintaining code quality and consistency

Also applies to: 1323-1329

Copy link
Collaborator

@sacOO7 sacOO7 left a comment

Choose a reason for hiding this comment

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

Not sure why so many tests got flaky suddenly : ( .
We always had green CI for every PR before.
One of the common test I can see failing is TestRealtimeConn_RTN15i_OnErrorWhenConnected
@lmars can you check what might be going wrong?

@lmars
Copy link
Member Author

lmars commented Nov 27, 2024

@sacOO7 the failing test is TestRealtimeConn_RTN16 which I have proposed a fix for in #675.

@sacOO7
Copy link
Collaborator

sacOO7 commented Dec 4, 2024

@lmars we are planning to create one more PR to fix present enter 250 test right?

@lmars
Copy link
Member Author

lmars commented Dec 4, 2024

@sacOO7 I've created a ticket for that here:

#676

It needs to be fixed, but I don't have an immediate plan to fix it.

Given this PR is unrelated to that failure, please can you review it so I can merge?

ably/options.go Show resolved Hide resolved
ably/auth.go Show resolved Hide resolved
ably/options.go Show resolved Hide resolved
Copy link
Collaborator

@sacOO7 sacOO7 left a comment

Choose a reason for hiding this comment

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

Suggested a small renaming change, so it's easier to understand while reading the code.
Also, would be good if you can add small unit/integration test for this 👍

Copy link
Collaborator

@sacOO7 sacOO7 left a comment

Choose a reason for hiding this comment

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

LGTM, can you add unit/integration test before merging the PR please?

@sacOO7
Copy link
Collaborator

sacOO7 commented Dec 4, 2024

Simple test can be added to options_test.go, using noTls=true over basic auth returns error with code ErrInvalidUseOfBasicAuthOverNonTLSTransport

We internally often want to use the Go SDK in private environments (e.g.
local development, or within virtual private networks), where we do not
use TLS, but find it convenient to be able to use API keys rather than
tokens.

This commit adds an InsecureAllowBasicAuthWithoutTLS option which
permits the use of an API key when the NoTLS option is also set.

We only require this in the Go SDK, so there is no intention of adding
this option to the feature spec.

Signed-off-by: Lewis Marshall <[email protected]>
@lmars
Copy link
Member Author

lmars commented Dec 12, 2024

@sacOO7 test added in 631f76c.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
ably/options.go (1)

411-414: Consider enhancing security documentation

While the implementation is solid, consider adding:

  1. A comment in the documentation explaining when this option should be used (e.g., specific development scenarios, private networks)
  2. A warning about the security implications of using this option in production environments

Also applies to: 1323-1329

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0e1c8e8 and 631f76c.

📒 Files selected for processing (3)
  • ably/auth.go (1 hunks)
  • ably/options.go (2 hunks)
  • ably/options_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • ably/auth.go
🔇 Additional comments (3)
ably/options_test.go (1)

239-259: Well-structured test cases!

The test cases effectively verify both the security constraint and the override behavior. Good job on:

  • Testing both positive and negative scenarios
  • Verifying the specific error code
  • Using a realistic API key format
ably/options.go (2)

411-414: Good field naming and documentation!

The field follows Go security-related naming conventions by using the "Insecure" prefix, similar to InsecureSkipVerify. The documentation clearly explains the security implications.


1323-1329: Clean implementation following existing patterns!

The function follows the established pattern of other option setters in the codebase and maintains consistent documentation style.

@lmars lmars merged commit 85a15a3 into main Dec 13, 2024
11 of 15 checks passed
@lmars lmars deleted the allow-no-tls-basic-auth branch December 13, 2024 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants