Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

60 website is never accessible if accessed too fast after upload #62

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions int/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -29,6 +32,7 @@ func DefaultConfig() *ServerConfig {
nodeConf := pkgConfig.DefaultConfig("", DefaultNetworkNodeURL)

return &ServerConfig{
Domain: DefaultDomain,
APIPort: DefaultAPIPort,
NetworkInfos: nodeConf.NetworkInfos,
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions int/api/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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, ".")
}
Expand Down
18 changes: 18 additions & 0 deletions int/zipper/zipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/webmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down