fix(training): 支持 HTTP 附件访问地址

This commit is contained in:
2026-10-19 18:12:10 +08:00
parent aaed9da964
commit 091d17ff21
3 changed files with 55 additions and 5 deletions
@@ -207,7 +207,7 @@ final class TrainingRemoteReadingViewModel {
} catch { saveContentError(error, epoch: epoch) }
}
/// 点击附件时取得可直接交给系统浏览器的有效 HTTPS 链接,不附 App Token。
/// 点击附件时取得有效 HTTP/HTTPS 链接,原样交给系统浏览器,不附 App Token。
func authorizeAttachment(id: String) async -> TrainingAttachmentLink? {
guard !isAuthorizingAttachment, let course, !isInvalidated else { return nil }
let epoch = generation
@@ -216,7 +216,9 @@ final class TrainingRemoteReadingViewModel {
do {
let link = try await service.attachmentURL(courseID: course.id, attachmentID: id, taskID: taskID)
try await checkIdentity(epoch)
guard link.url.scheme?.lowercased() == "https", link.url.host != nil, link.expiresAt > now() else {
let scheme = link.url.scheme?.lowercased()
guard scheme == "http" || scheme == "https", link.url.host?.isEmpty == false,
link.url.user == nil, link.url.password == nil, link.expiresAt > now() else {
throw TrainingRemoteError.paramInvalid("附件访问地址无效或已过期,请重试")
}
contentErrorMessage = nil
@@ -117,8 +117,8 @@ final class TrainingCourseRequestLoadingTests: XCTestCase {
XCTAssertEqual(calls.filter { $0.name == "sections" && $0.cursor == "next" }.count, 1)
}
/// 附件第一页、下一页和授权请求显示进度,复用目录及呈现 Safari 前已释放进度。
func testAttachmentLoadingPaginationReuseAndAuthorization() async throws {
/// HTTP 附件分页和授权显示进度,复用目录及呈现 Safari 前已释放进度。
func testHTTPAttachmentLoadingPaginationReuseAndAuthorization() async throws {
let first = TrainingCourseLoadingGate()
let next = TrainingCourseLoadingGate()
let authorization = TrainingCourseLoadingGate()
@@ -135,7 +135,7 @@ final class TrainingCourseRequestLoadingTests: XCTestCase {
}
$0.attachmentURLHandler = { id in
await authorization.wait()
return TrainingAttachmentLink(id: id, name: id, url: URL(string: "https://example.test/training.pdf")!, expiresAt: Date().addingTimeInterval(60))
return TrainingAttachmentLink(id: id, name: id, url: URL(string: "http://example.test/training.pdf?Signature=a%2Bb%3D")!, expiresAt: Date().addingTimeInterval(60))
}
}
let controller = TrainingCourseViewController(taskID: "task_1", service: source)
@@ -361,6 +361,54 @@ struct TrainingRemoteReadingTests {
#expect((await source.calls).filter { $0.name == "attachmentURL" }.count == 1)
}
/// 两种网页协议都能打开,带签名的查询参数不在客户端改写。
@Test("HTTP 和 HTTPS 附件原样返回有效签名地址", arguments: ["http", "https", "HTTP", "HTTPS"])
func attachmentAcceptsWebSchemes(_ scheme: String) async throws {
let url = try #require(URL(string: "\(scheme)://example.test/training.pdf?Expires=1800003600&Signature=a%2Bb%3D"))
let source = source(seconds: 20)
await source.configure { $0.attachmentURLHandler = { id in
TrainingAttachmentLink(id: id, name: "培训.pdf", url: url, expiresAt: TrainingRemoteFixtures.now.addingTimeInterval(60))
} }
let model = model(source)
await model.load()
let link = await model.authorizeAttachment(id: "file_1")
#expect(link?.url.absoluteString == url.absoluteString)
#expect(model.errorMessage == nil)
}
/// 放行 HTTP 不能绕过服务端签名链接的有效期。
@Test("HTTP 和 HTTPS 附件到期或已过期时仍拒绝打开", arguments: ["http", "https"])
func expiredAttachmentIsRejected(_ scheme: String) async throws {
let url = try #require(URL(string: "\(scheme)://example.test/training.pdf"))
let source = source(seconds: 20)
let model = model(source)
await model.load()
for offset: TimeInterval in [0, -1] {
await source.configure { $0.attachmentURLHandler = { id in
TrainingAttachmentLink(id: id, name: "培训.pdf", url: url, expiresAt: TrainingRemoteFixtures.now.addingTimeInterval(offset))
} }
#expect(await model.authorizeAttachment(id: "file_1") == nil)
#expect(model.errorMessage == "附件访问地址无效或已过期,请重试")
}
}
/// 非网页协议、空主机和 URL 内嵌账号均不交给浏览器。
@Test("附件拒绝非网页协议和无效身份地址", arguments: [
"file:///private/training.pdf", "content://app/file", "javascript:alert(1)", "intent://app/file",
"http:///file", "http://user:password@example.test/file", "https://user@example.test/file",
])
func invalidAttachmentIsRejected(_ value: String) async throws {
let url = try #require(URL(string: value))
let source = source(seconds: 20)
await source.configure { $0.attachmentURLHandler = { id in
TrainingAttachmentLink(id: id, name: "培训.pdf", url: url, expiresAt: TrainingRemoteFixtures.now.addingTimeInterval(60))
} }
let model = model(source)
await model.load()
#expect(await model.authorizeAttachment(id: "file_1") == nil)
#expect(model.errorMessage == "附件访问地址无效或已过期,请重试")
}
/// 只读回顾仍能加载正文,但不创建会话、不增加读时。
@Test("已读完的只读课程不新建学习会话")
func readOnlyDoesNotStartSession() async {