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

Add some directory manipulation nodes #2519

Closed
wants to merge 5 commits into from
Closed

Conversation

joeyballentine
Copy link
Member

Making this draft for feedback on these nodes. AFAIK, doing the type for the back directory node is impossible right now.

image

],
)
def into_directory_node(directory: Path, folder: str) -> Path:
return directory / folder
Copy link
Member

Choose a reason for hiding this comment

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

This has issues with path traversal. Example:

>>> Path("C:/foo/bar") / "/bar"
WindowsPath('C:/bar')
>>> Path("C:/foo/bar") / "D:/bar"
WindowsPath('D:/bar')
>>> Path("C:/foo/bar") / "../"
WindowsPath('C:/foo/bar/..') 
# ".." is not a valid dir name, so some libraries may resolve the path before. 
# This could lead to parts of chainner interpreting paths differently, which can lead to potential security vulnerabilities.
>>> (Path("C:/foo/bar") / "../").parent
WindowsPath('C:/foo/bar')

We need to define proper behavior for all these cases. The first question is whether we even want to allow .. and absolute paths.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe with my most recent push all of these are now consistent between the python value and navi type and work as you would expect.

Copy link
Member

Choose a reason for hiding this comment

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

... You solve one thing. Since you .resolve() now, it does the path traversal for ...

But my question was whether we should even allow that. Same for absolute paths.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think so, because it mirrors the behavior of both javascript and python for how they combine paths. Why would we make this different if there's a "standard" for resolving a valid path out of combining path strings like this?

Copy link
Member

Choose a reason for hiding this comment

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

Because they are joining paths. They don't call these functions "go into directory".

Copy link
Member

Choose a reason for hiding this comment

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

Sure. Then let's support ... But I don't see how joining with absolute paths would be useful.

Copy link
Member Author

Choose a reason for hiding this comment

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

But I don't see how joining with absolute paths would be useful.

These are not absolute paths

Copy link
Member

Choose a reason for hiding this comment

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

See the following examples:

>>> Path("C:/foo/bar") / "/bar"
WindowsPath('C:/bar')
>>> Path("C:/foo/bar") / "D:/bar"
WindowsPath('D:/bar')

The string value this node gets can very well represent an absolute path.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, then what do you suggest we do? restrict it to just single folder names?

Copy link
Member

Choose a reason for hiding this comment

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

restrict it to just single folder names?

We can't do that since nodes like Load Images can return multiple levels as their subdirectory path.

I guess we should just disallow absolute paths as the join argument. Unfortunately, python doesn't consider /bar an absolute path on windows. This is consistent with how windows itself treats paths, but it doesn't make sense for a cross-platform API. So we have to handle this ourselves.

@RunDevelopment
Copy link
Member

AFAIK, doing the type for the back directory node is impossible right now.

Just add an intrinsic function.

@joeyballentine
Copy link
Member Author

Question: do we need the back directory node if into directory supports ..? I think we could just get rid of back directory entirely and rename into directory to change directory

Copy link
Member

@RunDevelopment RunDevelopment left a comment

Choose a reason for hiding this comment

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

I think we could just get rid of back directory entirely and rename into directory to change directory

User experience. Normal people don't know that they can use .. in paths.


Also, about naming. I'm not sure whether "back" and "into" are the terms to communicate the operations. I dislike that there is no symmetry between "back" and "into" in their normal usage. "Back" also has the issue that is term is typically used in browsers and file explorers to refer to the history of the current tab/window.

The programming terms are "parent" and "join", but I don't think that we should necessarily copy that (UX again).

I was thinking that we could use the terms "up" and "down", but I'm not sure how to integrate that into the node names...

Comment on lines +32 to +35
if create_path and not resolved.exists():
resolved.mkdir(parents=True)
elif not resolved.exists():
raise FileNotFoundError(f"Directory does not exist: {resolved}")
Copy link
Member

Choose a reason for hiding this comment

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

So the directory must always exist?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Do you not think so? I don't think we should be passing in invalid directories into nodes willy nilly

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I don't like it.

  1. This means that the correctness of a chain now depends on the file system. If user A has folder foo but user B doesn't, it's not gonna work for user B unless they check that box. Depending on the file system is unavoidable for node like Load Image (singular), but it can be avoided here.
  2. This leads to errors being ignored. Nodes like Load Images don't allow non-existent directories as input for obvious reasons. So if you always just create those directories, then Load Images can't error for incorrect chains.
  3. We already allow passing non-existent directories. Save Image and the Directory node support it.
  4. Side effects. You turned this node into a side effect node.

@RunDevelopment
Copy link
Member

Also, we have to handle invalid characters ourselves. Basically, names can't contain <>:"|?* and non-printable ASCII characters (0-31). Yes, linux allows more, but it's better to be more restrictive for this kind of stuff.

https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names

@joeyballentine
Copy link
Member Author

Also, we have to handle invalid characters ourselves

what would be your recommended approach for that?

@RunDevelopment
Copy link
Member

Idk. Use a regex or define a set of characters and check each one. Doesn't really matter.

This was referenced Mar 20, 2024
@RunDevelopment RunDevelopment deleted the path-nodes branch March 21, 2024 21:36
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