Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
56f8aa69
Unverified
Commit
56f8aa69
authored
Apr 20, 2024
by
Blake Mizerany
Committed by
GitHub
Apr 20, 2024
Browse files
types/model: export IsValidNamePart (#3788)
parent
e6f9bfc0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
6 deletions
+15
-6
types/model/name.go
types/model/name.go
+9
-6
types/model/name_test.go
types/model/name_test.go
+6
-0
No files found.
types/model/name.go
View file @
56f8aa69
...
...
@@ -156,7 +156,7 @@ func ParseName(s, fill string) Name {
r
=
Name
{}
return
false
}
if
kind
==
PartExtraneous
||
!
i
sValidPart
(
kind
,
part
)
{
if
kind
==
PartExtraneous
||
!
I
sValid
Name
Part
(
kind
,
part
)
{
r
=
Name
{}
return
false
}
...
...
@@ -176,7 +176,7 @@ func parseMask(s string) Name {
// mask part; treat as empty but valid
return
true
}
if
!
i
sValidPart
(
kind
,
part
)
{
if
!
I
sValid
Name
Part
(
kind
,
part
)
{
panic
(
fmt
.
Errorf
(
"invalid mask part %s: %q"
,
kind
,
part
))
}
r
.
parts
[
kind
]
=
part
...
...
@@ -608,7 +608,7 @@ func ParseNameFromFilepath(s, fill string) Name {
var
r
Name
for
i
:=
range
PartBuild
+
1
{
part
,
rest
,
_
:=
strings
.
Cut
(
s
,
string
(
filepath
.
Separator
))
if
!
i
sValidPart
(
i
,
part
)
{
if
!
I
sValid
Name
Part
(
i
,
part
)
{
return
Name
{}
}
r
.
parts
[
i
]
=
part
...
...
@@ -654,9 +654,12 @@ func (r Name) FilepathNoBuild() string {
return
filepath
.
Join
(
r
.
parts
[
:
PartBuild
]
...
)
}
// isValidPart reports if s contains all valid characters for the given
// part kind.
func
isValidPart
(
kind
PartKind
,
s
string
)
bool
{
// IsValidNamePart reports if s contains all valid characters for the given
// part kind and is under MaxNamePartLen bytes.
func
IsValidNamePart
(
kind
PartKind
,
s
string
)
bool
{
if
len
(
s
)
>
MaxNamePartLen
{
return
false
}
if
s
==
""
{
return
false
}
...
...
types/model/name_test.go
View file @
56f8aa69
...
...
@@ -105,6 +105,12 @@ var testNames = map[string]fields{
strings
.
Repeat
(
"a"
,
MaxNamePartLen
+
1
)
:
{},
}
func
TestIsValidNameLen
(
t
*
testing
.
T
)
{
if
IsValidNamePart
(
PartNamespace
,
strings
.
Repeat
(
"a"
,
MaxNamePartLen
+
1
))
{
t
.
Errorf
(
"unexpectedly valid long name"
)
}
}
// TestConsecutiveDots tests that consecutive dots are not allowed in any
// part, to avoid path traversal. There also are some tests in testNames, but
// this test is more exhaustive and exists to emphasize the importance of
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment