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

implement generic file accessor class #870

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft

Conversation

M4LuZ
Copy link
Collaborator

@M4LuZ M4LuZ commented Jan 11, 2024

What is this PR doing?

WIP Implementation of a secure file accessor that makes working with files and folders easier while simultaneously preventing directory traversal both below LS root and any set intermediate path.
All current file accesses to be replaced with this

Which issue(s) this PR fixes:

no issue created
refs #710

Checklist

  • CHANGELOG.md entry
  • Documentation update

@M4LuZ M4LuZ added new feature WIP Indicates that somebody is working on this issue security issue Issues with relation to or with impact on the system security php labels Jan 11, 2024
@M4LuZ M4LuZ changed the title First draft of generic file accessor class implement generic file accessor class Jan 11, 2024
@M4LuZ
Copy link
Collaborator Author

M4LuZ commented Jan 13, 2024

@andygrunwald : Would appreciate your feedback on the general design and how that works based on the example file changed

Copy link
Collaborator

@andygrunwald andygrunwald left a comment

Choose a reason for hiding this comment

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

I am not 100% sure about the design of the class. Or lets say, purpose of the class.

Is the main purpose to represent one single file? Or is the purpose to enable Lansuite to initiate a File() instance once and handle multiple files?

From the name of the class, my first thought would be to handle one single file. If we loop over multiple files, for each file, a File() instance would be created.

In this case, I suggest

  • to add the file path for the constructor as a first parameter
  • to remove the argument from functions like getFullPath and exists, because the class only handles one file

In this case, it would also make sense to make a "delete" and "output" method.

LanSuite can still provide a helper function like "NewFile()" which sets the filesystem injection and root path.

Overall, I think we should do this, because we have multiple places where we deal with files.

If you think this is useful, I can also iterate over it a bit if you want :)

inc/Classes/File.php Outdated Show resolved Hide resolved
inc/Classes/File.php Outdated Show resolved Hide resolved
inc/Classes/File.php Outdated Show resolved Hide resolved
inc/Classes/File.php Outdated Show resolved Hide resolved
inc/Classes/File.php Outdated Show resolved Hide resolved
inc/Classes/File.php Outdated Show resolved Hide resolved
inc/Classes/File.php Outdated Show resolved Hide resolved
@M4LuZ
Copy link
Collaborator Author

M4LuZ commented Jan 14, 2024

HI @andygrunwald ,

thanks for the helfpul feedback.

Basically this was inteded as helper class for all file access operations.
Eplicitly not on a per-file basis, but more as per-file-access-context.
So that it can be used to define a subpart of the filesystem as area where access can/will hapen, potentially providing also further scoping options (e.g. filename filtering, extension / MIME-type filtering, both with a default set and the option to set additionals per object instance).

I am pondering whether addidng direct access methods like outputFileContent makes sense to it.
The intent was to ensure with that that the file path is OK before the file is read, thus that method .
This could be potentially be moved to a separate class for direct file operations?

I was close to naming the Class FileAccessor, based on your comments I think that helps to understand purpose and usage a lot better.

@andygrunwald
Copy link
Collaborator

What do you think about a pure Object Oriented approach here?
Creating two classes:

  • File
  • FileCollection

File

It is a single file and offers functions on a single file. Things like

  • Exists
  • Output
  • Rename
  • Delete
  • ...

FileCollection

Represents a list/number of files. Can be initialized via

  • An array of files
  • By a folder

Internally, it has multiple File() objects.

Why?

This way we would have a clean and unit testable result. Responsibilities are clear and the security aspects (which triggered the PR) can also be taken into account.
The FileCollection can be used in modules like gallery, downloads, etc.

Thoughts?

removed unnecessary if-case

Co-authored-by: Andy Grunwald <andygrunwald@gmail.com>
@M4LuZ
Copy link
Collaborator Author

M4LuZ commented Jan 14, 2024

Thoughts?

#worksforme ;)

Was mentally already halfway there, your idea of using an array of files for initialisation for FileCollection makes a ton of sense.
For most of the proposed File class methods I would use functions that Symphony/Filesystem already provides, either as internal calls or maybe even by inheriting them as a child class?

@andygrunwald
Copy link
Collaborator

For most of the proposed File class methods I would use functions that Symphony/Filesystem already provides

This is true. Still, I would shim them away from the actual implementation and create a LanSuite File class as a Wrapper. This way we can exchange the lib as we want, without doing a big rewrite.

Copy link

sonarcloud bot commented Feb 12, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot
3.8% Duplication on New Code (required ≤ 3%)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

idea Catch issues before they fail your Quality Gate with our IDE extension SonarLint SonarLint

@andygrunwald andygrunwald removed the php label Mar 27, 2024
Start of replacement of file upload for picgallery + direct implementation of multifile upload
to fix #1074
Copy link

sonarcloud bot commented Apr 4, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot
3.7% Duplication on New Code (required ≤ 3%)
E Security Rating on New Code (required ≥ A)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conflicts new feature security issue Issues with relation to or with impact on the system security WIP Indicates that somebody is working on this issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants