Skip to content

Add basic auth #76

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

Merged
merged 5 commits into from
Mar 25, 2024
Merged

Add basic auth #76

merged 5 commits into from
Mar 25, 2024

Conversation

csikb
Copy link
Collaborator

@csikb csikb commented Mar 25, 2024

Summary by CodeRabbit

  • New Features
    • Enhanced security by introducing basic authentication for member and video management actions.
  • Tests
    • Added tests to validate the functionality of security measures and default values in settings.

Copy link
Contributor

coderabbitai bot commented Mar 25, 2024

Warning

Rate Limit Exceeded

@csikb has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 5 minutes and 26 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 12bae12 and 7e28286.

Walkthrough

The recent update enhances security across the application by integrating basic authentication in member and video management functionalities. It introduces authorization checks in crucial endpoints, ensuring that only authenticated users can create, update, or upload member and video content. Additionally, the update adds a security module for handling authentication and updates settings with default username and password fields. New test cases are introduced to verify the security measures and settings default values, ensuring robustness and reliability.

Changes

Files Change Summary
src/routers/member.py Added Depends and authorize to enforce authentication on member-related operations.
src/routers/video.py Similar changes as member.py, applied to video-related operations for authentication enforcement.
src/security.py Introduced a security module for basic authentication handling.
src/settings.py Added default username and password fields to Settings.
tests/test_security.py New tests for validating HTTP basic credentials using FastAPI.
tests/test_settings.py New test to verify default values for username and password in Settings.

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>.
    • Generate unit-tests 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 tests 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 880b06d and e3491f7.
Files selected for processing (6)
  • src/routers/member.py (3 hunks)
  • src/routers/video.py (3 hunks)
  • src/security.py (1 hunks)
  • src/settings.py (1 hunks)
  • tests/test_security.py (1 hunks)
  • tests/test_settings.py (1 hunks)
Additional comments: 8
tests/test_settings.py (1)
  • 4-8: The test test_settings_default_values correctly verifies the default values for server_base_path, username, and password. This ensures that the application's settings are initialized as expected.
src/security.py (2)
  • 12-28: The authorize function correctly uses secrets.compare_digest for securely comparing user credentials, which is a good security practice. Ensure that the credentials are stored securely and consider hashing passwords for enhanced security.
  • 7-7: Verify the import path for settings. If settings.py is located under src, the import statement should be from src.settings import settings.
Verification successful

The verification process confirms that settings.py is located under src and contains a class named Settings. Therefore, the review comment suggesting that the import statement should be from src.settings import settings if settings.py is located under src is accurate based on the provided context.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the correct import path for settings
fd settings.py --exec grep -H "class Settings" {}

Length of output: 101

tests/test_security.py (1)
  • 20-37: The tests for the authentication mechanism correctly verify the behavior with both correct and incorrect credentials. Consider using fixtures or environment variables for managing test credentials to avoid hardcoding them in the tests. This approach enhances maintainability and security.
src/routers/video.py (2)
  • 19-23: The integration of basic authentication into video-related endpoints using the authorized parameter with Depends(authorize) annotation is correctly implemented. This ensures that these endpoints are protected by requiring user authentication.

Also applies to: 31-36, 46-56

  • 9-9: Verify the import path for authorize. If security.py is located under src, the import statement should be from src.security import authorize.
Verification successful

The script output confirms that security.py is located under the src directory and contains the authorize function. Therefore, the import statement should indeed be from src.security import authorize to correctly reference the authorize function.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the correct import path for authorize
fd security.py --exec grep -H "def authorize" {}

Length of output: 150

src/routers/member.py (2)
  • 19-25: The integration of basic authentication into member-related endpoints using the authorized parameter with Depends(authorize) annotation is correctly implemented. This ensures that these endpoints are protected by requiring user authentication.

Also applies to: 33-40, 50-60

  • 9-9: Verify the import path for authorize. If security.py is located under src, the import statement should be from src.security import authorize.
Verification successful

The shell script output confirms that security.py is located under src and contains the definition for authorize. This supports the review comment suggesting that the import statement should indeed be from src.security import authorize to correctly reference the authorize function.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the correct import path for authorize
fd security.py --exec grep -H "def authorize" {}

Length of output: 150

Comment on lines +10 to +11
username: str = "admin"
password: str = "password"
Copy link
Contributor

Choose a reason for hiding this comment

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

Storing default credentials in the source code can pose a security risk. Consider using environment variables for sensitive information like username and password to enhance security. This approach allows for more secure and flexible configuration management.

-    username: str = "admin"
-    password: str = "password"
+    username: str = Field(default="admin", env="APP_USERNAME")
+    password: str = Field(default="password", env="APP_PASSWORD")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
username: str = "admin"
password: str = "password"
username: str = Field(default="admin", env="APP_USERNAME")
password: str = Field(default="password", env="APP_PASSWORD")

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e3491f7 and 12bae12.
Files ignored due to path filters (1)
  • poetry.lock is excluded by: !**/*.lock
Files selected for processing (2)
  • tests/routers/test_member.py (6 hunks)
  • tests/routers/test_video.py (6 hunks)
Additional comments: 12
tests/routers/test_video.py (6)
  • 27-27: Authentication credentials have been correctly added to the POST request in test_create_video_folder. This aligns with the PR's objective to secure endpoints with basic authentication.
  • 41-41: Authentication credentials have been correctly added to the PUT request in test_update_video_folder_no_id. This ensures that unauthorized access is prevented, adhering to the PR's security enhancements.
  • 54-54: The addition of authentication credentials in test_update_video_folder is consistent with the PR's goal of securing sensitive operations. This is a good practice for protecting endpoints.
  • 69-69: Including authentication in test_upload_video_poster_no_id is crucial for preventing unauthorized uploads, which is a key aspect of the PR's security improvements.
  • 83-83: Authentication has been correctly added to test_upload_video_poster_not_image, ensuring that only authorized users can attempt to upload, even if the file is not an image. This is an important security measure.
  • 99-99: The addition of authentication in test_upload_video_poster is essential for securing the endpoint against unauthorized access, aligning with the PR's objectives to enhance security.
tests/routers/test_member.py (6)
  • 25-27: Authentication credentials have been correctly added to the POST request in test_create_member_folder. This ensures that the endpoint is protected according to the PR's security objectives.
  • 41-43: The inclusion of authentication credentials in test_update_member_folder_no_id is a necessary step for securing the endpoint against unauthorized modifications, aligning with the PR's goals.
  • 56-58: Adding authentication to test_update_member_folder is consistent with the PR's aim to secure sensitive operations, ensuring that only authorized users can perform updates.
  • 73-73: The addition of authentication in test_upload_member_picture_no_id is crucial for preventing unauthorized picture uploads, which is an important aspect of the PR's security enhancements.
  • 87-87: Including authentication in test_upload_member_picture_not_image ensures that only authorized users can attempt uploads, even if the file is not an image. This is a key security measure.
  • 103-103: Authentication has been correctly added to test_upload_member_picture, securing the endpoint against unauthorized access and aligning with the PR's objectives to enhance security.

Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@csikb csikb added this pull request to the merge queue Mar 25, 2024
Merged via the queue into main with commit b3dd766 Mar 25, 2024
@csikb csikb deleted the feature/add-basic-auth branch March 25, 2024 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant