Skip to content

Commit 8a63bb0

Browse files
committed
Add checks for indentation value
Signed-off-by: Bastien <bastien.wermeille@gmail.com>
1 parent 9d20985 commit 8a63bb0

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

stores/json/store.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,10 @@ func (store Store) treeBranchFromJSON(in []byte) (sops.TreeBranch, error) {
250250
func (store Store) reindentJSON(in []byte) ([]byte, error) {
251251
var out bytes.Buffer
252252
indent := "\t"
253-
if store.config.Indent != -1 {
253+
if store.config.Indent > -1 {
254254
indent = strings.Repeat(" ", store.config.Indent)
255+
} else if store.config.Indent < -1 {
256+
return nil, errors.New("JSON Indentation parameter smaller than -1 is not accepted")
255257
}
256258
err := json.Indent(&out, in, "", indent)
257259
return out.Bytes(), err

stores/yaml/store.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package yaml //import "github.com/getsops/sops/v3/stores/yaml"
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
"io"
78
"strings"
@@ -326,19 +327,25 @@ func (store *Store) LoadPlainFile(in []byte) (sops.TreeBranches, error) {
326327
return branches, nil
327328
}
328329

329-
func (store *Store) getIndentation() int {
330-
if store.config.Indent != 0 {
331-
return store.config.Indent
330+
func (store *Store) getIndentation() (int, error) {
331+
if store.config.Indent > 0 {
332+
return store.config.Indent, nil
333+
} else if store.config.Indent < 0 {
334+
return 0, errors.New("YAML Negative indentation not accepted")
332335
}
333-
return IndentDefault
336+
return IndentDefault, nil
334337
}
335338

336339
// EmitEncryptedFile returns the encrypted bytes of the yaml file corresponding to a
337340
// sops.Tree runtime object
338341
func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
339342
var b bytes.Buffer
340343
e := yaml.NewEncoder(io.Writer(&b))
341-
e.SetIndent(store.getIndentation())
344+
indent, err := store.getIndentation()
345+
if err != nil {
346+
return nil, err
347+
}
348+
e.SetIndent(indent)
342349
for _, branch := range in.Branches {
343350
// Document root
344351
var doc = yaml.Node{}
@@ -370,7 +377,11 @@ func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
370377
func (store *Store) EmitPlainFile(branches sops.TreeBranches) ([]byte, error) {
371378
var b bytes.Buffer
372379
e := yaml.NewEncoder(io.Writer(&b))
373-
e.SetIndent(store.getIndentation())
380+
indent, err := store.getIndentation()
381+
if err != nil {
382+
return nil, err
383+
}
384+
e.SetIndent(indent)
374385
for _, branch := range branches {
375386
// Document root
376387
var doc = yaml.Node{}

0 commit comments

Comments
 (0)