1
0
Fork 0

Merge pull request #7 from jaseemabid/jabid/url-parse

Validate ntfy.sh URL before use
This commit is contained in:
Pablo Ovelleiro Corral 2024-04-02 11:30:37 +02:00 committed by GitHub
commit c669b87012
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
@ -42,7 +43,7 @@ func WebhookHandler(w http.ResponseWriter, r *http.Request) {
req, err := http.NewRequest("POST", os.Getenv("NTFY_TOPIC"), strings.NewReader(alert.Annotations["description"]))
if err != nil {
log.Printf("Building request to %s failed: %s", req.RemoteAddr, err)
log.Printf("Building request failed: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
}
@ -90,6 +91,11 @@ func main() {
}
}
_, err := url.Parse(os.Getenv("NTFY_TOPIC"))
if err != nil {
log.Fatal("Environment variable NTFY_TOPIC is not a valid URL")
}
http.HandleFunc("/", WebhookHandler)
var listenAddr = fmt.Sprintf("%v:%v", os.Getenv("HTTP_ADDRESS"), os.Getenv("HTTP_PORT"))
log.Printf("Listening for HTTP requests (webhooks) on %v\n", listenAddr)