git_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package remote
  14. import (
  15. "testing"
  16. "gotest.tools/v3/assert"
  17. )
  18. func TestValidateGitSubDir(t *testing.T) {
  19. base := "/tmp/cache/compose/abc123def456"
  20. tests := []struct {
  21. name string
  22. subDir string
  23. wantErr bool
  24. }{
  25. {
  26. name: "valid simple directory",
  27. subDir: "examples",
  28. wantErr: false,
  29. },
  30. {
  31. name: "valid nested directory",
  32. subDir: "examples/nginx",
  33. wantErr: false,
  34. },
  35. {
  36. name: "valid deeply nested directory",
  37. subDir: "examples/web/frontend/config",
  38. wantErr: false,
  39. },
  40. {
  41. name: "valid current directory",
  42. subDir: ".",
  43. wantErr: false,
  44. },
  45. {
  46. name: "valid directory with redundant separators",
  47. subDir: "examples//nginx",
  48. wantErr: false,
  49. },
  50. {
  51. name: "valid directory with dots in name",
  52. subDir: "examples/nginx.conf.d",
  53. wantErr: false,
  54. },
  55. {
  56. name: "path traversal - parent directory",
  57. subDir: "..",
  58. wantErr: true,
  59. },
  60. {
  61. name: "path traversal - multiple parent directories",
  62. subDir: "../../../etc/passwd",
  63. wantErr: true,
  64. },
  65. {
  66. name: "path traversal - deeply nested escape",
  67. subDir: "../../../../../../../tmp/pwned",
  68. wantErr: true,
  69. },
  70. {
  71. name: "path traversal - mixed with valid path",
  72. subDir: "examples/../../etc/passwd",
  73. wantErr: true,
  74. },
  75. {
  76. name: "path traversal - at the end",
  77. subDir: "examples/..",
  78. wantErr: false, // This resolves to "." which is the current directory, safe
  79. },
  80. {
  81. name: "path traversal - in the middle",
  82. subDir: "examples/../../../etc/passwd",
  83. wantErr: true,
  84. },
  85. {
  86. name: "path traversal - windows style",
  87. subDir: "..\\..\\..\\windows\\system32",
  88. wantErr: true,
  89. },
  90. {
  91. name: "absolute unix path",
  92. subDir: "/etc/passwd",
  93. wantErr: true,
  94. },
  95. {
  96. name: "absolute windows path",
  97. subDir: "C:\\windows\\system32\\config\\sam",
  98. wantErr: true,
  99. },
  100. {
  101. name: "absolute path with home directory",
  102. subDir: "/home/user/.ssh/id_rsa",
  103. wantErr: true,
  104. },
  105. {
  106. name: "normalized path that would escape",
  107. subDir: "./../../etc/passwd",
  108. wantErr: true,
  109. },
  110. {
  111. name: "directory name with three dots",
  112. subDir: ".../config",
  113. wantErr: false,
  114. },
  115. {
  116. name: "directory name with four dots",
  117. subDir: "..../config",
  118. wantErr: false,
  119. },
  120. {
  121. name: "directory name with five dots",
  122. subDir: "...../etc/passwd",
  123. wantErr: false, // ".....'' is a valid directory name, not path traversal
  124. },
  125. {
  126. name: "directory name starting with two dots and letter",
  127. subDir: "..foo/bar",
  128. wantErr: false,
  129. },
  130. }
  131. for _, tt := range tests {
  132. t.Run(tt.name, func(t *testing.T) {
  133. err := validateGitSubDir(base, tt.subDir)
  134. if (err != nil) != tt.wantErr {
  135. t.Errorf("validateGitSubDir(%q, %q) error = %v, wantErr %v",
  136. base, tt.subDir, err, tt.wantErr)
  137. }
  138. })
  139. }
  140. }
  141. // TestValidateGitSubDirSecurityScenarios tests specific security scenarios
  142. func TestValidateGitSubDirSecurityScenarios(t *testing.T) {
  143. base := "/var/cache/docker-compose/git/1234567890abcdef"
  144. // Test the exact vulnerability scenario from the issue
  145. t.Run("CVE scenario - /tmp traversal", func(t *testing.T) {
  146. maliciousPath := "../../../../../../../tmp/pwned"
  147. err := validateGitSubDir(base, maliciousPath)
  148. assert.ErrorContains(t, err, "path traversal")
  149. })
  150. // Test variations of the attack
  151. t.Run("CVE scenario - /etc traversal", func(t *testing.T) {
  152. maliciousPath := "../../../../../../../../etc/passwd"
  153. err := validateGitSubDir(base, maliciousPath)
  154. assert.ErrorContains(t, err, "path traversal")
  155. })
  156. // Test that legitimate nested paths still work
  157. t.Run("legitimate nested path", func(t *testing.T) {
  158. validPath := "examples/docker-compose/nginx/config"
  159. err := validateGitSubDir(base, validPath)
  160. assert.NilError(t, err)
  161. })
  162. }