| ... | ... | @@ -25,23 +25,40 @@ import * as RelayConfiguration from "../Config.ts"; |
| 25 | 25 | export class EnvironmentLinkProofExpired extends Schema.TaggedErrorClass<EnvironmentLinkProofExpired>()( |
| 26 | 26 | "EnvironmentLinkProofExpired", |
| 27 | 27 | { |
| 28 | userId: Schema.String, |
| 29 | environmentId: Schema.String, |
| 28 | 30 | expiresAt: Schema.String, |
| 29 | 31 | }, |
| 30 | 32 | ) { |
| 31 | 33 | override get message(): string { |
| 32 | | return `Environment link proof expired at ${this.expiresAt}`; |
| 34 | return `Environment '${this.environmentId}' link proof expired at ${this.expiresAt}`; |
| 33 | 35 | } |
| 34 | 36 | } |
| 35 | 37 | |
| 36 | 38 | export class EnvironmentLinkProofInvalid extends Schema.TaggedErrorClass<EnvironmentLinkProofInvalid>()( |
| 37 | 39 | "EnvironmentLinkProofInvalid", |
| 38 | 40 | { |
| 41 | userId: Schema.String, |
| 39 | 42 | environmentId: Schema.String, |
| 40 | 43 | reason: RelayEnvironmentLinkProofInvalidReason, |
| 44 | stage: Schema.Literals([ |
| 45 | "decode_token", |
| 46 | "decode_payload", |
| 47 | "verify_proof", |
| 48 | "authorize_capabilities", |
| 49 | "validate_descriptor", |
| 50 | "verify_challenge", |
| 51 | "validate_expiration", |
| 52 | "consume_proof_nonce", |
| 53 | "consume_challenge_nonce", |
| 54 | "validate_origin", |
| 55 | "validate_endpoint", |
| 56 | ]), |
| 57 | cause: Schema.optional(Schema.Defect()), |
| 41 | 58 | }, |
| 42 | 59 | ) { |
| 43 | 60 | override get message(): string { |
| 44 | | return `Environment '${this.environmentId}' link proof is invalid: ${this.reason}`; |
| 61 | return `Environment '${this.environmentId}' link proof is invalid during ${this.stage}: ${this.reason}`; |
| 45 | 62 | } |
| 46 | 63 | } |
| 47 | 64 | |
| ... | ... | @@ -132,20 +149,27 @@ const make = Effect.gen(function* () { |
| 132 | 149 | const nowSeconds = Math.floor(now.epochMilliseconds / 1_000); |
| 133 | 150 | const unverified = yield* Effect.try({ |
| 134 | 151 | try: () => decodeRelayJwt(input.request.proof), |
| 135 | | catch: () => |
| 152 | catch: (cause) => |
| 136 | 153 | new EnvironmentLinkProofInvalid({ |
| 154 | userId: input.userId, |
| 137 | 155 | environmentId: "unknown", |
| 138 | 156 | reason: "invalid_signature_or_scope", |
| 157 | stage: "decode_token", |
| 158 | cause, |
| 139 | 159 | }), |
| 140 | 160 | }); |
| 141 | | const decoded = yield* decodeProof(unverified).pipe(Effect.option); |
| 142 | | if (decoded._tag === "None") { |
| 143 | | return yield* new EnvironmentLinkProofInvalid({ |
| 144 | | environmentId: "unknown", |
| 145 | | reason: "invalid_signature_or_scope", |
| 146 | | }); |
| 147 | | } |
| 148 | | const candidate = decoded.value; |
| 161 | const candidate = yield* decodeProof(unverified).pipe( |
| 162 | Effect.mapError( |
| 163 | (cause) => |
| 164 | new EnvironmentLinkProofInvalid({ |
| 165 | userId: input.userId, |
| 166 | environmentId: "unknown", |
| 167 | reason: "invalid_signature_or_scope", |
| 168 | stage: "decode_payload", |
| 169 | cause, |
| 170 | }), |
| 171 | ), |
| 172 | ); |
| 149 | 173 | yield* Effect.annotateCurrentSpan({ |
| 150 | 174 | "relay.environment_id": candidate.environmentId, |
| 151 | 175 | "relay.link.notifications_enabled": input.request.notificationsEnabled, |
| ... | ... | @@ -154,6 +178,8 @@ const make = Effect.gen(function* () { |
| 154 | 178 | }); |
| 155 | 179 | if (candidate.exp <= nowSeconds) { |
| 156 | 180 | return yield* new EnvironmentLinkProofExpired({ |
| 181 | userId: input.userId, |
| 182 | environmentId: candidate.environmentId, |
| 157 | 183 | expiresAt: DateTime.formatIso(DateTime.makeUnsafe(candidate.exp * 1_000)), |
| 158 | 184 | }); |
| 159 | 185 | } |
| ... | ... | @@ -169,10 +195,13 @@ const make = Effect.gen(function* () { |
| 169 | 195 | }).pipe( |
| 170 | 196 | Effect.flatMap(decodeProof), |
| 171 | 197 | Effect.mapError( |
| 172 | | () => |
| 198 | (cause) => |
| 173 | 199 | new EnvironmentLinkProofInvalid({ |
| 200 | userId: input.userId, |
| 174 | 201 | environmentId: candidate.environmentId, |
| 175 | 202 | reason: "invalid_signature_or_scope", |
| 203 | stage: "verify_proof", |
| 204 | cause, |
| 176 | 205 | }), |
| 177 | 206 | ), |
| 178 | 207 | ); |
| ... | ... | @@ -181,14 +210,18 @@ const make = Effect.gen(function* () { |
| 181 | 210 | !proofAuthorizesRequestedCapabilities(verified, input.request) |
| 182 | 211 | ) { |
| 183 | 212 | return yield* new EnvironmentLinkProofInvalid({ |
| 213 | userId: input.userId, |
| 184 | 214 | environmentId: candidate.environmentId, |
| 185 | 215 | reason: "invalid_signature_or_scope", |
| 216 | stage: "authorize_capabilities", |
| 186 | 217 | }); |
| 187 | 218 | } |
| 188 | 219 | if (verified.descriptor.environmentId !== verified.environmentId) { |
| 189 | 220 | return yield* new EnvironmentLinkProofInvalid({ |
| 221 | userId: input.userId, |
| 190 | 222 | environmentId: verified.environmentId, |
| 191 | 223 | reason: "descriptor_mismatch", |
| 224 | stage: "validate_descriptor", |
| 192 | 225 | }); |
| 193 | 226 | } |
| 194 | 227 | const challenge = yield* relayTokens.verifyLinkChallenge({ |
| ... | ... | @@ -203,15 +236,19 @@ const make = Effect.gen(function* () { |
| 203 | 236 | }); |
| 204 | 237 | if (challenge === null) { |
| 205 | 238 | return yield* new EnvironmentLinkProofInvalid({ |
| 239 | userId: input.userId, |
| 206 | 240 | environmentId: verified.environmentId, |
| 207 | 241 | reason: "challenge_invalid", |
| 242 | stage: "verify_challenge", |
| 208 | 243 | }); |
| 209 | 244 | } |
| 210 | 245 | const expiresAt = DateTime.make(verified.exp * 1_000); |
| 211 | 246 | if (expiresAt._tag === "None") { |
| 212 | 247 | return yield* new EnvironmentLinkProofInvalid({ |
| 248 | userId: input.userId, |
| 213 | 249 | environmentId: verified.environmentId, |
| 214 | 250 | reason: "invalid_signature_or_scope", |
| 251 | stage: "validate_expiration", |
| 215 | 252 | }); |
| 216 | 253 | } |
| 217 | 254 | const consumedNonce = yield* proofReplay.consume({ |
| ... | ... | @@ -222,8 +259,10 @@ const make = Effect.gen(function* () { |
| 222 | 259 | }); |
| 223 | 260 | if (!consumedNonce) { |
| 224 | 261 | return yield* new EnvironmentLinkProofInvalid({ |
| 262 | userId: input.userId, |
| 225 | 263 | environmentId: verified.environmentId, |
| 226 | 264 | reason: "replayed_nonce", |
| 265 | stage: "consume_proof_nonce", |
| 227 | 266 | }); |
| 228 | 267 | } |
| 229 | 268 | const consumedChallenge = yield* proofReplay.consume({ |
| ... | ... | @@ -234,14 +273,18 @@ const make = Effect.gen(function* () { |
| 234 | 273 | }); |
| 235 | 274 | if (!consumedChallenge) { |
| 236 | 275 | return yield* new EnvironmentLinkProofInvalid({ |
| 276 | userId: input.userId, |
| 237 | 277 | environmentId: verified.environmentId, |
| 238 | 278 | reason: "challenge_invalid", |
| 279 | stage: "consume_challenge_nonce", |
| 239 | 280 | }); |
| 240 | 281 | } |
| 241 | 282 | if (input.request.managedTunnelsEnabled && !isLoopbackManagedTunnelOrigin(verified.origin)) { |
| 242 | 283 | return yield* new EnvironmentLinkProofInvalid({ |
| 284 | userId: input.userId, |
| 243 | 285 | environmentId: verified.environmentId, |
| 244 | 286 | reason: "origin_not_allowed", |
| 287 | stage: "validate_origin", |
| 245 | 288 | }); |
| 246 | 289 | } |
| 247 | 290 | const provisioned = input.request.managedTunnelsEnabled |
| ... | ... | @@ -254,8 +297,10 @@ const make = Effect.gen(function* () { |
| 254 | 297 | const endpoint = provisioned?.endpoint ?? verified.endpoint; |
| 255 | 298 | if (!isSecureManagedEndpoint(endpoint)) { |
| 256 | 299 | return yield* new EnvironmentLinkProofInvalid({ |
| 300 | userId: input.userId, |
| 257 | 301 | environmentId: verified.environmentId, |
| 258 | 302 | reason: "endpoint_not_secure", |
| 303 | stage: "validate_endpoint", |
| 259 | 304 | }); |
| 260 | 305 | } |
| 261 | 306 | yield* links.upsert({ ...input, proof: verified, endpoint }); |