From a6eced07615e87309c09c68a90e0e11cb40f8fda Mon Sep 17 00:00:00 2001 From: zhengwr Date: Thu, 25 Jun 2026 23:01:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(report):=20pdf/xls=20=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E5=8A=A0=20Content-Disposition=20attachment=20+=20=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 报表查看器 iframe 用 format=html(inline 内嵌);导出按钮用 format=pdf|xls,端点须触发 下载而非内嵌渲染。render() 对 pdf/xls 设 Content-Disposition: attachment;文件名取报表名 (view_template.name,经 RFC5987 filename*=UTF-8'' 编码中文,ASCII 回退 filename="." 供老客户端);html 不设此头(查看器内嵌不变)。 - ReportTemplateRepository.loadReportName(templateId):取 view_template.name 作文件名,最佳努力 (找不到/异常返 null → 回退 templateId,绝不因取名失败影响渲染)。 - contentDisposition(fmt,name,templateId)/sanitizeFilename:纯函数。sanitizeFilename 剔除 CR/LF/引号/反斜杠/正斜杠/控制符,杜绝响应头注入(CRLF 拆头)与 filename 属性逃逸。 filename* 修正为 RFC5987 attr-char 口径(空格→%20、*→%2A)。 - ReportDownloadTest:5 单测(html 无头/pdf RFC5987 中文名/xls 扩展/注入中和/空名回退 templateId)。 真验(curl):pdf/xls 回 attachment + filename*=UTF-8''在建项目收付款信息表.{pdf,xls};html 无 Content-Disposition。 Co-Authored-By: Claude Opus 4.8 (1M context) --- .../platform/report/ReportController.java | 55 +++++++++++++++++- .../report/ReportTemplateRepository.java | 23 ++++++++ .../platform/report/ReportDownloadTest.java | 58 +++++++++++++++++++ 3 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 platform-report/src/test/java/com/eip4/platform/report/ReportDownloadTest.java diff --git a/platform-report/src/main/java/com/eip4/platform/report/ReportController.java b/platform-report/src/main/java/com/eip4/platform/report/ReportController.java index c7cc2f2..8fcda6a 100644 --- a/platform-report/src/main/java/com/eip4/platform/report/ReportController.java +++ b/platform-report/src/main/java/com/eip4/platform/report/ReportController.java @@ -13,6 +13,7 @@ import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -94,9 +95,17 @@ public class ReportController { "读取/渲染报表模板失败: " + e.getMessage(), e); } - return ResponseEntity.ok() - .contentType(MediaType.parseMediaType(fmt.contentType())) - .body(body); + ResponseEntity.BodyBuilder rb = ResponseEntity.ok() + .contentType(MediaType.parseMediaType(fmt.contentType())); + // pdf/xls(导出按钮)→ attachment 触发下载,文件名取报表名;html(查看器 iframe)→ inline 内嵌。 + if (fmt != ReportService.Format.HTML) { + String reportName = templateRepository.loadReportName(templateId); + String disposition = contentDisposition(fmt, reportName, templateId); + if (disposition != null) { + rb.header(HttpHeaders.CONTENT_DISPOSITION, disposition); + } + } + return rb.body(body); } /** @@ -176,6 +185,46 @@ public class ReportController { return env; } + /** + * 构造导出下载的 {@code Content-Disposition} 头值。 + *
    + *
  • {@code html} → {@code null}(查看器 iframe 内嵌,inline);
  • + *
  • {@code pdf}/{@code xls} → {@code attachment},中文报表名经 RFC5987 {@code filename*=UTF-8''} 编码, + * ASCII 回退 {@code filename="."}(UUID 安全,老客户端兜底)。
  • + *
+ * 文件名经 {@link #sanitizeFilename} 净化(剔除 CRLF/引号/斜杠/控制符),杜绝响应头注入与属性逃逸。纯函数,便于单测。 + */ + static String contentDisposition(ReportService.Format fmt, String reportName, String templateId) { + if (fmt == ReportService.Format.HTML) { + return null; + } + String ext = fmt == ReportService.Format.PDF ? "pdf" : "xls"; + String base = sanitizeFilename(reportName); + if (base.isEmpty()) { + base = sanitizeFilename(templateId); + } + if (base.isEmpty()) { + base = "report"; + } + // RFC5987 ext-value:URLEncoder 是 x-www-form-urlencoded(空格→+,且保留 * 不编码), + // 修正成 RFC5987 attr-char 口径——空格→%20、* →%2A(. - _ 本就是合法 attr-char)。 + String encoded = URLEncoder.encode(base + "." + ext, StandardCharsets.UTF_8) + .replace("+", "%20").replace("*", "%2A"); + String ascii = sanitizeFilename(templateId == null ? "" : templateId.replaceAll("[^\\x20-\\x7E]", "_")); + if (ascii.isEmpty()) { + ascii = "report"; + } + return "attachment; filename=\"" + ascii + "." + ext + "\"; filename*=UTF-8''" + encoded; + } + + /** 文件名净化:剔除 CR/LF/引号/反斜杠/正斜杠/控制字符,杜绝响应头注入(CRLF 拆头)与路径/属性逃逸。 */ + private static String sanitizeFilename(String s) { + if (s == null) { + return ""; + } + return s.replaceAll("[\\r\\n\"\\\\/\\x00-\\x1F\\x7F]", "").trim(); + } + /** templateId / ?tpl 解析为模板文件路径。 */ private Path resolveTemplate(String templateId, String tplPath) { Path xml; diff --git a/platform-report/src/main/java/com/eip4/platform/report/ReportTemplateRepository.java b/platform-report/src/main/java/com/eip4/platform/report/ReportTemplateRepository.java index 2fb62c1..446c38a 100644 --- a/platform-report/src/main/java/com/eip4/platform/report/ReportTemplateRepository.java +++ b/platform-report/src/main/java/com/eip4/platform/report/ReportTemplateRepository.java @@ -30,6 +30,10 @@ public class ReportTemplateRepository { private static final String LOAD_SQL = "SELECT xml::text FROM eip3.view_template WHERE trim(view_template_id) = ? AND type = 200"; + /** 报表名(view_template.name),供导出文件名;限定 type=200。 */ + private static final String NAME_SQL = + "SELECT name FROM eip3.view_template WHERE trim(view_template_id) = ? AND type = 200"; + private final JdbcTemplate jdbcTemplate; public ReportTemplateRepository(JdbcTemplate jdbcTemplate) { @@ -61,6 +65,25 @@ public class ReportTemplateRepository { return jor; } + /** + * 报表名(view_template.name),供导出 PDF/XLS 的文件名。最佳努力:找不到/异常返回 {@code null} + * (导出端点回退用 templateId 作文件名,绝不因取名失败影响渲染)。 + */ + public String loadReportName(String templateId) { + String id = templateId == null ? "" : templateId.trim(); + try { + List rows = jdbcTemplate.queryForList(NAME_SQL, String.class, id); + if (rows.isEmpty()) { + return null; + } + String name = rows.get(0); + return name == null || name.isBlank() ? null : name.trim(); + } catch (RuntimeException e) { + log.debug("取报表名失败 templateId={} : {}", id, e.getMessage()); + return null; + } + } + /** * 从 {@code } 外壳里抽出 {@code } 内层 JOR 文档。 * diff --git a/platform-report/src/test/java/com/eip4/platform/report/ReportDownloadTest.java b/platform-report/src/test/java/com/eip4/platform/report/ReportDownloadTest.java new file mode 100644 index 0000000..0a49ea0 --- /dev/null +++ b/platform-report/src/test/java/com/eip4/platform/report/ReportDownloadTest.java @@ -0,0 +1,58 @@ +package com.eip4.platform.report; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * 报表导出下载头(Content-Disposition)的纯逻辑单测。 + * + *

查看器 iframe 用 {@code format=html}(inline,内嵌渲染);导出按钮用 {@code format=pdf|xls} + * → 端点须回 {@code Content-Disposition: attachment} 触发下载,文件名取报表名(中文经 RFC5987 + * {@code filename*=UTF-8''} 编码),ASCII 回退用 templateId(UUID,老客户端兜底)。html 不设此头。 + */ +class ReportDownloadTest { + + @Test + void html_inline_no_disposition() { + assertNull(ReportController.contentDisposition(ReportService.Format.HTML, "在建项目收付款信息表", "tid-1")); + } + + @Test + void pdf_attachment_with_rfc5987_cjk_filename() { + String cd = ReportController.contentDisposition(ReportService.Format.PDF, "在建项目收付款信息表", "5558497f-id"); + assertTrue(cd.startsWith("attachment;"), cd); + // RFC5987 UTF-8 编码的中文名 + .pdf + assertTrue(cd.contains("filename*=UTF-8''"), cd); + assertTrue(cd.contains("%E5%9C%A8%E5%BB%BA"), cd); // 「在建」UTF-8 %xx + assertTrue(cd.contains(".pdf"), cd); + // ASCII 回退用 templateId(UUID 安全) + assertTrue(cd.contains("filename=\"5558497f-id.pdf\""), cd); + } + + @Test + void xls_uses_xls_extension() { + String cd = ReportController.contentDisposition(ReportService.Format.XLS, "报表", "tid"); + assertTrue(cd.contains(".xls"), cd); + assertFalse(cd.contains(".pdf"), cd); + } + + @Test + void header_injection_neutralized() { + // 报表名含 CRLF/引号/路径分隔 → 必须剔除,杜绝响应头注入(CRLF 拆头)与 filename 属性逃逸 + String evil = "a\r\nSet-Cookie: x=1\"/../b"; + String cd = ReportController.contentDisposition(ReportService.Format.PDF, evil, "tid"); + assertFalse(cd.contains("\r"), "不得含 CR: " + cd); + assertFalse(cd.contains("\n"), "不得含 LF: " + cd); + // ASCII filename 用 templateId(UUID),不被恶意报表名污染、无逃逸引号 + assertTrue(cd.contains("filename=\"tid.pdf\""), cd); + } + + @Test + void blank_name_falls_back_to_templateId() { + String cd = ReportController.contentDisposition(ReportService.Format.PDF, " ", "tid-xyz"); + assertTrue(cd.contains("tid-xyz.pdf"), cd); + } +}