Skip to content

Web UI: More file path sanitation, better network bridge warnings, each endpoint return one message#932

Merged
rdmark merged 17 commits intodevelopfrom
rdmark-python-even-more-refactoring
Oct 24, 2022
Merged

Web UI: More file path sanitation, better network bridge warnings, each endpoint return one message#932
rdmark merged 17 commits intodevelopfrom
rdmark-python-even-more-refactoring

Conversation

@rdmark
Copy link
Copy Markdown
Member

@rdmark rdmark commented Oct 22, 2022

  • Sanitize file paths with Path: for flat file structures, always extract Path().name, and for nested file structures either look for absolute paths, or someone trying to use ".." to traverse the dir strucutre.
  • Reduce redundancy in network bridge detection method, and return somewhat more informative messages
  • Make all endpoints return exactly one message
  • Move some warning messages to logging
  • Use tempfile for iso generation temp file handling

@rdmark rdmark requested a review from nucleogenic October 22, 2022 16:27
@rdmark rdmark requested a review from nucleogenic October 23, 2022 05:34
@rdmark rdmark marked this pull request as ready for review October 23, 2022 05:37
@rdmark rdmark changed the title Even more Python refactoring Web UI: More file path sanitation, better network bridge warnings, each endpoint return one message Oct 23, 2022
Copy link
Copy Markdown
Member

@nucleogenic nucleogenic left a comment

Choose a reason for hiding this comment

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

Lots of good stuff in here! Added a few thoughts.

Comment on lines +443 to +442
file_name = request.form.get("file_name")
file_name = Path(request.form.get("file_name"))
if file_name.is_absolute() or ".." in str(file_name):
file_name = file_name.name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think there are two considerations here:

  1. DRY principle, should this be extracted to a function? (6 uses in web.py)

  2. What should that function actually do? Upon reflection, it seems a bit unpredictable to have the side effect of changing an invalid path (e.g. /mac/foo.hds becomes foo.hds, losing the dir) to a valid, but different path. Perhaps, if validation fails, we should return an error response instead, so the client can correct the error?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

+1 on #1. I felt dirty copy pasting the code snippet. I added a utility method is_safe_path() to web_utils.py

As for point #2, the above utility method now returns a message an error that will display the path and a notice that it is invalid. Beyond that, do you have ideas on a more sophisticated way to detect unsafe paths, and what to do with them?

os.mkdir(tmp_dir)
tmp_full_path = tmp_dir + file_name
iso_filename = f"{server_info['image_dir']}/{file_name}.iso"
tmp_dir = Path("/tmp") / str(tmp_ts)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

BTW, I just realised, you could use TemporaryDirectory context manager here to handle the temporary dir generation and cleanup.

from tempfile import TemporaryDirectory
with TemporaryDirectory() as tmp_dir:
    do stuff

There's an example of this in unarchiver.py.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wow, that was so easy get up and running! I've had this on my todo list for a year. See #543

@sonarqubecloud
Copy link
Copy Markdown

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@rdmark rdmark merged commit 5172d16 into develop Oct 24, 2022
@rdmark rdmark deleted the rdmark-python-even-more-refactoring branch October 24, 2022 02:05
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.

2 participants