MCPcopy Create free account
hub / github.com/cli/cli / Join

Method Join

internal/safepaths/absolute.go:38–57  ·  view source on GitHub ↗

Join an absolute path with elements to create a new Absolute path, or error. A PathTraversalError will be returned if the joined path would traverse outside of the base Absolute path. Note that this does not handle symlinks.

(elem ...string)

Source from the content-addressed store, hash-verified

36// A PathTraversalError will be returned if the joined path would traverse outside of
37// the base Absolute path. Note that this does not handle symlinks.
38func (a Absolute) Join(elem ...string) (Absolute, error) {
39 joinedAbsolutePath, err := ParseAbsolute(filepath.Join(append([]string{a.path}, elem...)...))
40 if err != nil {
41 return Absolute{}, fmt.Errorf("failed to parse joined path: %w", err)
42 }
43
44 isSubpath, err := joinedAbsolutePath.isSubpathOf(a)
45 if err != nil {
46 return Absolute{}, err
47 }
48
49 if !isSubpath {
50 return Absolute{}, PathTraversalError{
51 Base: a,
52 Elems: elem,
53 }
54 }
55
56 return joinedAbsolutePath, nil
57}
58
59func (a Absolute) isSubpathOf(dir Absolute) (bool, error) {
60 relativePath, err := filepath.Rel(dir.path, a.path)

Callers 15

mainFunction · 0.80
normalizeBuildTagsFunction · 0.80
shellInspectFunction · 0.80
parseExportArgsFunction · 0.80
writeExportDirFunction · 0.80
parseWithNameFunction · 0.80
describeResponseFunction · 0.80
writeZipFunction · 0.80
writeExportArchiveFunction · 0.80
writeTarGzipFunction · 0.80
snapshotConfigMethod · 0.80
copyConfigTreeFunction · 0.80

Calls 3

ParseAbsoluteFunction · 0.85
isSubpathOfMethod · 0.80
ErrorfMethod · 0.65

Tested by 15

TestCheckRequestFunction · 0.64
TestCmdSetFunction · 0.64
TestCmdRepoFunction · 0.64
TestCmdWriteFunction · 0.64
TestCmdGitFunction · 0.64
gitForTestFunction · 0.64
TestCmdExportFunction · 0.64
extractTestArchiveFunction · 0.64
useCacheDirFunction · 0.64
readTestdataFunction · 0.64
TestSchemaSourceLocateFunction · 0.64
TestCmdSchemasFunction · 0.64