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

Do not ask for password if not needed. #69

Merged
merged 2 commits into from
Mar 10, 2024

Conversation

sstark
Copy link
Contributor

@sstark sstark commented Mar 9, 2024

In certain circumstances we do not need to ask for a password because we already have previously authenticated to sudo and can just reuse the credentials.

Since there is no way to check if a sudo timestamp has timed out we run a simple command and check the result.

Using an existing sudo timestamp from within nvim seems to work only if the timestamp_type is set to 'global' in the sudo configuration file.

The assesment of the risk of setting the security relevant setting timestamp_type to 'global' needs to be done by the user.

Summary by CodeRabbit

  • Refactor
    • Updated the method of command execution in the suda plugin to improve user experience by reducing unnecessary password prompts.
  • Documentation
    • Enhanced suda functionality documentation to include support for global timestamps for sudo authentication.

In certain circumstances we do not need to ask for a password because we
already have previously authenticated to sudo and can just reuse the
credentials.

Since there is no way to check if a sudo timestamp has timed out we run
a simple command and check the result.

Using an existing sudo timestamp from within nvim seems to work only if
the timestamp_type is set to 'global' in the sudo configuration file.

The assesment of the risk of setting the security relevant setting
timestamp_type to 'global' needs to be done by the user.
Copy link

coderabbitai bot commented Mar 9, 2024

Walkthrough

This update enhances the suda.vim plugin by improving the suda#system function. It now smartly checks for an active sudo session by trying a non-interactive command first. If this check fails, meaning there's no active sudo timestamp, it then prompts the user for a password. This approach streamlines operations by avoiding unnecessary password requests, making the user experience smoother and more efficient.

Changes

File(s) Summary of Changes
autoload/suda.vim Updated suda#system to first attempt non-interactive command execution for sudo timestamp before prompting for password.
doc/suda.txt Enhanced suda to support global timestamps for sudo authentication, enabling token reuse without password prompts.

🐇✨
In the realm of code, where vim commands dance,
A clever rabbit hopped, seeking a chance.
To smooth the path, with a magical tweak,
No more passwords, if sudo's not weak.
🌟🐾
"Hop, skip, a leap," it said with a grin,
"For smoother coding, let the magic begin!"
🎉🐇

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

@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 8b0fc37 and 621169a.
Files selected for processing (1)
  • autoload/suda.vim (1 hunks)

Comment on lines +12 to +29
" Let's try running a command non-interactively. If it works, we have a sudo
" timestamp that has not timed out yet. In this case there is no need to ask
" for a password.
" This only works if the timestamp_type is set to 'global' in the sudo
" configuation file. It does not work with 'ppid', 'kernel' or 'tty'.
let cmd = printf('%s -n true', g:suda#executable)
let result = system(cmd)
if v:shell_error == 0
let cmd = printf('%s %s', g:suda#executable, a:cmd)
else
try
call inputsave()
redraw | let password = inputsecret(g:suda#prompt)
finally
call inputrestore()
endtry
let cmd = printf('%s -p '''' -S %s', g:suda#executable, a:cmd)
endif
Copy link

Choose a reason for hiding this comment

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

The implementation of the non-interactive command execution to check for an active sudo timestamp before prompting for a password is a clever solution to improve user experience. However, there are a few points to consider for refinement:

  1. Security Consideration Comment: It's good practice to include a comment about the security implications of setting the sudo timestamp type to 'global', as mentioned in the PR description. This helps ensure that users are aware of the potential risks involved.

  2. Error Handling: The current implementation assumes that if v:shell_error is not 0 after the non-interactive command execution, it's safe to proceed with password prompting. However, it might be beneficial to consider specific error codes that indicate a lack of sudo timestamp versus other potential errors that could occur during command execution.

  3. Password Variable Scope: The password variable is used only within the else block but is appended to the command outside of this block. This could lead to a situation where the password variable is not defined. Consider initializing the password variable before the if statement or restructuring the logic to ensure that it's always defined when needed.

  4. Duplication of Command Construction: The command construction logic (let cmd = printf('%s %s', g:suda#executable, a:cmd)) is repeated before and after the non-interactive check. This could be refactored to avoid duplication and enhance maintainability.

  5. Documentation: Adding documentation for the new logic within the suda#system function would be helpful for future maintainers. This documentation could explain the rationale behind the non-interactive command execution and the conditions under which it is expected to work.

Consider addressing these points to refine the implementation and ensure clarity and robustness in the code.

Comment on lines +15 to +16
" This only works if the timestamp_type is set to 'global' in the sudo
" configuation file. It does not work with 'ppid', 'kernel' or 'tty'.
Copy link
Owner

Choose a reason for hiding this comment

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

Please explain this behavior and limitation in doc/suda.txt as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Please let me know if you are ok with an extra commit like that or you want me to squash it.

Copy link

@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: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 621169a and 5639260.
Files selected for processing (1)
  • doc/suda.txt (1 hunks)
Additional comments: 1
doc/suda.txt (1)
  • 40-50: The documentation now includes a section explaining how users can configure sudo with Defaults timestamp_type = global to avoid unnecessary password prompts when the sudo session is still valid. This is a valuable addition for users who have sudo version 1.8.21 or higher and prefer not to enter their password multiple times during a session. However, it's crucial to emphasize the security implications of this configuration, as it could potentially increase the risk if the user's session is compromised. Users should be encouraged to carefully consider their security requirements before enabling this feature.

Consider adding a note or warning that explicitly outlines the potential security risks associated with enabling global timestamps for sudo authentication. This will help users make an informed decision based on their security needs and the trustworthiness of their environment.

Comment on lines 37 to 53
>
: echo executable('sudo')
<
*suda* will ask for a password each time sudo is used for reading or writing.
However, you can set global timestamps in your sudoers configuration, if you
have sudo version 1.8.21 or higher. This will enable *suda* to reuse an
existing sudo authentication token. In this case, it will not ask for a
password if not needed. To enable, configure sudo with this option:
>
Defaults timestamp_type = global
<
The other types 'ppid', 'kernel' or 'tty' will not allow *suda* to use sudo
credential caching. Please make sure this is in line with your security
requirements.

=============================================================================
USAGE *suda-usage*
Copy link

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [29-29]

In the sentence "This plugin is strongly inspired by sudo.vim but the interfaces was aggressively modified for modern Vim script," consider adding a comma before "but" and correcting the verb agreement to "were" to improve readability and grammatical correctness.

- This plugin is strongly inspired by sudo.vim but the interfaces was aggressively modified for modern Vim script.
+ This plugin is strongly inspired by sudo.vim, but the interfaces were aggressively modified for modern Vim script.

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [102-102]

The verb "switch" does not agree with the subject in the sentence "When |g:suda_smart_edit| is true, suda automatically switch a buffer name when the target file is not readable or writable." Consider changing "switch" to "switches" for correct subject-verb agreement.

- When |g:suda_smart_edit| is true, suda automatically switch a buffer name when the target file is not readable or writable.
+ When |g:suda_smart_edit| is true, suda automatically switches a buffer name when the target file is not readable or writable.

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [192-192]

There's a typo in "openning" within the sentence "so you shouldn't feel any slowdown when openning buffers." It should be corrected to "opening" for proper spelling.

- so you shouldn't feel any slowdown when openning buffers.
+ so you shouldn't feel any slowdown when opening buffers.

@sstark sstark requested a review from lambdalisue March 10, 2024 08:48
Copy link
Owner

@lambdalisue lambdalisue left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks 🎉

@lambdalisue lambdalisue merged commit 257767d into lambdalisue:master Mar 10, 2024
@sstark sstark deleted the ask-for-pw-when-needed branch March 23, 2024 16:41
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.

None yet

2 participants