oci_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "path/filepath"
  16. "testing"
  17. )
  18. func TestValidatePathInBase(t *testing.T) {
  19. base := "/tmp/cache/compose"
  20. tests := []struct {
  21. name string
  22. unsafePath string
  23. wantErr bool
  24. }{
  25. {
  26. name: "valid simple filename",
  27. unsafePath: "compose.yaml",
  28. wantErr: false,
  29. },
  30. {
  31. name: "valid hashed filename",
  32. unsafePath: "f8f9ede3d201ec37d5a5e3a77bbadab79af26035e53135e19571f50d541d390c.yaml",
  33. wantErr: false,
  34. },
  35. {
  36. name: "valid env file",
  37. unsafePath: ".env",
  38. wantErr: false,
  39. },
  40. {
  41. name: "valid env file with suffix",
  42. unsafePath: ".env.prod",
  43. wantErr: false,
  44. },
  45. {
  46. name: "unix path traversal",
  47. unsafePath: "../../../etc/passwd",
  48. wantErr: true,
  49. },
  50. {
  51. name: "windows path traversal",
  52. unsafePath: "..\\..\\..\\windows\\system32\\config\\sam",
  53. wantErr: true,
  54. },
  55. {
  56. name: "subdirectory unix",
  57. unsafePath: "config/base.yaml",
  58. wantErr: true,
  59. },
  60. {
  61. name: "subdirectory windows",
  62. unsafePath: "config\\base.yaml",
  63. wantErr: true,
  64. },
  65. {
  66. name: "absolute unix path",
  67. unsafePath: "/etc/passwd",
  68. wantErr: true,
  69. },
  70. {
  71. name: "absolute windows path",
  72. unsafePath: "C:\\windows\\system32\\config\\sam",
  73. wantErr: true,
  74. },
  75. {
  76. name: "parent reference only",
  77. unsafePath: "..",
  78. wantErr: true,
  79. },
  80. {
  81. name: "current directory reference",
  82. unsafePath: "./file.yaml",
  83. wantErr: false, // ./ resolves to base dir
  84. },
  85. {
  86. name: "mixed separators",
  87. unsafePath: "config/sub\\file.yaml",
  88. wantErr: true,
  89. },
  90. {
  91. name: "filename with spaces",
  92. unsafePath: "my file.yaml",
  93. wantErr: false,
  94. },
  95. {
  96. name: "filename with special chars",
  97. unsafePath: "file-name_v1.2.3.yaml",
  98. wantErr: false,
  99. },
  100. {
  101. name: "single parent then back",
  102. unsafePath: "../compose/file.yaml",
  103. wantErr: false, // Resolves back to base dir, which is fine
  104. },
  105. }
  106. for _, tt := range tests {
  107. t.Run(tt.name, func(t *testing.T) {
  108. err := validatePathInBase(base, tt.unsafePath)
  109. if (err != nil) != tt.wantErr {
  110. targetPath := filepath.Join(base, tt.unsafePath)
  111. targetDir := filepath.Dir(targetPath)
  112. t.Errorf("validatePathInBase(%q, %q) error = %v, wantErr %v\ntargetDir=%q base=%q",
  113. base, tt.unsafePath, err, tt.wantErr, targetDir, base)
  114. }
  115. })
  116. }
  117. }