Skip to content

Home directory for non-existent user should not fall back to /var/empty or %ALLUSERSPROFILE% #1072

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

Merged
merged 1 commit into from
Dec 6, 2024

Conversation

jmschonfeld
Copy link
Contributor

When porting FileManager to swift-foundation, I made the mistake of changing the underlying home directory lookup behavior for non-existent users to fallback to /var/empty. I also synced this behavior to Windows where it now falls back to %ALLUSERSPROFILE% and updated unit tests to match this behavior. However, this behavior is incorrect. Falling back to /var/empty or %ALLUSERSPROFILE% is an alright choice for the current user, but for an arbitrary other user, we should instead return nil as these APIs return optional values. This PR splits the user home directory implementation into two: one for the current user that falls back to /var/empty/%ALLUSERSPROFILE% and one for a specific user that returns nil on failure.

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@kperryua kperryua left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

There seems to be a bit of bleeding whitespace that this change is introducing. It seems mostly like an extraction of the windows logic into a helper function which is adding a fair amount of contextual change.

internal static func homeDirectoryPath(forUser user: String) -> String? {
#if os(Windows)
guard !user.isEmpty else {
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we return nil for empty user on non-Windows platforms, too?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see that Platform.homeDirectory(forUserName: "") returns nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah we have some unit tests that check for this behavior that aren't platform specific so in the end I think the behavior matches, but happy to restructure if you think this should be moved out of the #if block

}
#else
// First check CFFIXED_USER_HOME if the environment is not considered tainted
if let envVar = Platform.getEnvSecure("CFFIXED_USER_HOME") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed this. The behavior appears consistent with CFCopyHomeDirectoryURLForUser, but I'm not sure it's correct. 😓 An issue for a different day, perhaps.

Copy link
Contributor Author

@jmschonfeld jmschonfeld Dec 5, 2024

Choose a reason for hiding this comment

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

Yeah it did seem a bit weird that CFFIXED_USER_HOME affects all users and not just the current user, but I agree probably an issue to address separately to minimize the behavioral changes of this patch (this was sort of easily overlooked before but is now very clear since the implementations for current vs. specific user are split)

@jmschonfeld
Copy link
Contributor Author

There seems to be a bit of bleeding whitespace that this change is introducing. It seems mostly like an extraction of the windows logic into a helper function which is adding a fair amount of contextual change.

Somewhat yes. The windows logic was previously

if let user {
    // Other user specific logic
}

// Current user logic

I split this into two separate functions (one where user is non-optional and one where there is no user variable) so you're likely seeing the indentation change of the user-specific logic moving from an if let user block to just the top level of a function which more closely matches the structure of the non-Windows implementation. I can see if there's a way to break it up into two commits in case that's helpful but not sure how successful that'd be - it might be best to view this diff in the Split View rather than the unified view.

@jmschonfeld jmschonfeld merged commit 2eeb1b0 into swiftlang:main Dec 6, 2024
3 checks passed
@jmschonfeld jmschonfeld deleted the nonexistent-user-home-dir branch December 6, 2024 23:51
jmschonfeld added a commit to jmschonfeld/swift-foundation that referenced this pull request Dec 6, 2024
jmschonfeld added a commit that referenced this pull request Dec 12, 2024
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.

4 participants