Modify Post read_file to check if file is readable #11342
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This changes the behavior for Post
read_file
on command shell sessions to first check if a file is readable before attempting read.This fixes #9854 by ensuring that
nil
is returned in the event a file is not readable.Prior to this PR, invoking
read_file
on a command shell session, for a file which was not readable (didn't exist or insufficient permissions) would sometimes return an error message from the system (which could vary in value based on system locale) depending on whetherstderr
was routed over the socket.Advantages
It ensures consistency.
read_file
on command shell sessions will now always returnnil
if the file cannot be read, regardless of session type or remote system environment.It should be stealthier in logs, in that it won't trigger permission denied errors.
Disadvantages
It adds an extra
cmd_exec
call for every file read, and requirestest
to be in$PATH
.TOCTOU. If this is a concern, instead of executing the
test
in a separatecmd_exec
call,test -r /path/to/file &&
could be prepended in a singlecmd_exec
call.Command Shell Session
Test Code