Skip to content

Commit

Permalink
Fix edge cases when parsing subproperties (#2359)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahvita authored Nov 9, 2023
1 parent 5aa6e2b commit 3db3b14
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .changelog/d767f490a7f644ebb9eb36f126053870.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "d767f490-a7f6-44eb-b9eb-36f126053870",
"type": "feature",
"description": "BREAKFIX: In order to support subproperty parsing, invalid property definitions must not be ignored",
"modules": [
"internal/ini",
"config"
]
}
12 changes: 12 additions & 0 deletions config/shared_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var (
testConfigFilename = filepath.Join("testdata", "shared_config")
testConfigOtherFilename = filepath.Join("testdata", "shared_config_other")
testCredentialsFilename = filepath.Join("testdata", "shared_credentials")
testConfigLeadingWSFilename1 = filepath.Join("testdata", "leading_ws")
testConfigLeadingWSFilename2 = filepath.Join("testdata", "leading_ws_trailing_nl")
)

func TestNewSharedConfig(t *testing.T) {
Expand Down Expand Up @@ -685,6 +687,16 @@ func TestNewSharedConfig(t *testing.T) {
EC2IMDSv1Disabled: aws.Bool(false),
},
},
"leading whitespace error 1": {
ConfigFilenames: []string{testConfigLeadingWSFilename1},
Profile: "leading-whitespace-error",
Err: fmt.Errorf("Invalid token, remove leading whitespace"),
},
"leading whitespace error 2": {
ConfigFilenames: []string{testConfigLeadingWSFilename2},
Profile: "leading-whitespace-error",
Err: fmt.Errorf("Invalid token, remove leading whitespace"),
},
}

for name, c := range cases {
Expand Down
2 changes: 2 additions & 0 deletions config/testdata/leading_ws
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[profile leading-whitespace-error]
retry_mode = standard
3 changes: 3 additions & 0 deletions config/testdata/leading_ws_trailing_nl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[profile leading-whitespace-error]
retry_mode = standard

7 changes: 7 additions & 0 deletions internal/ini/ini_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ loop:
stack.Push(k)
case StatementState:
if k.Kind != ASTKindStart {
if tok.Type() == TokenLit && isSubProperty(tok.raw) {
return nil, NewParseError(
fmt.Sprintf(
"Invalid token, remove leading whitespace %s",
string(tok.raw)),
)
}
stack.MarkComplete(k)
}
expr := newExpression(tok)
Expand Down
2 changes: 1 addition & 1 deletion internal/ini/literal_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func getSubProperty(runes []rune, offset int) (int, error) {
return offset + idx, nil
}
}
return 0, fmt.Errorf("no sub property")
return offset + len(runes), nil
}

// MapValue returns a map value for sub properties
Expand Down

0 comments on commit 3db3b14

Please sign in to comment.