Skip to content

Commit 95902c1

Browse files
authored
[component] Restrict character set for component.ID name (#10674)
#### Description While working on #10495 we discovered that there are not any restrictions on the `name` field of a `component.ID`. There are restrictions on the `type` field introduced in #9208. This PR adds similar restrictions to `name`. A type must - have at least one character, - start with an ASCII alphabetic character and - can only contain ASCII alphanumeric characters and '_'. I found that we need a slightly different set of rules for name as some tests use a digit and others use a uuid as a name. A name is still optional, but if it's provided it must: - have at least one character, - start with an ASCII alphanumeric character and - can only contain ASCII alphanumeric characters, '_', and '-'. I'd be willing to adjust these restrictions if anyone has any opinions on what should or should not be allowed. <!-- Issue number if applicable --> #### Link to tracking issue Fixes #10673 <!--Describe what testing was performed and which tests were added.--> #### Testing Unit tests <!--Describe the documentation added.--> #### Documentation Code comments <!--Please delete paragraphs that you did not use before submitting.-->
1 parent 192bfe3 commit 95902c1

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

.chloggen/nameval_refactor.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'breaking'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: component
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Adds restrictions on the character set for component.ID name.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [10673]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

component/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,15 @@ var (
164164
// DataTypeLogs is the data type tag for logs.
165165
DataTypeLogs = mustNewDataType("logs")
166166
)
167+
168+
// nameRegexp is used to validate the name of a component. A name can consist of
169+
// 1 to 63 unicode characters excluding whitespace, control characters, and
170+
// symbols.
171+
var nameRegexp = regexp.MustCompile(`^[^\pZ\pC\pS]{1,63}$`)
172+
173+
func validateName(nameStr string) error {
174+
if !nameRegexp.MatchString(nameStr) {
175+
return fmt.Errorf("invalid character(s) in name %q", nameStr)
176+
}
177+
return nil
178+
}

component/identifiable.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func (id *ID) UnmarshalText(text []byte) error {
8282
if nameStr == "" {
8383
return fmt.Errorf("in %q id: the part after %s should not be empty", idStr, typeAndNameSeparator)
8484
}
85+
if err := validateName(nameStr); err != nil {
86+
return fmt.Errorf("in %q id: %w", nameStr, err)
87+
}
8588
}
8689

8790
var err error

component/identifiable_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ func TestUnmarshalText(t *testing.T) {
3535
idStr: " valid_type / valid_name ",
3636
expectedID: ID{typeVal: validType, nameVal: "valid_name"},
3737
},
38+
{
39+
idStr: "valid_type/中文好",
40+
expectedID: ID{typeVal: validType, nameVal: "中文好"},
41+
},
42+
{
43+
idStr: "valid_type/name-with-dashes",
44+
expectedID: ID{typeVal: validType, nameVal: "name-with-dashes"},
45+
},
46+
{
47+
idStr: "valid_type/1",
48+
expectedID: ID{typeVal: validType, nameVal: "1"},
49+
},
3850
{
3951
idStr: "/valid_name",
4052
expectedErr: true,
@@ -55,6 +67,10 @@ func TestUnmarshalText(t *testing.T) {
5567
idStr: " ",
5668
expectedErr: true,
5769
},
70+
{
71+
idStr: "valid_type/invalid name",
72+
expectedErr: true,
73+
},
5874
}
5975

6076
for _, test := range testCases {

0 commit comments

Comments
 (0)