-- 같은 파싱, 실패를 알리는 방식만 다르다new :: String -> Url -- 실패하면 TypeError 를 던진다canParse :: String -> Bool -- 실패하면 Falseparse :: String -> Maybe Url -- 실패하면 nullparse s = if canParse s then Just (new s) else Nothing -- try…catch 로 감싸던 걸 한 번에new null -- throws TypeErrorcanParse null == Falseparse null == Nothing-- 두 번째 인자 base: 상대 URL 을 해석할 기준. URL 객체도 문자열로 바꿔 받는다
-- URL 의 공백이 %2520 으로 깨져 들어온다 = iOS 17.0·17.1 pasteboard 가 복사할 때 다시 인코딩 (WebKit 261936)-- 인코딩은 멱등이 아니다encode "%20" == "%2520" -- % 가 %25 로decode "%2520" == "%20" -- 한 번 풀어도 %20 이 남으면 이중 인코딩pasteboard :: IOS -> String -> Stringpasteboard v | v `elem` [17.0, 17.1] = encodeInvalid -- %20 을 invalid 로 보고 % 를 다시 인코딩. 한글(%EA%B0%80)은 유효해서 통과 | v < 17 = id -- NSURL URLWithString: 이 nil | otherwise = id -- 17.2 수정-- 추적: 깨진 값 해독 → 경로 좁히기 → 우리 코드 배제cause :: Request -> Causecause r | iOS (ua r) && isNothing (referer r) = Paste -- 클릭(JS redirect)은 pasteboard 를 안 거쳐 정상 | otherwise = Ours -- 링크 생성의 encodeURIComponent 가 한 번인지 본다-- 고칠 수 없으니 막는다redirect :: Path -> Responseredirect p | not (whitelisted p) = pass p | any (`isInfixOf` d) ["..", "//", "\\"] = Forbidden | otherwise = Moved301 (encodeSegments d) where d = decode p-- 무조건 이중 디코딩 금지: decode (decode "..%252f") == "../" — path traversal-- 확인: curl -Iredirect "/a%2520b" == Moved301 "/a%20b"redirect "/..%252f..%252f" == Forbidden-- URL 은 디코딩된 원본으로 보관하고, 인코딩은 출력 직전 한 번-- "방어적으로 한 번 더" 는 멱등이 아닌 연산엔 통하지 않는다