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

File manager exception fix #2229

Merged
merged 8 commits into from
Nov 3, 2024
Merged

File manager exception fix #2229

merged 8 commits into from
Nov 3, 2024

Conversation

KesterTan
Copy link
Contributor

Description

This PR fixes the routing when a user is not allowed to access a path. This also adds unit tests which tests this functionality for this fix.

Resolves #2228

How Has This Been Tested?

  • Create a course and assign a student to that course
  • Navigate to that course in the file manager and copy the link
  • Sign in as a student using dev login and try to access the link
  • Note that you will not be able to see anything in that path and note the flash[:error]
  • Run the new unit tests: rake spec SPEC=./spec/controllers/file_manager_controller_spec.rb and make sure that everything passes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have run rubocop and erblint for style check. If you haven't, run overcommit --install && overcommit --sign to use pre-commit hook for linting
  • My change requires a change to the documentation, which is located at Autolab Docs
  • I have updated the documentation accordingly, included in this PR

Copy link
Contributor

coderabbitai bot commented Oct 4, 2024

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in the pull request involve modifications to the FileManagerController to enhance error handling and authorization checks for file management actions. Key methods such as index, delete, rename, download_tar, and upload_file have been updated to include authorization checks, ensuring unauthorized users are redirected to the file manager index with an appropriate flash error message. Additionally, the test suite for the controller has been expanded to include shared examples for unauthorized access scenarios, improving coverage for various actions.

Changes

File Change Summary
app/controllers/file_manager_controller.rb Enhanced error handling and authorization checks in index, delete, rename, download_tar, and upload_file methods.
spec/controllers/file_manager_controller_spec.rb Added shared examples for unauthorized access tests and modified existing tests for various actions to include authorization checks.

Assessment against linked issues

Objective Addressed Explanation
Ensure users are redirected to the index page if not authorized (2228)
Prevent unauthorized access to file management actions (2228)

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 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.

@KesterTan KesterTan requested review from a team and jlge and removed request for a team October 4, 2024 18:46
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: 10

🧹 Outside diff range and nitpick comments (1)
spec/controllers/file_manager_controller_spec.rb (1)

164-164: Redundant sign_in calls within test examples

In several test examples (lines 164, 221, 278, 284, 351, and 358), sign_in is called inside the test block even though the user is already signed in within the before(:each) block. This is unnecessary and can be removed to clean up the tests.

Consider removing the redundant sign_in calls:

-        sign_in(instructor)

Also applies to: 221-221, 278-278, 284-284, 351-351, 358-358

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between be48fe4 and 59f9684.

📒 Files selected for processing (2)
  • app/controllers/file_manager_controller.rb (5 hunks)
  • spec/controllers/file_manager_controller_spec.rb (2 hunks)
🔇 Additional comments (2)
spec/controllers/file_manager_controller_spec.rb (2)

26-33: Well-defined shared example for unauthorized access

The unauthorized_access shared example is correctly implemented. It enhances test coverage by ensuring unauthorized access scenarios are consistently tested across different contexts.


352-352: ⚠️ Potential issue

Verify correctness of constructed path parameters

At lines 352 and 359, the path parameter in get :download_tar includes "autopopulated/test/#{dir_path}" and "autopopulated/test/#{file_path}". Ensure that these paths are correctly constructed and correspond to the actual file system paths used in the application.

Run the following script to check the constructed paths:

Also applies to: 359-359

app/controllers/file_manager_controller.rb Outdated Show resolved Hide resolved
app/controllers/file_manager_controller.rb Outdated Show resolved Hide resolved
app/controllers/file_manager_controller.rb Outdated Show resolved Hide resolved
app/controllers/file_manager_controller.rb Outdated Show resolved Hide resolved
app/controllers/file_manager_controller.rb Outdated Show resolved Hide resolved
app/controllers/file_manager_controller.rb Outdated Show resolved Hide resolved
spec/controllers/file_manager_controller_spec.rb Outdated Show resolved Hide resolved
spec/controllers/file_manager_controller_spec.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@jlge jlge left a comment

Choose a reason for hiding this comment

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

When I login as a student user and access the file_manager path, it shows the base screen, but no error flash is displayed.
image
I think a more ideal behavior would be to redirect back to the Autolab homepage and flash the "You are not authorized to view this page." message there. Also when an unauthorized user views the file manager, they can still press the "create folder" and "upload files" buttons which I know don't work but I think it's unideal, so this can be prevented by redirecting out to a different page.

@KesterTan KesterTan requested a review from jlge October 27, 2024 18:29
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: 5

🧹 Outside diff range and nitpick comments (2)
app/controllers/file_manager_controller.rb (2)

Line range hint 30-48: Improve consistency in authorization handling

The authorization check has been improved, but there are a few consistency issues:

  1. Using root_path instead of file_manager_index_path like other methods
  2. Missing HTTP status code in the redirect

Apply this change:

    else
      flash[:error] = "You are not authorized to view this path"
-     redirect_to root_path
+     redirect_to file_manager_index_path, status: :forbidden
    end

Line range hint 1-258: Consider implementing a before_action filter for authorization

The controller has duplicate authorization logic across multiple actions. Consider implementing a before_action filter to centralize the authorization checks and reduce code duplication.

Example implementation:

class FileManagerController < ApplicationController
  before_action :authorize_path_access!, except: [:index]
  
  private
  
  def authorize_path_access!
    path = params[:path].presence || ""
    absolute_path = check_path_exist(path)
    
    unless check_instructor(absolute_path)
      flash[:error] = "You are not authorized to access this path"
      redirect_to file_manager_index_path, status: :forbidden and return
    end
  end
end
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 59f9684 and f84e60e.

📒 Files selected for processing (2)
  • app/controllers/file_manager_controller.rb (7 hunks)
  • spec/controllers/file_manager_controller_spec.rb (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • spec/controllers/file_manager_controller_spec.rb

app/controllers/file_manager_controller.rb Show resolved Hide resolved
app/controllers/file_manager_controller.rb Show resolved Hide resolved
app/controllers/file_manager_controller.rb Show resolved Hide resolved
app/controllers/file_manager_controller.rb Show resolved Hide resolved
app/controllers/file_manager_controller.rb Show resolved Hide resolved
Copy link
Contributor

@jlge jlge left a comment

Choose a reason for hiding this comment

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

lgtm!

@KesterTan KesterTan added this pull request to the merge queue Nov 3, 2024
Merged via the queue into master with commit 72d658b Nov 3, 2024
6 checks passed
@KesterTan KesterTan deleted the file_manager_exception_fix branch November 3, 2024 17: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.

Fix File Manager Authentication and Add File Manager Tests
2 participants