Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrectly indented block scalar is successfully parsed #366

Closed
martin-sucha opened this issue Mar 21, 2023 · 0 comments · Fixed by #513
Closed

Incorrectly indented block scalar is successfully parsed #366

martin-sucha opened this issue Mar 21, 2023 · 0 comments · Fixed by #513
Labels
bug Something isn't working parser

Comments

@martin-sucha
Copy link
Contributor

The following yaml is invalid according to the spec:

args:
  - |
      aaa
    key:
      nest1: something
      nest2:
        nest2a: b

This is because when the Block Indentation Indicator is absent, content indentation level is determined by the leading spaces in first non-empty line (or largest number of spaces if there are only empty lines).

Section 8.1.1.1. of yaml v1.2.2 spec:

If no indentation indicator is given, then the content indentation level is equal to the number of leading spaces on the first non-empty line of the contents. If there is no non-empty line then the content indentation level is equal to the number of spaces on the longest line.

It is an error if any non-empty line does not begin with a number of spaces greater than or equal to the content indentation level.

go-yaml v1.10.0 does not return an error in this case.

Here is a test that reproduces the issue:

func TestParseBlockIndent(t *testing.T) {
	in := `args:
  - |
      aaa
    key:
      nest1: something
      nest2:
        nest2a: b
`
	_, err := parser.ParseBytes([]byte(in), 0)
	if err == nil {
		t.Fatalf("expected error, but no error was produced")
	}
}

For the record, correct version of the block scalar above should have the indentation indicator present, like this:

args:
  - |2
      aaa
    key:
      nest1: something
      nest2:
        nest2a: b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants