From d36bee0bdd3ae62cfff80e827133442e8a0210d1 Mon Sep 17 00:00:00 2001
From: Mårten Nordheim <marten.nordheim@qt.io>
Date: Tue, 29 Sep 2026 18:21:20 +0200
Subject: [PATCH] QAuthenticator: pass the SPN to SSPI for NTLM as well

qSspiContinue() left the target name empty for NTLM, but this can be
used to verify the intended target server on the server side.

When used by the server, it can identify whether a request was
originally aimed at the server or if it has been relayed from somewhere
else. Effectively making it a cheap way to thwart relay attacks when
the server takes it into consideration.

[ChangeLog][QtNetwork][QAuthenticator] On Windows, NTLM authentication
now sends the target SPN (the "spn" option, or HTTP/<host>) to SSPI,
as Negotiate already did.

Task-number: QTBUG-150897
Pick-to: 6.11 6.8 6.5 5.15
Change-Id: I8a8c9e412b36ffcc670ac0c2fb1178540976a9bf
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mate Barany <mate.barany@qt.io>
(cherry picked from commit 9d74952b9ce11e04868fe4f86d88c602a2c5759c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
---

diff --git a/src/network/kernel/qauthenticator.cpp b/src/network/kernel/qauthenticator.cpp
index fc8a887..44742ff4 100644
--- a/src/network/kernel/qauthenticator.cpp
+++ b/src/network/kernel/qauthenticator.cpp
@@ -1718,6 +1718,7 @@
 static QByteArray qSspiContinue(QAuthenticatorPrivate *ctx, QAuthenticatorPrivate::Method method,
                                 QStringView host, QByteArrayView challenge)
 {
+    Q_UNUSED(method);
     QByteArray result;
     SecBuffer challengeBuf;
     SecBuffer responseBuf;
@@ -1745,12 +1746,10 @@
     responseBuf.pvBuffer   = nullptr;
     responseBuf.cbBuffer   = 0;
 
-    // Calculate target (SPN for Negotiate, empty for NTLM)
     QString targetName = ctx->options.value("spn"_L1).toString();
     if (targetName.isEmpty())
         targetName = "HTTP/"_L1 + host;
-    const std::wstring targetNameW = (method == QAuthenticatorPrivate::Negotiate
-                                      ? targetName : QString()).toStdWString();
+    const std::wstring targetNameW = targetName.toStdWString();
 
     // Generate our challenge-response message
     SECURITY_STATUS secStatus = pSecurityFunctionTable->InitializeSecurityContext(
