oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2
3struct PreviewPass: Codable, Sendable {
4 let pass: String
5 let expiresIn: Int
6 let grantExpiresIn: Int
7 let expiresAt: Date
8 let grantExpiresAt: Date
9}
10extension EquipoClient {
11 func previewPass(task: String, candidate: String) async throws -> PreviewPass {
12 try await post("/tasks/\(escape(task))/preview-pass", try .json(["candidate_sha": candidate]))
13 }
14}
15
16struct PreviewDestination: Equatable, Sendable {
17 let origin: URL
18 let taskID: String
19 let candidate: String
20 let wrapper: URL
21 let exchangeURL: URL
22 init?(origin: URL, taskID: String, candidate: String) {
23 guard origin.scheme == "https", origin.host != nil, origin.user == nil, origin.password == nil,
24 taskID.range(of: #"^[A-Za-z0-9_-]{1,128}\z"#, options: .regularExpression) != nil,
25 candidate.range(of: #"^[0-9a-f]{40}\z"#, options: .regularExpression) != nil,
26 var parts = URLComponents(url: origin, resolvingAgainstBaseURL: false) else { return nil }
27 parts.path = ""; parts.query = nil; parts.fragment = nil
28 guard let base = parts.url else { return nil }
29 self.origin = base; self.taskID = taskID; self.candidate = candidate
30 parts.path = "/tasks/\(taskID)/preview"
31 guard let wrapper = parts.url else { return nil }; self.wrapper = wrapper
32 parts.path = "/native/preview/exchange"
33 guard let exchange = parts.url else { return nil }; self.exchangeURL = exchange
34 }
35 func exchange(pass: String) -> URLRequest {
36 var request = URLRequest(url: exchangeURL)
37 request.httpMethod = "POST"
38 request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
39 request.setValue(origin.absoluteString, forHTTPHeaderField: "Origin")
40 // Every value uses a validated ASCII alphabet, never arbitrary URL text.
41 request.httpBody = Data("pass=\(pass)&task=\(taskID)&candidate=\(candidate)".utf8)
42 request.cachePolicy = .reloadIgnoringLocalCacheData
43 return request
44 }
45 private static func cookieHost(_ url: URL) -> String? {
46 url.host?.lowercased().trimmingCharacters(in: CharacterSet(charactersIn: "."))
47 }
48 func allows(_ request: URLRequest, mainFrame: Bool, initial: Bool) -> Bool {
49 guard let url = request.url, url.user == nil, url.password == nil else { return false }
50 if !mainFrame && (url.absoluteString == "about:blank" || url.absoluteString == "about:srcdoc" || url.scheme == "blob" || url.scheme == "data") { return true }
51 guard url.scheme == "https" else { return false }
52 if !mainFrame {
53 // The response's frame-src CSP pins the preview origin. Never let a
54 // child navigate onto the Equipo cookie host (ports do not isolate cookies).
55 return Self.cookieHost(url) != Self.cookieHost(origin)
56 }
57 if initial && request.httpMethod == "POST" && url == exchangeURL { return true }
58 return request.httpMethod == "GET" && url == wrapper
59 }
60}
61