| 1 |
// SPDX-License-Identifier: BSD-3-Clause |
| 2 |
|
| 3 |
// Copyright (C) 2017-2025 SUSE LLC. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package securejoin |
| 8 |
|
| 9 |
import ( |
| 10 |
"path/filepath" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
) |
| 15 |
|
| 16 |
// Windows has very specific behaviour relating to volumes, and we can only |
| 17 |
// test it on Windows machines because filepath.* behaviour depends on GOOS. |
| 18 |
// |
| 19 |
// See <https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats> |
| 20 |
// for more information about the various path formats we need to make sure are |
| 21 |
// correctly handled. |
| 22 |
func TestHasDotDot_WindowsVolumes(t *testing.T) { |
| 23 |
for _, test := range []struct { |
| 24 |
testName, path string |
| 25 |
expected bool |
| 26 |
}{ |
| 27 |
{"plain-dotdot", `C:..`, true}, // apparently legal |
| 28 |
{"relative-dotdot", `C:..\foo\bar`, true}, // apparently legal |
| 29 |
{"trailing-dotdot", `D:\foo\bar\..`, true}, |
| 30 |
{"leading-dotdot", `F:\..\foo\bar`, true}, |
| 31 |
{"middle-dotdot", `F:\foo\..\bar`, true}, |
| 32 |
{"drive-like-path", `\foo\C:..\bar`, false}, // C:.. is a filename here |
| 33 |
{"unc-dotdot", `\\gondor\share\call\for\aid\..\help`, true}, |
| 34 |
{"dos-dotpath-dotdot1", `\\.\C:\..\foo\bar`, true}, |
| 35 |
{"dos-dotpath-dotdot2", `\\.\C:\foo\..\bar`, true}, |
| 36 |
{"dos-questionpath-dotdot1", `\\?\C:\..\foo\bar`, true}, |
| 37 |
{"dos-questionpath-dotdot2", `\\?\C:\foo\..\bar`, true}, |
| 38 |
} { |
| 39 |
test := test // copy iterator |
| 40 |
t.Run(test.testName, func(t *testing.T) { |
| 41 |
got := hasDotDot(test.path) |
| 42 |
assert.Equalf(t, test.expected, got, "unexpected result for hasDotDot(`%s`) (VolumeName: %q)", test.path, filepath.VolumeName(test.path)) |
| 43 |
}) |
| 44 |
} |
| 45 |
} |