8.3: Local fallback for external stylesheets is inverted in CssLoader (basepathIsLocal check negated) — CSS silently dropped when remote fetch fails
Maintainer thường phản hồi trong vòng 5 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 75/100
Hướng nghiên cứu
Bắt đầu tại src/Css/CssLoader.php, ở fallback quanh dòng 51, rồi so sánh điều kiện basepathIsLocal với hành vi trước đây được mô tả trong issue. Kiểm tra xem đã có bài kiểm thử nào bao quát fallback cục bộ khi việc tải stylesheet bên ngoài thất bại hay chưa. Công việc hoàn tất khi fallback chỉ ánh xạ URL sang tệp cục bộ nếu đường dẫn cơ sở là cục bộ, và stylesheet được tải sau khi việc tải từ xa thất bại.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Guidelines
- I understand that if I fail to adhere to contribution guidelines and/or fail to provide all required details, this issue may be closed without review.
Description of the bug
Since 8.3.0, external stylesheets referenced by a same-host absolute URL are no longer read from the local filesystem when the remote fetch fails. The stylesheet is silently dropped (unless debug is on), and the PDF renders with no CSS. This worked in 8.2.7.
The cause appears to be an inverted condition introduced when CssManager was refactored and stylesheet loading moved to Css\CssLoader (commit eeae96b3, "Refactor CssManager").
8.2.7 — src/CssManager.php
$CSSextblock = $this->assetFetcher->fetchDataFromPath($path);
if (!$CSSextblock) {
$path = $this->normalizePath($path);
$CSSextblock = $this->assetFetcher->fetchDataFromPath($path);
}
// ...
private function normalizePath($path)
{
if ($this->mpdf->basepathIsLocal) {
// maps https://host/path to $_SERVER['DOCUMENT_ROOT'] . /path
}
return $path;
}
The fallback is converted to a local path only when basepathIsLocal is true.
8.3.x — src/Css/CssLoader.php (still present on development, line 51)
$data = $this->assetFetcher->fetchDataFromPath($path);
if (!$data) {
$path = !$this->mpdf->basepathIsLocal ? Path::normalizeLocalFilePath($path) : $path;
$data = $this->assetFetcher->fetchDataFromPath($path);
}
The condition is negated. When the base path is local (the exact case the fallback exists for), the path is left unchanged, so the second attempt repeats the same failed remote request. When the base path is not local, a remote URL is mapped onto DOCUMENT_ROOT, which doesn't make sense.
When it shows up
The first (remote) fetch has to fail for this to be visible, so many setups never notice. It fails, for example, when:
- PHP has no CA bundle configured (the default on Windows PHP builds):
cURL error: "SSL certificate OpenSSL verify result: unable to get local issuer certificate (20)" - the server cannot reach its own public hostname (hairpin NAT, split DNS, containers)
- the site uses a self-signed or internal certificate
- the site sits behind auth or a WAF that blocks the server's own requests
In these cases 8.2.7 recovered by reading the file from disk; 8.3.x renders without the stylesheet.
Suggested fix
- $path = !$this->mpdf->basepathIsLocal ? Path::normalizeLocalFilePath($path) : $path;
+ $path = $this->mpdf->basepathIsLocal ? Path::normalizeLocalFilePath($path) : $path;
Related: #234 (prefer local disk over a remote fetch for local stylesheets).
Found via InvoicePlane 1.7.2, which bundles mPDF 8.3.1. Its PDF templates link their stylesheets with absolute base_url() URLs, and every generated invoice lost its styling after the upgrade.
mPDF version
8.3.1 (same line still present on the development branch). Last working version: 8.2.7
PHP Version and environment (server type, cli provider etc., enclosing libraries and their respective versions)
PHP 8.3.36 NTS x64 (FastCGI), IIS 10 on Windows Server 2022, no curl.cainfo / openssl.cafile set (Windows default). Enclosing app: InvoicePlane 1.7.2 (CodeIgniter 3). Expected on any OS where the remote fetch of a same-host stylesheet fails.
Reproducible PHP+CSS+HTML snippet suffering by the error
Run as a web request (needs $_SERVER['DOCUMENT_ROOT'] / HTTP_HOST, so basepathIsLocal is true), with style.css present in the document root and the remote fetch failing, e.g. HTTPS with no CA bundle configured:
<?php
require __DIR__ . '/vendor/autoload.php';
// style.css in the document root contains:
// body { font-family: sans-serif; color: #c00; }
// table { border-collapse: collapse; } td { border: 1px solid #000; padding: 10px; }
$url = 'https://' . $_SERVER['HTTP_HOST'] . '/style.css';
$mpdf = new \Mpdf\Mpdf(['tempDir' => sys_get_temp_dir()]);
$mpdf->debug = true; // without this the stylesheet is dropped silently
$mpdf->WriteHTML(
'<html><head><link rel="stylesheet" href="' . $url . '"></head>'
. '<body><p>Styled?</p><table><tr><td>A</td><td>B</td></tr></table></body></html>'
);
$mpdf->Output();
Expected (as in 8.2.7): the remote fetch fails, the fallback reads DOCUMENT_ROOT/style.css, and the PDF is red sans-serif with bordered cells.
Actual (8.3.1): the remote fetch fails, the fallback repeats the same remote URL, and you get the MpdfException above (debug on) or an unstyled serif PDF (debug off).
Configuring curl.cainfo makes the remote fetch succeed and hides the problem, but doesn't fix the inverted fallback.
- Ngôn ngữ chính
- PHP
- Star
- 4.7k
- Fork
- 1.1k
- Merge trung bình
- 6 ngày 11 giờ
- Pull request đã merge (30 ngày)
- 4
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của mpdf/mpdf
-
SVG path arcs with an explicit zero radius throw DivisionByZeroErrorCó thể đã có người làm @Syed-Shehroz-Sohail đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 5 ngày
-
Closing a column set with an image in it leaves the cursor below the page, wasting the rest of itĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 5 ngày
-
Tag/Td.php silently clears cell top/bottom borders when a row has a partial border (plus two warnings and a deprecation)Có thể đã có người làm @alizamani1616 đã nhận 61 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 5 ngày
-
AdjustHTML() collapses multiple spaces inside quoted attribute values (src, href)Có thể đã có người làm @sornapudisuresh đã nhận 130 ngày trước. Đang mởbug/fix help wanted HTML/CSS support
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
mpdf/mpdf#2208 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 5 ngày
-
bug/fix
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
mpdf/mpdf#1509 · 1 bình luận · 3 reaction ·
Maintainer thường phản hồi trong vòng 5 ngày
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
sync-en
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Перевод устарел
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
follow-up
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày