~xenrox/ntfy-alertmanager

3bbde04774cdea84a9b9acfdb3224496e11df2ac — Thorben Günther 16 days ago d34f90a master
Remove HTTP method checks

This is enforced by the router now.
2 files changed, 2 insertions(+), 15 deletions(-)

M main.go
M silence.go
M main.go => main.go +2 -8
@@ 430,12 430,6 @@ func (br *bridge) handleWebhooks(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	logger := br.logger.With(slog.String("handler", "/"))

	if r.Method != http.MethodPost {
		http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
		logger.Debug(fmt.Sprintf("Illegal HTTP method: expected %q, got %q", "POST", r.Method))
		return
	}

	contentType := r.Header.Get("Content-Type")
	if contentType != "application/json" {
		http.Error(w, "Only application/json allowed", http.StatusUnsupportedMediaType)


@@ 586,8 580,8 @@ func main() {
	logger.Info(fmt.Sprintf("Listening on %s, ntfy-alertmanager %s", cfg.HTTPAddress, version))

	mux := http.NewServeMux()
	mux.HandleFunc("/", bridge.handleWebhooks)
	mux.HandleFunc("/silences", bridge.handleSilences)
	mux.HandleFunc("POST /", bridge.handleWebhooks)
	mux.HandleFunc("POST /silences", bridge.handleSilences)

	httpServer := &http.Server{
		Addr:    cfg.HTTPAddress,

M silence.go => silence.go +0 -7
@@ 4,7 4,6 @@ import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"log/slog"
	"net/http"


@@ 40,12 39,6 @@ type silenceResponse struct {
func (br *bridge) handleSilences(w http.ResponseWriter, r *http.Request) {
	logger := br.logger.With(slog.String("handler", "/silences"))

	if r.Method != http.MethodPost {
		http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
		logger.Debug(fmt.Sprintf("Illegal HTTP method: expected %q, got %q", "POST", r.Method))
		return
	}

	b, err := io.ReadAll(r.Body)
	if err != nil {
		logger.Error("Failed to read body",