diff --git a/cmd/cli/main.go b/cmd/cli/main.go index e4ab8a4..839275e 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -42,6 +42,10 @@ func main() { } filepath := cCtx.Args().Get(1) + if !zipper.IsValidZipFile(filepath) { + return fmt.Errorf("invalid zip file: %s", filepath) + } + config := pkgConfig.DefaultConfig(cCtx.Args().Get(0), "https://buildnet.massa.net/api/v2") siteAddress, err := deployWebsite(config, filepath) @@ -68,6 +72,10 @@ func main() { siteAddress := cCtx.Args().Get(1) filepath := cCtx.Args().Get(2) + if !zipper.IsValidZipFile(filepath) { + return fmt.Errorf("invalid zip file: %s", filepath) + } + bytecode, err := processFileForUpload(filepath) if err != nil { logger.Fatalf("failed to process file for upload: %v", err) diff --git a/int/api/config/config.go b/int/api/config/config.go index 9f9dc53..8803ecf 100644 --- a/int/api/config/config.go +++ b/int/api/config/config.go @@ -11,16 +11,19 @@ import ( ) const ( + DefaultDomain = "localhost" DefaultNetworkNodeURL = "https://buildnet.massa.net/api/v2" DefaultAPIPort = 8080 ) type yamlServerConfig struct { + Domain string `yaml:"domain"` NetworkNodeURL string `yaml:"network_node_url"` APIPort int `yaml:"api_port"` } type ServerConfig struct { + Domain string APIPort int NetworkInfos msConfig.NetworkInfos } @@ -29,6 +32,7 @@ func DefaultConfig() *ServerConfig { nodeConf := pkgConfig.DefaultConfig("", DefaultNetworkNodeURL) return &ServerConfig{ + Domain: DefaultDomain, APIPort: DefaultAPIPort, NetworkInfos: nodeConf.NetworkInfos, } @@ -56,6 +60,10 @@ func LoadServerConfig(configPath string) (*ServerConfig, error) { return nil, fmt.Errorf("failed to unmarshal YAML data: %w", err) } + // Set default values if not specified in the YAML file + if yamlConf.Domain == "" { + yamlConf.Domain = DefaultDomain + } if yamlConf.NetworkNodeURL == "" { yamlConf.NetworkNodeURL = DefaultNetworkNodeURL } @@ -66,6 +74,7 @@ func LoadServerConfig(configPath string) (*ServerConfig, error) { nodeConf := pkgConfig.DefaultConfig("", yamlConf.NetworkNodeURL) return &ServerConfig{ + Domain: yamlConf.Domain, APIPort: yamlConf.APIPort, NetworkInfos: nodeConf.NetworkInfos, }, nil diff --git a/int/api/middlewares.go b/int/api/middlewares.go index 80a66f7..2504d0e 100644 --- a/int/api/middlewares.go +++ b/int/api/middlewares.go @@ -18,7 +18,7 @@ func SubdomainMiddleware(handler http.Handler, conf *config.ServerConfig) http.H return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { logger.Debugf("SubdomainMiddleware: Handling request for %s", r.Host) - subdomain := extractSubdomain(r.Host) + subdomain := extractSubdomain(r.Host, conf.Domain) if subdomain != "" { path := cleanPath(r.URL.Path) @@ -55,8 +55,8 @@ func SubdomainMiddleware(handler http.Handler, conf *config.ServerConfig) http.H } // extractSubdomain extracts the subdomain from the host. -func extractSubdomain(host string) string { - subdomain := strings.Split(host, ".")[0] +func extractSubdomain(host string, domain string) string { + subdomain := strings.Split(host, domain)[0] return strings.TrimSuffix(subdomain, ".") } diff --git a/int/zipper/zipper.go b/int/zipper/zipper.go index ad9b3f2..f4806ed 100644 --- a/int/zipper/zipper.go +++ b/int/zipper/zipper.go @@ -41,3 +41,21 @@ func ReadFileFromZip(zipFile []byte, fileName string) ([]byte, error) { func IsNotFoundError(err error, fileName string) bool { return fmt.Sprintf(notFoundErrorTemplate, fileName) == err.Error() } + +func IsValidZip(zipFile []byte) bool { + reader := bytes.NewReader(zipFile) + _, err := zip.NewReader(reader, int64(reader.Len())) + + return err == nil +} + +func IsValidZipFile(fileName string) bool { + reader, err := zip.OpenReader(fileName) + if err != nil { + return false + } + + _ = reader.Close() + + return true +} diff --git a/pkg/webmanager/manager.go b/pkg/webmanager/manager.go index f8b5c9a..82d5c9a 100644 --- a/pkg/webmanager/manager.go +++ b/pkg/webmanager/manager.go @@ -85,6 +85,10 @@ func fetchAndCache(networkInfo *msConfig.NetworkInfos, scAddress string, cache * logger.Debugf("Website fetched successfully with size: %d bytes", len(websiteBytes)) + if !zipper.IsValidZip(websiteBytes) { + return nil, fmt.Errorf("fetched website is not a valid zip") + } + if err := cache.Save(fileName, websiteBytes); err != nil { return nil, fmt.Errorf("failed to save website to cache: %w", err) }