public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lucas Barros <lucasbarros.kam@gmail.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox] fix #8256: notify: webhook: do not follow redirects
Date: Thu, 12 Nov 2026 01:43:57 -0300	[thread overview]
Message-ID: <20261112044357.82306-1-lucasbarros.kam@gmail.com> (raw)

Webhook targets use the HTTP client with its default redirect handling.
A POST or PUT that is answered with a redirect is then sent again as a
GET to the new location, and the response to that is taken as a
successful delivery. For example, Slack answers a request to a webhook
URL with a malformed token with a redirect to its API documentation, so
the notification is logged as sent and testing the target reports
success, while nothing was delivered.

Do not follow redirects for webhook targets and treat a redirect
response as an error, mentioning the status and the location in the
error message.

Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>
---
Reproduced on a PVE 9 node with a webhook target pointing to a local
test server that answers POST /hook with '302 Location: /docs' and
GET /docs with 200:

  $ pvesh create /cluster/notifications/targets/redir-test/test
  exit=0
  requests received by the server:
  POST /hook
  GET /docs

So the test of the target reports success, although the hook itself
only got redirected. This is what the reporter saw with Slack.

The new test does the same on a loopback socket. It fails without the
change ('send' returns Ok) and passes with it; the rest of
'cargo test -p proxmox-notify --all-features' passes before and after
(54 tests with the new one).

The same could be done for gotify, which uses the same client, but the
bug is only about webhooks, so I left it out.

This patch was prepared with the help of an LLM based coding assistant;
I reviewed the change and ran the tests mentioned above myself.

 proxmox-notify/src/endpoints/webhook.rs | 73 ++++++++++++++++++++++++-
 1 file changed, 71 insertions(+), 2 deletions(-)

diff --git a/proxmox-notify/src/endpoints/webhook.rs b/proxmox-notify/src/endpoints/webhook.rs
index f0fd5b6a..33bf6599 100644
--- a/proxmox-notify/src/endpoints/webhook.rs
+++ b/proxmox-notify/src/endpoints/webhook.rs
@@ -243,10 +243,26 @@ impl Endpoint for WebhookEndpoint {
     fn send(&self, notification: &Notification) -> Result<(), Error> {
         let request = self.build_request(notification)?;
 
-        self.create_client()?
+        let response = self
+            .create_client()?
             .request(request)
             .map_err(|err| self.mask_secret_in_error(err))?;
 
+        // Redirects are not followed, see create_client(). The notification was not delivered to
+        // the configured URL, so do not report success.
+        if response.status().is_redirection() {
+            let location = response
+                .headers()
+                .get(http::header::LOCATION)
+                .and_then(|value| value.to_str().ok())
+                .unwrap_or_default();
+
+            return Err(self.mask_secret_in_error(format!(
+                "webhook request was answered with a redirect ({}) to '{location}'",
+                response.status()
+            )));
+        }
+
         Ok(())
     }
 
@@ -274,7 +290,9 @@ impl WebhookEndpoint {
             ..Default::default()
         };
 
-        Ok(Client::new_with_timeout(options, HTTP_TIMEOUT))
+        // Following a redirect would turn a POST or PUT into a GET to another URL and report its
+        // response, for example a documentation page, as a successful delivery.
+        Ok(Client::new_with_timeout(options, HTTP_TIMEOUT).with_max_redirects(0))
     }
 
     fn build_request(&self, notification: &Notification) -> Result<Request<String>, Error> {
@@ -570,4 +588,55 @@ hello%20world
 
         Ok(())
     }
+
+    #[test]
+    fn test_redirect_is_an_error() {
+        use std::io::{BufRead, BufReader, Write};
+        use std::net::TcpListener;
+
+        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+        let addr = listener.local_addr().unwrap();
+
+        // answers with a redirect to a page on the same server, like a webhook URL with a wrong
+        // token that is redirected to a documentation page
+        std::thread::spawn(move || {
+            let responses = [
+                format!("HTTP/1.1 302 Found\r\nLocation: http://{addr}/docs\r\n"),
+                "HTTP/1.1 200 OK\r\n".to_string(),
+            ];
+            for response in responses {
+                let (mut stream, _) = listener.accept().unwrap();
+                let mut reader = BufReader::new(stream.try_clone().unwrap());
+                let mut line = String::new();
+                while reader.read_line(&mut line).unwrap() > 2 {
+                    line.clear();
+                }
+                write!(
+                    stream,
+                    "{response}Content-Length: 0\r\nConnection: close\r\n\r\n"
+                )
+                .unwrap();
+            }
+        });
+
+        let endpoint = WebhookEndpoint {
+            config: WebhookConfig {
+                name: "test".into(),
+                method: HttpMethod::Post,
+                url: format!("http://{addr}/hook"),
+                ..Default::default()
+            },
+            private_config: WebhookPrivateConfig {
+                name: "test".into(),
+                ..Default::default()
+            },
+        };
+
+        let notification =
+            Notification::from_template(Severity::Info, "foo", json!({}), HashMap::new());
+
+        let err = endpoint.send(&notification).unwrap_err();
+
+        assert!(err.to_string().contains("redirect (302 Found)"), "{err}");
+    }
 }
-- 
2.47.3




                 reply	other threads:[~2026-11-12  4:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112044357.82306-1-lucasbarros.kam@gmail.com \
    --to=lucasbarros.kam@gmail.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal