|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | + |
| 6 | +@dataclass(frozen=True) |
| 7 | +class LivePolicy: |
| 8 | + kind: str |
| 9 | + reason: str = "" |
| 10 | + verify_with: str = "" |
| 11 | + |
| 12 | + |
| 13 | +SYSTEM_PATHS = { |
| 14 | + "/health", |
| 15 | + "/ready", |
| 16 | + "/metrics", |
| 17 | + "/build", |
| 18 | + "/deps", |
| 19 | +} |
| 20 | + |
| 21 | +SESSION_MUTATIONS = { |
| 22 | + ("POST", "/auth/login"): "/account", |
| 23 | + ("POST", "/auth/login/by/sessionid"): "/account", |
| 24 | + ("PATCH", "/auth/relogin"): "/account", |
| 25 | + ("PATCH", "/auth/settings"): "/auth/settings", |
| 26 | +} |
| 27 | + |
| 28 | +REVERSIBLE_MUTATIONS = { |
| 29 | + ("POST", "/hashtag/follow"): "DELETE /hashtag/follow", |
| 30 | + ("DELETE", "/hashtag/follow"): "GET /hashtag", |
| 31 | + ("POST", "/media/like"): "DELETE /media/like", |
| 32 | + ("DELETE", "/media/like"): "GET /media", |
| 33 | + ("PATCH", "/media/seen"): "GET /media", |
| 34 | + ("POST", "/media/comment"): "GET /media/comments", |
| 35 | + ("DELETE", "/media/comment"): "GET /media/comments", |
| 36 | + ("POST", "/media/comment/check/offensive"): "same response", |
| 37 | + ("POST", "/media/comment/like"): "DELETE /media/comment/like", |
| 38 | + ("DELETE", "/media/comment/like"): "GET /media/comment/likers", |
| 39 | + ("POST", "/media/save"): "DELETE /media/save", |
| 40 | + ("DELETE", "/media/save"): "GET /account/liked/media", |
| 41 | + ("POST", "/note"): "GET /notes", |
| 42 | + ("DELETE", "/note"): "GET /notes", |
| 43 | + ("POST", "/note/music"): "GET /notes", |
| 44 | + ("PATCH", "/notes/last-seen"): "GET /notes", |
| 45 | + ("POST", "/story/like"): "DELETE /story/like", |
| 46 | + ("DELETE", "/story/like"): "GET /story", |
| 47 | + ("PATCH", "/story/seen"): "GET /story", |
| 48 | + ("POST", "/user/follow"): "GET /user/friendship", |
| 49 | + ("DELETE", "/user/follow"): "GET /user/friendship", |
| 50 | + ("DELETE", "/user/follower"): "GET /user/friendship", |
| 51 | + ("POST", "/user/close-friend"): "GET /user/friendship", |
| 52 | + ("DELETE", "/user/close-friend"): "GET /user/friendship", |
| 53 | + ("POST", "/user/mute/posts"): "GET /user/friendship", |
| 54 | + ("DELETE", "/user/mute/posts"): "GET /user/friendship", |
| 55 | + ("POST", "/user/mute/stories"): "GET /user/friendship", |
| 56 | + ("DELETE", "/user/mute/stories"): "GET /user/friendship", |
| 57 | + ("POST", "/user/notifications/posts"): "GET /user/friendship", |
| 58 | + ("DELETE", "/user/notifications/posts"): "GET /user/friendship", |
| 59 | + ("POST", "/user/notifications/stories"): "GET /user/friendship", |
| 60 | + ("DELETE", "/user/notifications/stories"): "GET /user/friendship", |
| 61 | + ("POST", "/user/notifications/reels"): "GET /user/friendship", |
| 62 | + ("DELETE", "/user/notifications/reels"): "GET /user/friendship", |
| 63 | + ("POST", "/user/notifications/videos"): "GET /user/friendship", |
| 64 | + ("DELETE", "/user/notifications/videos"): "GET /user/friendship", |
| 65 | + ("POST", "/user/block"): "GET /user/friendship", |
| 66 | + ("DELETE", "/user/block"): "GET /user/friendship", |
| 67 | +} |
| 68 | + |
| 69 | +UPLOAD_MUTATIONS = { |
| 70 | + ("POST", "/album/upload"): "GET /media", |
| 71 | + ("POST", "/album/upload/with/music"): "GET /media", |
| 72 | + ("POST", "/clip/upload"): "GET /media", |
| 73 | + ("POST", "/clip/upload/by/url"): "GET /media", |
| 74 | + ("POST", "/clip/upload/with/music"): "GET /media", |
| 75 | + ("POST", "/igtv/upload"): "GET /media", |
| 76 | + ("POST", "/igtv/upload/by/url"): "GET /media", |
| 77 | + ("POST", "/photo/upload"): "GET /media", |
| 78 | + ("POST", "/photo/upload/by/url"): "GET /media", |
| 79 | + ("POST", "/photo/upload/with/music"): "GET /media", |
| 80 | + ("POST", "/story/upload"): "GET /story + GET /user/stories + GET /story/download", |
| 81 | + ("POST", "/story/upload/by/url"): "GET /story + GET /user/stories + GET /story/download", |
| 82 | + ("POST", "/video/upload"): "GET /media", |
| 83 | + ("POST", "/video/upload/by/url"): "GET /media", |
| 84 | +} |
| 85 | + |
| 86 | +GUARDED_PREFIX_REASONS = { |
| 87 | + ("/account", "account mutation"): "changes authenticated account state or depends on inbound follow requests", |
| 88 | + ("/auth/challenge", "challenge"): "requires a real active Instagram challenge", |
| 89 | + ("/auth/totp", "totp state"): "changes two-factor authentication state", |
| 90 | + ("/clip/pin", "owned reel"): "requires owned Reel fixture and profile cleanup", |
| 91 | + ("/direct", "direct fixture"): "requires controlled Direct threads, users, and messages", |
| 92 | + ("/highlight", "highlight fixture"): "requires owned story/highlight fixtures", |
| 93 | + ("/media/archive", "owned media"): "requires owned media and archive state cleanup", |
| 94 | + ("/media/livestream", "live broadcast"): "creates or mutates a real livestream", |
| 95 | + ("/media/pin", "owned media"): "requires owned media and visible profile cleanup", |
| 96 | + ("/media/comment/pin", "owned comment"): "requires owned media comments", |
| 97 | + ("/notifications", "account settings"): "changes notification settings on the authenticated account", |
| 98 | +} |
| 99 | + |
| 100 | +GUARDED_EXACT_REASONS = { |
| 101 | + ("DELETE", "/media"): "deletes real media and requires a dedicated upload fixture", |
| 102 | + ("PATCH", "/media"): "edits real media caption and requires a dedicated upload fixture", |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +def operation_policy(method: str, path: str) -> LivePolicy: |
| 107 | + method = method.upper() |
| 108 | + |
| 109 | + if path in SYSTEM_PATHS: |
| 110 | + return LivePolicy("system") |
| 111 | + if method == "GET" and path.endswith(("/download", "/download/by/url", "/download/by/urls")): |
| 112 | + return LivePolicy("download", verify_with="binary/media validation") |
| 113 | + if method == "GET": |
| 114 | + if path.startswith("/auth/"): |
| 115 | + return LivePolicy("session-read") |
| 116 | + return LivePolicy("read") |
| 117 | + |
| 118 | + if (method, path) in SESSION_MUTATIONS: |
| 119 | + return LivePolicy("session", verify_with=SESSION_MUTATIONS[(method, path)]) |
| 120 | + if (method, path) in REVERSIBLE_MUTATIONS: |
| 121 | + return LivePolicy("reversible", verify_with=REVERSIBLE_MUTATIONS[(method, path)]) |
| 122 | + if (method, path) in UPLOAD_MUTATIONS: |
| 123 | + return LivePolicy("upload", verify_with=UPLOAD_MUTATIONS[(method, path)]) |
| 124 | + if (method, path) == ("DELETE", "/story"): |
| 125 | + return LivePolicy("cleanup", verify_with="GET /story returns 404 or missing story") |
| 126 | + |
| 127 | + guarded_reason = GUARDED_EXACT_REASONS.get((method, path)) |
| 128 | + if guarded_reason: |
| 129 | + return LivePolicy("guarded", reason=guarded_reason) |
| 130 | + for prefix, reason in GUARDED_PREFIX_REASONS: |
| 131 | + if path.startswith(prefix): |
| 132 | + return LivePolicy("guarded", reason=reason) |
| 133 | + |
| 134 | + return LivePolicy("unclassified") |
| 135 | + |
| 136 | + |
| 137 | +def guarded_operations() -> dict[tuple[str, str], LivePolicy]: |
| 138 | + from aiograpi_rest.main import app |
| 139 | + |
| 140 | + return { |
| 141 | + (method.upper(), path): policy |
| 142 | + for path, methods in app.openapi()["paths"].items() |
| 143 | + for method in methods |
| 144 | + if (policy := operation_policy(method, path)).kind == "guarded" |
| 145 | + } |
0 commit comments