Skip to content

Commit ce7f773

Browse files
committed
Remove colon from characters allowed in anchor
This partially rolls back the changes made with 926dd0b The colon was allowed in the characters allowed for anchor and alias name. But it leads to ambiguity in the YAML structure.
1 parent d280c75 commit ce7f773

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

node_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ var nodeTests = []struct {
11301130
}},
11311131
},
11321132
}, {
1133-
"a: &anchor(.!@#$%^&*+=?:;)name [1, 2]\nb: *anchor(.!@#$%^&*+=?:;)name\n",
1133+
"a: &anchor(.!@#$%^&*+=?;)name [1, 2]\nb: *anchor(.!@#$%^&*+=?;)name\n",
11341134
yaml.Node{
11351135
Kind: yaml.DocumentNode,
11361136
Line: 1,
@@ -1148,25 +1148,25 @@ var nodeTests = []struct {
11481148
Line: 1,
11491149
Column: 1,
11501150
},
1151-
saveNode("anchor(.!@#$%^&*+=?:;)name", &yaml.Node{
1151+
saveNode("anchor(.!@#$%^&*+=?;)name", &yaml.Node{
11521152
Kind: yaml.SequenceNode,
11531153
Style: yaml.FlowStyle,
11541154
Tag: "!!seq",
1155-
Anchor: "anchor(.!@#$%^&*+=?:;)name",
1155+
Anchor: "anchor(.!@#$%^&*+=?;)name",
11561156
Line: 1,
11571157
Column: 4,
11581158
Content: []*yaml.Node{{
11591159
Kind: yaml.ScalarNode,
11601160
Value: "1",
11611161
Tag: "!!int",
11621162
Line: 1,
1163-
Column: 33,
1163+
Column: 32,
11641164
}, {
11651165
Kind: yaml.ScalarNode,
11661166
Value: "2",
11671167
Tag: "!!int",
11681168
Line: 1,
1169-
Column: 36,
1169+
Column: 35,
11701170
}},
11711171
}),
11721172
{
@@ -1178,8 +1178,8 @@ var nodeTests = []struct {
11781178
},
11791179
{
11801180
Kind: yaml.AliasNode,
1181-
Value: "anchor(.!@#$%^&*+=?:;)name",
1182-
Alias: dropNode("anchor(.!@#$%^&*+=?:;)name"),
1181+
Value: "anchor(.!@#$%^&*+=?;)name",
1182+
Alias: dropNode("anchor(.!@#$%^&*+=?;)name"),
11831183
Line: 2,
11841184
Column: 4,
11851185
},

yamlprivateh.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ func isFlowIndicator(b []byte, i int) bool {
6767
// We further limit it to ascii chars only, which is a subset of the spec
6868
// production but is usually what most people expect.
6969
func isAnchorChar(b []byte, i int) bool {
70+
if isColon(b, i) {
71+
// [Go] we exclude colons from anchor/alias names.
72+
//
73+
// A colon is a valid anchor character according to the YAML 1.2 specification,
74+
// but it can lead to ambiguity.
75+
// https://github.com/yaml/go-yaml/issues/109
76+
//
77+
// Also, it would have been a breaking change to support it, as go.yaml.in/yaml/v3 ignores it.
78+
// Supporting it could lead to unexpected behavior.
79+
return false
80+
}
81+
7082
return isPrintable(b, i) &&
7183
!isLineBreak(b, i) &&
7284
!isBlank(b, i) &&
@@ -75,6 +87,11 @@ func isAnchorChar(b []byte, i int) bool {
7587
isASCII(b, i)
7688
}
7789

90+
// isColon checks whether the character at the specified position is a colon.
91+
func isColon(b []byte, i int) bool {
92+
return b[i] == ':'
93+
}
94+
7895
// Check if the character at the specified position is a digit.
7996
func isDigit(b []byte, i int) bool {
8097
return b[i] >= '0' && b[i] <= '9'

yts/known-failing-tests

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ TestYAMLSuite/2JQS/UnmarshalTest
44
TestYAMLSuite/2JQS/EventComparisonTest
55
TestYAMLSuite/2LFX/UnmarshalTest
66
TestYAMLSuite/2LFX/EventComparisonTest
7+
TestYAMLSuite/2SXE/UnmarshalTest
8+
TestYAMLSuite/2SXE/EventComparisonTest
79
TestYAMLSuite/35KP/JSONComparisonTest
810
TestYAMLSuite/3HFZ/UnmarshalTest
911
TestYAMLSuite/3UYS/UnmarshalTest
@@ -185,13 +187,17 @@ TestYAMLSuite/01#15/UnmarshalTest
185187
TestYAMLSuite/01#15/EventComparisonTest
186188
TestYAMLSuite/W4TN/UnmarshalTest
187189
TestYAMLSuite/W4TN/EventComparisonTest
190+
TestYAMLSuite/W5VH/UnmarshalTest
191+
TestYAMLSuite/W5VH/EventComparisonTest
188192
TestYAMLSuite/WZ62/UnmarshalTest
189193
TestYAMLSuite/WZ62/EventComparisonTest
190194
TestYAMLSuite/X38W/UnmarshalTest
191195
TestYAMLSuite/X38W/EventComparisonTest
192196
TestYAMLSuite/X4QW/UnmarshalTest
193197
TestYAMLSuite/X4QW/EventComparisonTest
194198
TestYAMLSuite/XW4D/UnmarshalTest
199+
TestYAMLSuite/Y2GN/EventComparisonTest
200+
TestYAMLSuite/Y2GN/JSONComparisonTest
195201
TestYAMLSuite/001/UnmarshalTest
196202
TestYAMLSuite/001/EventComparisonTest
197203
TestYAMLSuite/003/UnmarshalTest

0 commit comments

Comments
 (0)