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