Skip to content

Ensure replacement values are properly quoted when necessary #109

@jeromefroe

Description

@jeromefroe

YAML requires that strings which contain characters that conflict with YAML syntax elements must be quoted in order to be parsed properly. For example, YAML does not allow unquoted strings to begin with the @ character so any string that begins with this character must be quoted. The expandTransformer, however, does not currently check if a replacement value needs to be quoted and will produce invalid YAML if it's lookup function returns a value that must be quoted. This problem is reproduced in the following program:

package main

import (
	"fmt"
	"os"
	"strings"

	"go.uber.org/config"
)

func main() {
	environment := map[string]string{"FOO": "@foo"}
	lookup := func(key string) (string, bool) {
		s, ok := environment[key]
		return s, ok
	}

	yaml := strings.NewReader("key: ${FOO}")
	p, err := config.NewYAML(config.Source(yaml), config.Expand(lookup))
	if err != nil {
		fmt.Printf("Error: Unable to construct YAML provider: %v.\n", err)
		os.Exit(1)
	}
	fmt.Println(p.Get("key").Value())
}

The output of this program is:

Error: Unable to construct YAML provider: couldn't decode merged YAML: yaml: found character that cannot start any token.

The issue is that the YAML template

key: ${FOO}

is converted to the following YAML source

key: @foo

but in order for this source to be valid YAML it should be

key: "@foo"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions