Skip to content

Commit 8461abd

Browse files
fix: reject non-YAML extensions for --sqlconfig flag
1 parent a183f6c commit 8461abd

3 files changed

Lines changed: 53 additions & 7 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ sqlcmd config connection-strings
8484
sqlcmd config view
8585
```
8686

87+
#### Custom configuration files
88+
89+
The `--sqlconfig` flag specifies a custom configuration file. The file must be YAML. Extensionless filenames are allowed; if the file has an extension, it must be `.yaml` or `.yml`:
90+
91+
```
92+
sqlcmd config --sqlconfig ./myproject.yaml add-endpoint --name ep1434 --address localhost --port 1434
93+
sqlcmd config --sqlconfig ./myproject.yaml view
94+
```
95+
8796
### Versions
8897

8998
To see all version tags to choose from (2017, 2019, 2022 etc.), and install a specific version, run:

internal/config/config.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
package config
55

66
import (
7+
"fmt"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
"testing"
12+
713
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
814
"github.com/microsoft/go-sqlcmd/internal/io/file"
915
"github.com/microsoft/go-sqlcmd/internal/io/folder"
1016
"github.com/microsoft/go-sqlcmd/internal/pal"
11-
"os"
12-
"path/filepath"
13-
"testing"
1417
)
1518

1619
var config Sqlconfig
@@ -24,6 +27,11 @@ func SetFileName(name string) {
2427
panic("name is empty")
2528
}
2629

30+
if ext := strings.ToLower(filepath.Ext(name)); ext != "" && ext != ".yaml" && ext != ".yml" {
31+
checkErr(fmt.Errorf("configuration file %q has unsupported extension %q (must be .yaml or .yml)", name, ext))
32+
return
33+
}
34+
2735
filename = name
2836

2937
file.CreateEmptyIfNotExists(filename)

internal/config/config_test.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
package config
55

66
import (
7+
"os"
8+
"path/filepath"
9+
"reflect"
10+
"strings"
11+
"testing"
12+
713
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
814
"github.com/microsoft/go-sqlcmd/internal/output"
915
"github.com/microsoft/go-sqlcmd/internal/pal"
1016
"github.com/microsoft/go-sqlcmd/internal/secret"
1117
"github.com/stretchr/testify/assert"
12-
"os"
13-
"reflect"
14-
"strings"
15-
"testing"
18+
"github.com/stretchr/testify/require"
1619
)
1720

1821
func TestConfig(t *testing.T) {
@@ -380,6 +383,32 @@ func TestConfig_GetCurrentContextEndPointNotFoundPanic(t *testing.T) {
380383
})
381384
}
382385

386+
func TestSetFileName_RejectsNonYAMLExtension(t *testing.T) {
387+
var gotErr error
388+
originalCallback := errorCallback
389+
errorCallback = func(err error) { gotErr = err }
390+
t.Cleanup(func() { errorCallback = originalCallback })
391+
392+
SetFileName("config.json")
393+
require.Error(t, gotErr)
394+
assert.Contains(t, gotErr.Error(), ".json")
395+
396+
gotErr = nil
397+
SetFileName("config.toml")
398+
require.Error(t, gotErr)
399+
assert.Contains(t, gotErr.Error(), ".toml")
400+
401+
tempDir := t.TempDir()
402+
403+
gotErr = nil
404+
SetFileName(filepath.Join(tempDir, t.Name()+".yaml"))
405+
assert.NoError(t, gotErr)
406+
407+
gotErr = nil
408+
SetFileName(filepath.Join(tempDir, t.Name()+".yml"))
409+
assert.NoError(t, gotErr)
410+
}
411+
383412
func TestConfig_DeleteContextThatDoesNotExist(t *testing.T) {
384413
assert.Panics(t, func() {
385414
contextOrdinal("does-not-exist")

0 commit comments

Comments
 (0)