Package: apt-cacher-ng
Version: 3.7.4-1+b2
Severity: normal
Tags: patch
X-Debbugs-Cc: [email protected]

Originally discovered and discussed in #debian-mentors IRC which got me
to debugging it as shown here.

$ http_proxy=http://127.0.0.1:3142 wget -S  
http://snapshot-mlm-01.debian.org/archive/debian/20240617T023617Z/dists/unstable/InRelease
--2024-06-26 00:37:20--  
http://snapshot-mlm-01.debian.org/archive/debian/20240617T023617Z/dists/unstable/InRelease
Connecting to 127.0.0.1:3142... connected.
Proxy request sent, awaiting response...
HTTP/1.1 500 Remote or cache error
Content-Length: 492
Content-Type: text/html
Connection: Keep-Alive
Date: Tue, 25 Jun 2024 23:37:38 GMT
Server: Debian Apt-Cacher NG/3.7.4
2024-06-26 00:37:38 ERROR 500: Remote or cache error.

apt-cacher-ng-3.7.4/src# gdb --directory $PWD --args /usr/sbin/apt-cacher-ng -c 
"/etc/apt-cacher-ng" ForeGround=1
GNU gdb (Debian 13.1-3) 13.1
...
Reading symbols from /usr/sbin/apt-cacher-ng...

This GDB supports auto-downloading debuginfo from the following URLs:
<https://debuginfod.debian.net>
Enable debuginfod for this session? (y or [n]) y
Debuginfod has been enabled.
To make this setting permanent, add 'set debuginfod enabled on' to .gdbinit.
Downloading separate debug info for /usr/sbin/apt-cacher-ng
Reading symbols from 
/root/.cache/debuginfod_client/00ddd159ae04921c4537ad1eefb870636580ec62/debuginfo...
(gdb) start
Temporary breakpoint 1 at 0x2520: file ./src/apt-cacher.cc, line 312.
Starting program: /usr/sbin/apt-cacher-ng -c /etc/apt-cacher-ng ForeGround=1
...
Temporary breakpoint 1, main (argc=4, argv=0x7fffffffe348) at 
./src/apt-cacher.cc:312
312     {
(gdb) break RewriteSource
Breakpoint 2 at 0x7ffff7ec7300: file ./src/dlcon.cc, line 190.
(gdb) c
Continuing.
Loaded 11 backend descriptors
Loaded mappings for 1558 hosts and 2228 paths
[New Thread 0x7ffff6e006c0 (LWP 1770177)]
[New Thread 0x7ffff64006c0 (LWP 1770178)]
[Switching to Thread 0x7ffff64006c0 (LWP 1770178)]

Thread 3 "apt-cacher-ng" hit Breakpoint 2, acng::tDlJob::RewriteSource 
(this=this@entry=0x7ffff0023710,
pNewUrl=0x7fffe80081a0 "/file/63b5c6e98e9fb2ff206f8043a9ff68014c7cf771") at 
./src/dlcon.cc:190
190             inline bool RewriteSource(const char *pNewUrl)
(gdb) p pNewUrl
$1 = 0x7fffe80081a0 "/file/63b5c6e98e9fb2ff206f8043a9ff68014c7cf771"
(gdb) n
199                     if (!pNewUrl || !*pNewUrl)
(gdb) n
206                     m_pCurBackend = nullptr;
(gdb) n
210                     auto sLocationDecoded = UrlUnescape(pNewUrl);
(gdb) n
238               ~_Guard() { if (_M_guarded) _M_guarded->_M_dispose(); }
(gdb) p sLocationDecoded
$2 = ""
(gdb) n
216                             m_remoteUri = newUri;
(gdb) p newUri
$3 = {nPort = 0, sHost = "file", sPath = 
"/63b5c6e98e9fb2ff206f8043a9ff68014c7cf771", sUserPass = "", bSSL = false}
(gdb) p sLocationDecoded
$4 = "/file/63b5c6e98e9fb2ff206f8043a9ff68014c7cf771"

Shown here sHost has been incorrectly assigned the first segment of the path
and sPath is set to a non-existent value which in this case confuses the 
snapshot server.

With my patch we see success:

$ http_proxy=http://127.0.0.1:3142 wget -S  
http://snapshot-mlm-01.debian.org/archive/debian/20240617T023617Z/dists/unstable/InRelease
--2024-06-28 04:19:32--  
http://snapshot-mlm-01.debian.org/archive/debian/20240617T023617Z/dists/unstable/InRelease
Connecting to 127.0.0.1:3142... connected.
Proxy request sent, awaiting response...
HTTP/1.1 200 OK
Content-Type: octet/stream
Last-Modified: Mon, 17 Jun 2024 02:57:43 GMT
Content-Length: 198381
X-Original-Source: 
http://snapshot-mlm-01.debian.org/file/63b5c6e98e9fb2ff206f8043a9ff68014c7cf771
Connection: Keep-Alive
Date: Fri, 28 Jun 2024 03:19:33 GMT
Server: Debian Apt-Cacher NG/3.7.4
Length: 198381 (194K) [octet/stream]
Saving to: ‘InRelease’

InRelease                                  
100%[=======================================================================================>]
 193.73K   700KB/s    in 0.3s

2024-06-28 04:19:34 (700 KB/s) - ‘InRelease’ saved [198381/198381]

$ head InRelease
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Origin: Debian
Label: Debian
Suite: unstable
Codename: sid
Changelogs: 
https://metadata.ftp-master.debian.org/changelogs/@CHANGEPATH@_changelog
Date: Mon, 17 Jun 2024 02:15:25 UTC
Valid-Until: Mon, 24 Jun 2024 02:15:25 UTC
>From 51961164a89c562b333efcbc3dcd8537794d2ed6 Mon Sep 17 00:00:00 2001
From: Tj <[email protected]>
Date: Wed, 26 Jun 2024 01:39:16 +0100
Subject: [PATCH] Location: handle /path/ redirects

Fixes 302 Location redirects on snapshot.debian.org.

Signed-off-by: Tj <[email protected]>
---
 src/dlcon.cc | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/dlcon.cc b/src/dlcon.cc
index 935e41f..8d280ad 100644
--- a/src/dlcon.cc
+++ b/src/dlcon.cc
@@ -209,6 +209,14 @@ struct tDlJob
 
                auto sLocationDecoded = UrlUnescape(pNewUrl);
 
+               if (startsWithSz(sLocationDecoded, "/"))
+               {
+                       return m_remoteUri.SetHttpUrl(
+                               m_remoteUri.GetProtoPrefix()
+                               + m_remoteUri.sHost
+                               + sLocationDecoded);
+               }
+
                tHttpUrl newUri;
                if (newUri.SetHttpUrl(sLocationDecoded, false))
                {
-- 
2.39.2

Reply via email to