1
0
mirror of https://github.com/schollz/croc.git synced 2024-11-23 23:54:17 +03:00

ValidFileName returns error

This commit is contained in:
Zack 2024-06-26 06:57:57 -07:00
parent 907f2b3824
commit 3e804a4a09
2 changed files with 16 additions and 19 deletions

View File

@ -571,20 +571,16 @@ func UnzipDirectory(destination string, source string) error {
// ValidFileName checks if a filename is valid
// by making sure it has no invisible characters
func ValidFileName(fname string) bool {
clean1 := strings.Map(func(r rune) rune {
if unicode.IsGraphic(r) {
return r
func ValidFileName(fname string) (err error) {
for _, r := range fname {
if !unicode.IsGraphic(r) {
err = fmt.Errorf("non-graphical unicode: %x U+%d in '%s'", string(r), r, fname)
return
}
return -1
}, fname)
clean2 := strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
if !unicode.IsPrint(r) {
err = fmt.Errorf("non-printable unicode: %x U+%d in '%s'", string(r), r, fname)
return
}
return -1
}, fname)
return (fname == clean1) && (fname == clean2)
}
return
}

View File

@ -252,12 +252,13 @@ func TestIsLocalIP(t *testing.T) {
func TestValidFileName(t *testing.T) {
// contains regular characters
assert.True(t, ValidFileName("中文.csl"))
assert.Nil(t, ValidFileName("中文.csl"))
// contains regular characters
assert.True(t, ValidFileName("[something].csl"))
assert.Nil(t, ValidFileName("[something].csl"))
// contains regular characters
assert.True(t, ValidFileName("[(something)].csl"))
assert.True(t, ValidFileName("_index_slash_ssl.html"))
assert.Nil(t, ValidFileName("[(something)].csl"))
// contains invisible character
assert.False(t, ValidFileName("D中文.cslouglas"))
err := ValidFileName("D中文.cslouglas")
assert.NotNil(t, err)
assert.Equal(t, "non-graphical unicode: e2808b U+8203 in 'D中文.cslouglas'", err.Error())
}