Skip to content

Commit

Permalink
Merge pull request #44777 from thaJeztah/23.0_backport_ignore_bom
Browse files Browse the repository at this point in the history
[23.0 backport] daemon/config: ignore UTF-8 BOM in config JSON
  • Loading branch information
neersighted committed Jan 10, 2023
2 parents 5830188 + 101bd10 commit d29ab75
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 8 additions & 4 deletions daemon/config/config.go
Expand Up @@ -424,12 +424,16 @@ func getConflictFreeConfiguration(configFile string, flags *pflag.FlagSet) (*Con
return nil, err
}

var config Config

// Strip the UTF-8 BOM if present ([RFC 8259] allows JSON implementations to optionally strip the BOM for
// interoperability; do so here as Notepad on older versions of Windows Server insists on a BOM).
// [RFC 8259]: https://tools.ietf.org/html/rfc8259#section-8.1
b = bytes.TrimPrefix(b, []byte("\xef\xbb\xbf"))
// Trim whitespace so that an empty config can be detected for an early return.
b = bytes.TrimSpace(b)

var config Config
if len(b) == 0 {
// empty config file
return &config, nil
return &config, nil // early return on empty config
}

if flags != nil {
Expand Down
15 changes: 15 additions & 0 deletions daemon/config/config_test.go
Expand Up @@ -2,6 +2,7 @@ package config // import "github.com/docker/docker/daemon/config"

import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -35,6 +36,20 @@ func TestDaemonBrokenConfiguration(t *testing.T) {
assert.ErrorContains(t, err, `invalid character ' ' in literal true`)
}

// TestDaemonConfigurationWithBOM ensures that the UTF-8 byte order mark is ignored when reading the configuration file.
func TestDaemonConfigurationWithBOM(t *testing.T) {
configFile := filepath.Join(t.TempDir(), "daemon.json")

f, err := os.Create(configFile)
assert.NilError(t, err)

f.Write([]byte("\xef\xbb\xbf{\"debug\": true}"))
f.Close()

_, err = MergeDaemonConfigurations(&Config{}, nil, configFile)
assert.NilError(t, err)
}

func TestFindConfigurationConflicts(t *testing.T) {
config := map[string]interface{}{"authorization-plugins": "foobar"}
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
Expand Down

0 comments on commit d29ab75

Please sign in to comment.