Skip to content

Commit

Permalink
Init 0.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
n0vad3v committed Nov 27, 2024
1 parent ec27023 commit 2f9a27b
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 22 deletions.
32 changes: 17 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@ const (
)

var (
ConfigPath string
Jobs int
DumpSystemd bool
DumpConfig bool
ShowVersion bool
ProxyMode bool
Prefetch bool // Prefech in go-routine, with WebP Server Go launch normally
PrefetchForeground bool // Standalone prefetch, prefetch and exit
Config = NewWebPConfig()
Version = "0.12.3"
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
ConvertLock = cache.New(5*time.Minute, 10*time.Minute)
LocalHostAlias = "local"
RemoteCache *cache.Cache
ConfigPath string
Jobs int
DumpSystemd bool
DumpConfig bool
ShowVersion bool
ProxyMode bool
Prefetch bool // Prefech in go-routine, with WebP Server Go launch normally
PrefetchForeground bool // Standalone prefetch, prefetch and exit
AllowNonImage bool
Config = NewWebPConfig()
Version = "0.13.0"
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
ConvertLock = cache.New(5*time.Minute, 10*time.Minute)
LocalHostAlias = "local"
RemoteCache *cache.Cache
DefaultAllowedTypes = []string{"jpg", "png", "jpeg", "bmp", "gif", "svg", "nef", "heic"} // Default allowed image types
)

type MetaFile struct {
Expand Down Expand Up @@ -99,7 +101,7 @@ func NewWebPConfig() *WebpConfig {
Port: "3333",
ImgPath: "./pics",
Quality: 80,
AllowedTypes: []string{"jpg", "png", "jpeg", "bmp", "gif", "svg", "nef", "heic", "webp"},
AllowedTypes: DefaultAllowedTypes,
ConvertTypes: []string{"webp"},
ImageMap: map[string]string{},
ExhaustPath: "./exhaust",
Expand Down
11 changes: 10 additions & 1 deletion encoder/prefetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,18 @@ func PrefetchImages() {
if info.IsDir() {
return nil
}
if !helper.CheckAllowedType(picAbsPath) {
// Only convert files with image extensions, use smaller of config.DefaultAllowedTypes and config.Config.AllowedTypes
if helper.CheckAllowedExtension(picAbsPath) {
// File type is allowed by user, check if it is an image
if helper.CheckImageExtension(picAbsPath) {
// File is an image, continue
} else {
return nil
}
} else {
return nil
}

// RawImagePath string, ImgFilename string, reqURI string
metadata := helper.ReadMetadata(picAbsPath, "", config.LocalHostAlias)
avifAbsPath, webpAbsPath, jxlAbsPath := helper.GenOptimizedAbsPath(metadata, config.LocalHostAlias)
Expand Down
10 changes: 8 additions & 2 deletions handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,20 @@ func Convert(c *fiber.Ctx) error {

log.Debugf("Incoming connection from %s %s %s", c.IP(), reqHostname, reqURIwithQuery)

if !helper.CheckAllowedType(filename) {
if !helper.CheckAllowedExtension(filename) {
msg := "File extension not allowed! " + filename
log.Warn(msg)
c.Status(http.StatusBadRequest)
_ = c.Send([]byte(msg))
_ = c.SendString(msg)
return nil
}

// Check if the file extension is allowed and not with image extension
// In this case we will serve the file directly
if helper.CheckAllowedExtension(filename) && !helper.CheckImageExtension(filename) {
return c.SendFile(path.Join(config.Config.ImgPath, reqURI))
}

// Rewrite the target backend if a mapping rule matches the hostname
if hostMap, hostMapFound := config.Config.ImageMap[reqHost]; hostMapFound {
log.Debugf("Found host mapping %s -> %s", reqHostname, hostMap)
Expand Down
21 changes: 21 additions & 0 deletions handler/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,33 @@ func TestConvertNotAllowed(t *testing.T) {
defer resp.Body.Close()
assert.Contains(t, string(data), "File extension not allowed")

// not allowed, but we have the file, this should return File extension not allowed
url = "http://127.0.0.1:3333/config.json"
resp, data = requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Contains(t, string(data), "File extension not allowed")

// not allowed, random file
url = url + "hagdgd"
resp, data = requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Contains(t, string(data), "File extension not allowed")
}

func TestConvertPassThrough(t *testing.T) {
setupParam()
config.Config.AllowedTypes = []string{"*"}

var app = fiber.New()
app.Get("/*", Convert)

// not allowed, but we have the file, this should return File extension not allowed
url := "http://127.0.0.1:3333/config.json"
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Contains(t, string(data), "HOST")
}

func TestConvertProxyModeBad(t *testing.T) {
Expand Down
10 changes: 9 additions & 1 deletion helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func ImageExists(filename string) bool {
return !info.IsDir()
}

func CheckAllowedType(imgFilename string) bool {
// CheckAllowedExtension checks if the image extension is in the user's allowed types
func CheckAllowedExtension(imgFilename string) bool {
if config.Config.AllowedTypes[0] == "*" {
return true
}
Expand All @@ -93,6 +94,13 @@ func CheckAllowedType(imgFilename string) bool {
return slices.Contains(config.Config.AllowedTypes, imgFilenameExtension)
}

// CheckImageExtension checks if the image extension is in the WebP Server Go's default types
func CheckImageExtension(imgFilename string) bool {
imgFilenameExtension := strings.ToLower(path.Ext(imgFilename))
imgFilenameExtension = strings.TrimPrefix(imgFilenameExtension, ".") // .jpg -> jpg
return slices.Contains(config.DefaultAllowedTypes, imgFilenameExtension)
}

func GenOptimizedAbsPath(metadata config.MetaFile, subdir string) (string, string, string) {
webpFilename := fmt.Sprintf("%s.webp", metadata.Id)
avifFilename := fmt.Sprintf("%s.avif", metadata.Id)
Expand Down
6 changes: 3 additions & 3 deletions helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func TestImageExists(t *testing.T) {
})
}

func TestCheckAllowedType(t *testing.T) {
func TestCheckAllowedExtension(t *testing.T) {
t.Run("not allowed type", func(t *testing.T) {
assert.False(t, CheckAllowedType("./helper_test.go"))
assert.False(t, CheckAllowedExtension("./helper_test.go"))
})

t.Run("allowed type", func(t *testing.T) {
assert.True(t, CheckAllowedType("test.jpg"))
assert.True(t, CheckAllowedExtension("test.jpg"))
})
}

Expand Down
16 changes: 16 additions & 0 deletions pics/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"HOST": "127.0.0.1",
"PORT": "3333",
"QUALITY": "80",
"IMG_PATH": "./pics",
"EXHAUST_PATH": "./exhaust",
"IMG_MAP": {},
"ALLOWED_TYPES": ["jpg","png","jpeg","gif","bmp","svg","heic","nef"],
"CONVERT_TYPES": ["webp"],
"STRIP_METADATA": true,
"ENABLE_EXTRA_PARAMS": false,
"READ_BUFFER_SIZE": 4096,
"CONCURRENCY": 262144,
"DISABLE_KEEPALIVE": false,
"CACHE_TTL": 259200
}

0 comments on commit 2f9a27b

Please sign in to comment.