Skip to content

Commit

Permalink
func: User url rules to scape non alphanumeric values in hcl variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanadelacuesta committed Nov 11, 2024
1 parent c5249c6 commit 4d40732
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 2 additions & 4 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/url"
"sort"
"strconv"
"strings"
"time"

"github.com/hashicorp/cronexpr"
Expand Down Expand Up @@ -324,6 +323,7 @@ func (j *Jobs) Submission(jobID string, version int, q *QueryOptions) (*JobSubmi
if err != nil {
return nil, nil, err
}

return &sub, qm, nil
}

Expand Down Expand Up @@ -1061,9 +1061,7 @@ func (js *JobSubmission) Canonicalize() {
// characters to preserve them. This way, when the job gets stopped and
// restarted in the UI, variable values will be parsed correctly.
for k, v := range js.VariableFlags {
if strings.Contains(v, "\n") {
js.VariableFlags[k] = strings.ReplaceAll(v, "\n", "\\n")
}
js.VariableFlags[k] = url.QueryEscape(v)
}
}

Expand Down
12 changes: 11 additions & 1 deletion api/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,17 @@ func TestJobs_JobSubmission_Canonicalize(t *testing.T) {
VariableFlags: map[string]string{"test": "foo\nbar"},
}
js.Canonicalize()
must.Eq(t, js.VariableFlags["test"], "foo\\nbar")

must.Eq(t, js.VariableFlags["test"], "foo%0Abar")
})

t.Run("non-alphabetic chars", func(t *testing.T) {
js := &JobSubmission{
Source: "abc123",
VariableFlags: map[string]string{"test": `"foo": "bar"`},
}
js.Canonicalize()
must.Eq(t, js.VariableFlags["test"], "%22foo%22%3A+%22bar%22")
})
}

Expand Down

0 comments on commit 4d40732

Please sign in to comment.