diff --git a/config.go b/config.go index aee8b15..9fb96b9 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ import ( "log" "os" "path/filepath" + "strings" "github.com/BurntSushi/toml" ) @@ -93,6 +94,18 @@ func getConfig(filename string) (Config, error) { } config.CGIPaths = cgiPaths + // Validate redirects + for _, value := range config.TempRedirects { + if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") { + return config, errors.New("Invalid cross-protocol redirect to " + value) + } + } + for _, value := range config.PermRedirects { + if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") { + return config, errors.New("Ignoring cross-protocol redirect to " + value) + } + } + return config, nil } @@ -164,9 +177,17 @@ func parseMollyFiles(path string, config *Config, errorLog *log.Logger) { config.DirectoryReverse = mollyFile.DirectoryReverse config.DirectoryTitles = mollyFile.DirectoryTitles for key, value := range mollyFile.TempRedirects { + if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") { + errorLog.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + mollyPath) + continue + } config.TempRedirects[key] = value } for key, value := range mollyFile.PermRedirects { + if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") { + errorLog.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + mollyPath) + continue + } config.PermRedirects[key] = value } for key, value := range mollyFile.MimeOverrides { diff --git a/handler.go b/handler.go index 096416e..d476c3b 100644 --- a/handler.go +++ b/handler.go @@ -193,8 +193,12 @@ func handleRedirectsInner(URL *url.URL, redirects map[string]string, status int, continue } if compiled.MatchString(URL.Path) { - URL.Path = compiled.ReplaceAllString(URL.Path, dst) - conn.Write([]byte(strStatus + " " + URL.String() + "\r\n")) + new_target := compiled.ReplaceAllString(URL.Path, dst) + if !strings.HasPrefix(new_target, "gemini://") { + URL.Path = new_target + new_target = URL.String() + } + conn.Write([]byte(strStatus + " " + new_target + "\r\n")) log.Status = status return }