Skip to content

Commit

Permalink
[2.5] Consolidate text file name validation (#2632)
Browse files Browse the repository at this point in the history
* test pr

* fix formatting issues

---------

Co-authored-by: Yuan-Ting Hsieh (謝沅廷) <[email protected]>
  • Loading branch information
yanchengnv and YuanTingHsieh authored Jun 13, 2024
1 parent 67dc2c5 commit 9c9298e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
11 changes: 4 additions & 7 deletions nvflare/fuel/hci/client/fl_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from nvflare.fuel.hci.client.api_status import APIStatus
from nvflare.fuel.hci.client.fl_admin_api_constants import FLDetailKey
from nvflare.fuel.hci.client.fl_admin_api_spec import APISyntaxError, FLAdminAPIResponse, FLAdminAPISpec, TargetType
from nvflare.fuel.hci.cmd_arg_utils import get_file_extension
from nvflare.fuel.hci.cmd_arg_utils import validate_text_file_name
from nvflare.security.logging import secure_format_exception

from .overseer_service_finder import ServiceFinderByOverseer
Expand Down Expand Up @@ -211,12 +211,9 @@ def _validate_file_string(self, file: str) -> str:
if p == "..":
raise APISyntaxError(".. in file path is not allowed")

file_extension = get_file_extension(file)
if file_extension not in [".txt", ".log", ".json", ".csv", ".sh", ".config", ".py"]:
raise APISyntaxError(
"this command cannot be applied to file {}. Only files with the following extensions are "
"permitted: .txt, .log, .json, .csv, .sh, .config, .py".format(file)
)
err = validate_text_file_name(file)
if err:
raise APISyntaxError(err)
return file

def _validate_sp_string(self, sp_string) -> str:
Expand Down
28 changes: 22 additions & 6 deletions nvflare/fuel/hci/cmd_arg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,31 @@ def get_file_extension(file: str) -> str:
return ex


def validate_text_file_name(file_name: str) -> str:
"""Check the specified file name whether it is acceptable.
Args:
file_name: file name to be checked.
Returns: error string if invalid; or empty string if valid
"""
file_extension = get_file_extension(file_name)
if file_extension not in [".txt", ".log", ".json", ".csv", ".sh", ".config", ".py"]:
return (
f"this command cannot be applied to file {file_name}. Only files with the following extensions are "
"permitted: .txt, .log, .json, .csv, .sh, .config, .py"
)
else:
return ""


def validate_file_string(file: str) -> str:
"""Returns the file string if it is valid."""
validate_path_string(file)
file_extension = get_file_extension(file)
if file_extension not in [".txt", ".log", ".json", ".csv", ".sh", ".config", ".py"]:
raise SyntaxError(
"this command cannot be applied to file {}. Only files with the following extensions are "
"permitted: .txt, .log, .json, .csv, .sh, .config, .py".format(file)
)
err = validate_text_file_name(file)
if err:
raise SyntaxError(err)
return file


Expand Down
13 changes: 4 additions & 9 deletions nvflare/private/fed/server/shell_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import subprocess
from typing import List

from nvflare.fuel.hci.cmd_arg_utils import get_file_extension, join_args
from nvflare.fuel.hci.cmd_arg_utils import join_args, validate_text_file_name
from nvflare.fuel.hci.conn import Connection
from nvflare.fuel.hci.proto import MetaStatusValue, make_meta
from nvflare.fuel.hci.reg import CommandModule, CommandModuleSpec, CommandSpec
Expand Down Expand Up @@ -185,14 +185,9 @@ def validate_shell_command(self, args: List[str], parse_result):
return ".. in path name is not allowed"

if self.text_file_only:
# check whether the file name is ended with numbers. If so, the actual file extension is before it.
# this is the case that when the log file (log.txt) is rotated, the previous file becomes log.txt.1.
file_extension = get_file_extension(f)
if file_extension not in [".txt", ".log", ".json", ".csv", ".sh", ".config", ".py"]:
return (
"this command cannot be applied to file {}. Only files with the following extensions "
"are permitted: .txt, .log, .json, .csv, .sh, .config, .py".format(f)
)
err = validate_text_file_name(f)
if err:
return err

return ""

Expand Down

0 comments on commit 9c9298e

Please sign in to comment.