8.3: Local fallback for external stylesheets is inverted in CssLoader (basepathIsLocal check negated) — CSS silently dropped when remote fetch fails

Đang mở Phù hợp với người mới
#2,244 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 5 ngày

@hemreduru đang làm issue này rồi.

Từ ngày 22/11/2026.

  • #2251 của @hemreduru — đang mở

Đá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
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
backend

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
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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của mpdf/mpdf

Tất cả issue của mpdf/mpdf

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.