Skip to content

Test parent directory of .netrc file is writable before using it #4075

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 2 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,13 @@ public class SwiftTool {
// User didn't tell us to use these .netrc files so be more lenient with errors
func loadNetrcNoThrows(at path: AbsolutePath) -> NetrcAuthorizationProvider? {
guard localFileSystem.exists(path) else { return nil }

do {
try withTemporaryFile(dir: path.parentDirectory) { _ in }
} catch {
self.observabilityScope.emit(warning: "\(path.parentDirectory) is not accessible or not writable, not using .netrc file in it: \(error)")
return nil
}

do {
return try NetrcAuthorizationProvider(path: path, fileSystem: localFileSystem)
Expand Down
15 changes: 9 additions & 6 deletions Sources/Workspace/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4085,9 +4085,10 @@ extension Workspace.Location {

// check that shared configuration directory is accessible, or warn + reset if not
if let sharedConfigurationDirectory = self.sharedConfigurationDirectory {
// it may not always be possible to create default location (for example de to restricted sandbox)
// It may not always be possible to create default location (for example de to restricted sandbox),
// in which case defaultDirectory would be nil.
let defaultDirectory = try? fileSystem.getOrCreateSwiftPMConfigurationDirectory(warningHandler: warningHandler)
if sharedConfigurationDirectory != defaultDirectory {
if defaultDirectory != nil, sharedConfigurationDirectory != defaultDirectory {
// custom location must be writable, throw if we cannot access it
try withTemporaryFile(dir: sharedConfigurationDirectory) { _ in }
} else {
Expand All @@ -4103,9 +4104,10 @@ extension Workspace.Location {

// check that shared configuration directory is accessible, or warn + reset if not
if let sharedSecurityDirectory = self.sharedSecurityDirectory {
// it may not always be possible to create default location (for example de to restricted sandbox)
// It may not always be possible to create default location (for example de to restricted sandbox),
// in which case defaultDirectory would be nil.
let defaultDirectory = try? fileSystem.getOrCreateSwiftPMSecurityDirectory()
if sharedSecurityDirectory != defaultDirectory {
if defaultDirectory != nil, sharedSecurityDirectory != defaultDirectory {
// custom location must be writable, throw if we cannot access it
try withTemporaryFile(dir: sharedSecurityDirectory) { _ in }
} else {
Expand All @@ -4121,9 +4123,10 @@ extension Workspace.Location {

// check that shared configuration directory is accessible, or warn + reset if not
if let sharedCacheDirectory = self.sharedCacheDirectory {
// it may not always be possible to create default location (for example de to restricted sandbox)
// It may not always be possible to create default location (for example de to restricted sandbox),
// in which case defaultDirectory would be nil.
let defaultDirectory = try? fileSystem.getOrCreateSwiftPMCacheDirectory()
if sharedCacheDirectory != defaultDirectory {
if defaultDirectory != nil, sharedCacheDirectory != defaultDirectory {
// custom location must be writable, throw if we cannot access it
try withTemporaryFile(dir: sharedCacheDirectory) { _ in }
} else {
Expand Down