@bitkyc08/opencodex 2.99.0 → 2.100.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS_INSTALL.md +19 -1
- package/README.md +37 -33
- package/SPONSORS.md +5 -1
- package/bin/ocx.mjs +1 -1
- package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
- package/gui/dist/assets/App-g3Cm2xhZ.js +59 -0
- package/gui/dist/assets/Tray-BMDkAiXy.js +1 -0
- package/gui/dist/assets/brand-mark-nr3ziR-T.js +1 -0
- package/gui/dist/assets/index-Bziu8gTy.css +2 -0
- package/gui/dist/assets/index-OYzhQUNP.js +94 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-DKCVUUTi.js} +1 -1
- package/gui/dist/classic/assets/App-CgPBKUP-.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-BedmOsE7.js} +1 -1
- package/gui/dist/classic/assets/index-BR-HhNrp.js +94 -0
- package/gui/dist/classic/assets/quota-summary-CXKhvSmQ.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +215 -0
- package/native/ocx-child/Cargo.toml +31 -0
- package/native/ocx-child/src/cli.rs +1901 -0
- package/native/ocx-child/src/codex_home.rs +429 -0
- package/native/ocx-child/src/codex_settings.rs +231 -0
- package/native/ocx-child/src/daemon.rs +363 -0
- package/native/ocx-child/src/http_wire.rs +619 -0
- package/native/ocx-child/src/journal.rs +977 -0
- package/native/ocx-child/src/lib.rs +22 -0
- package/native/ocx-child/src/main.rs +9 -0
- package/native/ocx-child/src/parity.rs +102 -0
- package/native/ocx-child/src/platform.rs +1487 -0
- package/native/ocx-child/src/relay.rs +1288 -0
- package/native/ocx-child/src/relay_auth.rs +166 -0
- package/native/ocx-child/src/routes.rs +236 -0
- package/native/ocx-child/src/state.rs +190 -0
- package/native/ocx-child/src/test_hooks.rs +58 -0
- package/native/ocx-child/src/toml_surgeon.rs +1156 -0
- package/package.json +8 -2
- package/src/adapters/anthropic-image-normalize.ts +1 -1
- package/src/adapters/anthropic.ts +28 -2
- package/src/adapters/claude-cli/adapter.ts +5 -25
- package/src/adapters/claude-cli/instruction-images.ts +31 -0
- package/src/adapters/claude-cli/stable-replay.ts +42 -14
- package/src/adapters/claude-cli/usage-admission.ts +53 -64
- package/src/adapters/client-fingerprint.ts +1 -1
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/devin/live-models.ts +3 -0
- package/src/adapters/google-http.ts +3 -0
- package/src/adapters/google-vertex-key-project.ts +10 -2
- package/src/adapters/google.ts +40 -8
- package/src/adapters/kiro/usage.ts +3 -13
- package/src/adapters/openai-chat/mistral-image-limit.ts +28 -15
- package/src/adapters/openai-chat-images.ts +100 -0
- package/src/adapters/openai-chat.ts +6 -9
- package/src/adapters/openai-responses/passthrough.ts +5 -2
- package/src/adapters/openai-responses/request-strips.ts +32 -0
- package/src/adapters/openai-responses/tool-schema.ts +9 -4
- package/src/chat/inbound.ts +1 -1
- package/src/claude/auth-detect.ts +26 -8
- package/src/claude/inbound-content-options.ts +18 -3
- package/src/claude/inbound.ts +5 -2
- package/src/cli/bundled-bun-diagnostic.ts +91 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-observe-system.ts +60 -0
- package/src/cli/combo-input.ts +6 -0
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +4 -0
- package/src/cli/config-command.ts +16 -2
- package/src/cli/connect.ts +12 -10
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/doctor.ts +11 -0
- package/src/cli/help.ts +1 -0
- package/src/cli/init.ts +4 -0
- package/src/cli/link-enrollment.ts +152 -25
- package/src/cli/link-local-control.ts +49 -0
- package/src/cli/link.ts +37 -10
- package/src/cli/provider-lifecycle-runtime.ts +4 -2
- package/src/cli/registry.ts +13 -0
- package/src/cli/star-prompt.ts +2 -0
- package/src/cli/status.ts +1 -1
- package/src/cli/telemetry-profile.ts +206 -0
- package/src/cli/telemetry.ts +260 -0
- package/src/client/hub-relay.ts +26 -0
- package/src/client/machine-api.ts +3 -0
- package/src/clients/config-export/t3code.ts +44 -5
- package/src/clients/omo-role-models.ts +1 -1
- package/src/codex/catalog/bundled.ts +44 -2
- package/src/codex/runtime.ts +12 -0
- package/src/codex/shim-restore-lock.ts +67 -30
- package/src/combos/types.ts +21 -1
- package/src/config/atomic-write.ts +24 -1
- package/src/config/diagnostics.ts +15 -0
- package/src/config/live-reconcile.ts +17 -0
- package/src/config/load-degrade.ts +16 -0
- package/src/config/provider-validation.ts +29 -0
- package/src/config/schema/config-schema.ts +24 -0
- package/src/config/schema/leaf-validators.ts +17 -0
- package/src/config/telemetry-carry.ts +30 -0
- package/src/config.ts +13 -1
- package/src/generated/compatibility-version.json +422 -170
- package/src/generated/model-metadata.ts +3 -3
- package/src/integrations/aside-profiles.ts +7 -1
- package/src/integrations/owned-refresh.ts +9 -2
- package/src/lab/artifacts/secure-fs.ts +12 -11
- package/src/lab/fabric/scratch.ts +11 -9
- package/src/lab/public/community.ts +2 -3
- package/src/lab/public/file-safety.ts +18 -20
- package/src/lab/public/mutation-lock.ts +109 -337
- package/src/lab/public/private-file.ts +4 -3
- package/src/lib/bun-runtime-preflight.ts +1 -1
- package/src/lib/codex-catalog-read-only.ts +122 -0
- package/src/lib/config-ownership.ts +56 -18
- package/src/lib/errors.ts +11 -0
- package/src/lib/gcp-adc.ts +167 -48
- package/src/lib/jsonc.ts +4 -2
- package/src/lib/local-control-socket.ts +88 -0
- package/src/lib/privacy.ts +15 -0
- package/src/lib/provider-client-headers.ts +3 -0
- package/src/lib/token-estimate.ts +13 -2
- package/src/link/child-binary-source.ts +228 -0
- package/src/link/ephemeral-child.ts +45 -0
- package/src/link/remote-plant.ts +306 -0
- package/src/link/remote-windows.ts +59 -5
- package/src/link/ssh-argv.ts +71 -8
- package/src/link/status-projection.ts +2 -0
- package/src/link/store.ts +5 -1
- package/src/oauth/anthropic-model-quota.ts +12 -0
- package/src/oauth/anthropic-routing.ts +11 -4
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/kiro-iam-identity-center.ts +4 -1
- package/src/oauth/local-token-detect.ts +14 -17
- package/src/protocols/envelope.ts +20 -2
- package/src/providers/api-key-selection.ts +8 -2
- package/src/providers/claude-cli-identity.ts +9 -3
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/claude-cli-usage.ts +3 -2
- package/src/providers/github-copilot-auto.ts +25 -5
- package/src/providers/meta-muse-plan-refusal.ts +94 -0
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/quota/anthropic-family-headers.ts +8 -1
- package/src/providers/quota/anthropic-usage-windows.ts +49 -2
- package/src/providers/quota/vendor-probes-key.ts +23 -4
- package/src/providers/quota/vendor-probes-oauth.ts +59 -0
- package/src/providers/quota/workbuddy.ts +2 -2
- package/src/providers/quota-types.ts +7 -0
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +42 -8
- package/src/providers/registry/model-ids.ts +1 -0
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/registry/types.ts +6 -0
- package/src/providers/resolved-model-policy.ts +4 -0
- package/src/providers/routed-config.ts +30 -3
- package/src/providers/stale-no-vision-seed-migration.ts +7 -0
- package/src/providers/vertex-environment-authority.ts +50 -0
- package/src/providers/workbuddy-credit-balance.ts +38 -6
- package/src/quota-pool/assemble.ts +14 -6
- package/src/responses/retained-compaction.ts +31 -5
- package/src/router.ts +1 -0
- package/src/server/adapter-resolve.ts +11 -6
- package/src/server/audio-dictation.ts +21 -7
- package/src/server/auth-cors.ts +19 -0
- package/src/server/background-lifecycle.ts +71 -0
- package/src/server/chat-completions.ts +2 -0
- package/src/server/claude-messages.ts +30 -3
- package/src/server/index/live-sideband.ts +15 -1
- package/src/server/index/local-control-listener.ts +515 -0
- package/src/server/index/optional-listeners.ts +21 -1
- package/src/server/index/serve-options.ts +4 -3
- package/src/server/index/websocket-handler.ts +2 -0
- package/src/server/index.ts +3 -0
- package/src/server/management/combo-routes.ts +6 -1
- package/src/server/management/context.ts +1 -0
- package/src/server/management/link-child-preflight.ts +74 -0
- package/src/server/management/link-enrollment.ts +190 -25
- package/src/server/management/link-route-state.ts +6 -1
- package/src/server/management/link-routes.ts +27 -9
- package/src/server/management/model-rows.ts +4 -0
- package/src/server/management/oauth-account-routes.ts +9 -4
- package/src/server/management/provider-overwrite-carry.ts +9 -2
- package/src/server/management/provider-routes.ts +14 -0
- package/src/server/management/provider-thinking-display-patch.ts +54 -0
- package/src/server/management/route-registry.ts +7 -0
- package/src/server/management/session-routes.ts +4 -1
- package/src/server/management/telemetry-routes.ts +37 -0
- package/src/server/management/usage-aggregate-cache.ts +290 -67
- package/src/server/management-api.ts +11 -2
- package/src/server/messages-native-preflight.ts +166 -0
- package/src/server/messages-native.ts +143 -43
- package/src/server/messages-shared.ts +14 -0
- package/src/server/proxy-liveness.ts +99 -6
- package/src/server/request-log-conversation.ts +18 -2
- package/src/server/request-log.ts +7 -2
- package/src/server/responses/adapter-continuation.ts +1 -0
- package/src/server/responses/adapter-dispatch.ts +11 -28
- package/src/server/responses/adapter-failure-message.ts +68 -0
- package/src/server/responses/combo-stream-preflight.ts +14 -2
- package/src/server/responses/compact.ts +13 -1
- package/src/server/responses/core-auth.ts +26 -5
- package/src/server/responses/core-codex-account.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +6 -1
- package/src/server/responses/core-combo-native.ts +150 -37
- package/src/server/responses/core-combo.ts +15 -9
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -1
- package/src/server/responses/core-pipeline.ts +21 -1
- package/src/server/responses/fetch-helpers.ts +5 -0
- package/src/server/responses/opengateway-completion-guard.ts +7 -3
- package/src/server/responses/passthrough-delivery.ts +8 -1
- package/src/server/responses/passthrough-error.ts +22 -0
- package/src/server/responses/request-prepare.ts +7 -1
- package/src/server/responses/request-transport.ts +28 -5
- package/src/server/responses/run-turn-execution.ts +13 -0
- package/src/server/sse-frame-buffer.ts +5 -0
- package/src/server/ws-bridge.ts +10 -0
- package/src/service/launchd.ts +78 -4
- package/src/telemetry/aggregate.ts +348 -0
- package/src/telemetry/consent.ts +128 -0
- package/src/telemetry/contract.ts +403 -0
- package/src/telemetry/disclosure.ts +19 -0
- package/src/telemetry/install-id.ts +226 -0
- package/src/telemetry/payload.ts +129 -0
- package/src/telemetry/profile/client.ts +150 -0
- package/src/telemetry/profile/contract.ts +288 -0
- package/src/telemetry/profile/disclosure.ts +14 -0
- package/src/telemetry/profile/paths.ts +13 -0
- package/src/telemetry/profile/routes.ts +99 -0
- package/src/telemetry/profile/service.ts +234 -0
- package/src/telemetry/profile/state.ts +189 -0
- package/src/telemetry/profile/sync.ts +276 -0
- package/src/telemetry/routes.ts +183 -0
- package/src/telemetry/scheduler.ts +386 -0
- package/src/telemetry/sender.ts +97 -0
- package/src/telemetry/state.ts +334 -0
- package/src/types/combo-jev-effort-mode.ts +11 -0
- package/src/types/config.ts +3 -0
- package/src/types/provider.ts +11 -1
- package/src/types/request.ts +2 -0
- package/src/types/telemetry.ts +15 -0
- package/src/types/wire.ts +10 -0
- package/src/types.ts +5 -0
- package/src/update/index.ts +7 -1
- package/src/update/minisign.d.mts +17 -0
- package/src/update/minisign.mjs +88 -0
- package/src/update/signed-supply.d.mts +59 -0
- package/src/update/signed-supply.mjs +289 -0
- package/src/update/trust-root.json +38 -0
- package/src/update/update-bounded-json.d.mts +4 -0
- package/src/update/update-bounded-json.mjs +113 -0
- package/src/update/update-bytes.d.mts +2 -0
- package/src/update/update-bytes.mjs +23 -0
- package/src/update/update-candidate-id.d.mts +2 -0
- package/src/update/update-candidate-id.mjs +23 -0
- package/src/update/update-checkpoint-codec.d.mts +33 -0
- package/src/update/update-checkpoint-codec.mjs +217 -0
- package/src/update/update-contracts.d.mts +24 -0
- package/src/update/update-contracts.mjs +207 -0
- package/src/update/update-signature-threshold.d.mts +12 -0
- package/src/update/update-signature-threshold.mjs +47 -0
- package/src/update/update-signed-version.d.mts +5 -0
- package/src/update/update-signed-version.mjs +25 -0
- package/src/update/update-trust-anchor.d.mts +2 -0
- package/src/update/update-trust-anchor.mjs +2 -0
- package/src/usage/cache-diagnostic.ts +195 -50
- package/src/usage/expected-prices.ts +20 -2
- package/src/usage/filter-index.ts +1469 -0
- package/src/usage/ledger-scanner.ts +245 -4
- package/src/usage/source-filter.ts +10 -2
- package/src/usage/summary.ts +16 -0
- package/src/web-search/parse.ts +265 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/brand-mark-wzPe3R-J.js +0 -1
- package/gui/dist/assets/index-C-9X2yaI.js +0 -94
- package/gui/dist/assets/index-bnUWypyz.css +0 -2
- package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
- package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
- package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
use hmac::{Hmac, Mac};
|
|
2
|
+
use sha2::Sha256;
|
|
3
|
+
|
|
4
|
+
use crate::platform::{
|
|
5
|
+
constant_time_eq, hex_decode_lower, hex_encode, random_hex, sha256_hex, volatile_zero_bytes,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
pub const LINK_RELAY_AUTH_PATH: &str = "/.well-known/opencodex/link-relay-auth";
|
|
9
|
+
pub const LINK_RELAY_SESSION_HEADER: &str = "x-opencodex-link-session";
|
|
10
|
+
pub const LINK_RELAY_PROBE_HEADER: &str = "x-opencodex-link-probe";
|
|
11
|
+
pub const LINK_RELAY_PROOF_HEADER: &str = "x-opencodex-link-proof";
|
|
12
|
+
pub const LINK_RELAY_CONNECTION_HEADER: &str = "x-opencodex-link-connection";
|
|
13
|
+
pub const LINK_RELAY_CONNECTION_PROOF_HEADER: &str = "x-opencodex-link-connection-proof";
|
|
14
|
+
pub const LINK_RELAY_AUTH_VERSION: &str = "2";
|
|
15
|
+
|
|
16
|
+
type HmacSha256 = Hmac<Sha256>;
|
|
17
|
+
|
|
18
|
+
pub fn compute_child_shutdown_proof(secret: &str, role: &str, binding: &str) -> Option<String> {
|
|
19
|
+
if secret.len() != 48
|
|
20
|
+
|| !secret
|
|
21
|
+
.bytes()
|
|
22
|
+
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
|
23
|
+
|| (role != "caller" && role != "listener")
|
|
24
|
+
|| binding.len() != 128
|
|
25
|
+
|| !binding
|
|
26
|
+
.bytes()
|
|
27
|
+
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
|
28
|
+
{
|
|
29
|
+
return None;
|
|
30
|
+
}
|
|
31
|
+
let mut key = hex_decode_lower(secret)?;
|
|
32
|
+
let mac = HmacSha256::new_from_slice(&key);
|
|
33
|
+
volatile_zero_bytes(&mut key);
|
|
34
|
+
let mut mac = mac.ok()?;
|
|
35
|
+
mac.update(b"opencodex-child-shutdown-v1\0");
|
|
36
|
+
mac.update(role.as_bytes());
|
|
37
|
+
mac.update(b"\0");
|
|
38
|
+
mac.update(binding.as_bytes());
|
|
39
|
+
Some(hex_encode(&mac.finalize().into_bytes()))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
pub fn token_fingerprint_hex(raw_token: &str) -> String {
|
|
43
|
+
sha256_hex(raw_token.as_bytes())
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
pub fn is_valid_link_id(link_id: &str) -> bool {
|
|
47
|
+
link_id.len() == 20
|
|
48
|
+
&& link_id.starts_with("lnk_")
|
|
49
|
+
&& link_id[4..]
|
|
50
|
+
.bytes()
|
|
51
|
+
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
pub fn is_valid_key_id(key_id: &str) -> bool {
|
|
55
|
+
let bytes = key_id.as_bytes();
|
|
56
|
+
if bytes.is_empty() || bytes.len() > 256 {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if !bytes[0].is_ascii_alphanumeric() {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
bytes[1..]
|
|
63
|
+
.iter()
|
|
64
|
+
.all(|&b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'.' | b':' | b'-'))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
pub fn is_valid_hex64(value: &str) -> bool {
|
|
68
|
+
value.len() == 64
|
|
69
|
+
&& value
|
|
70
|
+
.bytes()
|
|
71
|
+
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
pub fn is_valid_connection_generation(value: &str) -> bool {
|
|
75
|
+
!value.is_empty() && value.len() <= 256 && value.bytes().all(|b| (0x21..=0x7e).contains(&b))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// Computes `linkRelayProof` using the 32 raw bytes decoded from `fingerprint_hex`.
|
|
79
|
+
pub fn compute_relay_proof(
|
|
80
|
+
fingerprint_hex: &str,
|
|
81
|
+
direction: &str,
|
|
82
|
+
key_id: &str,
|
|
83
|
+
link_id: &str,
|
|
84
|
+
nonce: &str,
|
|
85
|
+
) -> Option<String> {
|
|
86
|
+
if (direction != "caller" && direction != "listener")
|
|
87
|
+
|| !is_valid_hex64(fingerprint_hex)
|
|
88
|
+
|| !is_valid_key_id(key_id)
|
|
89
|
+
|| !is_valid_link_id(link_id)
|
|
90
|
+
|| !is_valid_hex64(nonce)
|
|
91
|
+
{
|
|
92
|
+
return None;
|
|
93
|
+
}
|
|
94
|
+
let key_bytes = hex_decode_lower(fingerprint_hex)?; // 32 raw bytes
|
|
95
|
+
let mut mac = HmacSha256::new_from_slice(&key_bytes).ok()?;
|
|
96
|
+
mac.update(b"opencodex-link-relay-v2\0");
|
|
97
|
+
mac.update(direction.as_bytes());
|
|
98
|
+
mac.update(b"\0");
|
|
99
|
+
mac.update(key_id.as_bytes());
|
|
100
|
+
mac.update(b"\0");
|
|
101
|
+
mac.update(link_id.as_bytes());
|
|
102
|
+
mac.update(b"\0");
|
|
103
|
+
mac.update(nonce.as_bytes());
|
|
104
|
+
Some(hex_encode(&mac.finalize().into_bytes()))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
pub fn compute_connection_proof(
|
|
108
|
+
fingerprint_hex: &str,
|
|
109
|
+
key_id: &str,
|
|
110
|
+
link_id: &str,
|
|
111
|
+
nonce: &str,
|
|
112
|
+
generation: &str,
|
|
113
|
+
) -> Option<String> {
|
|
114
|
+
if !is_valid_connection_generation(generation) {
|
|
115
|
+
return None;
|
|
116
|
+
}
|
|
117
|
+
let _ = compute_relay_proof(fingerprint_hex, "listener", key_id, link_id, nonce)?;
|
|
118
|
+
let key_bytes = hex_decode_lower(fingerprint_hex)?;
|
|
119
|
+
let mut mac = HmacSha256::new_from_slice(&key_bytes).ok()?;
|
|
120
|
+
mac.update(b"opencodex-link-connection-v1\0");
|
|
121
|
+
mac.update(key_id.as_bytes());
|
|
122
|
+
mac.update(b"\0");
|
|
123
|
+
mac.update(link_id.as_bytes());
|
|
124
|
+
mac.update(b"\0");
|
|
125
|
+
mac.update(nonce.as_bytes());
|
|
126
|
+
mac.update(b"\0");
|
|
127
|
+
mac.update(generation.as_bytes());
|
|
128
|
+
Some(hex_encode(&mac.finalize().into_bytes()))
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
pub fn relay_proof_matches(actual: Option<&str>, expected: Option<&str>) -> bool {
|
|
132
|
+
let (Some(a), Some(e)) = (actual, expected) else {
|
|
133
|
+
return false;
|
|
134
|
+
};
|
|
135
|
+
if !is_valid_hex64(a) || !is_valid_hex64(e) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
let (Some(ab), Some(eb)) = (hex_decode_lower(a), hex_decode_lower(e)) else {
|
|
139
|
+
return false;
|
|
140
|
+
};
|
|
141
|
+
constant_time_eq(&ab, &eb)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
pub struct RelayChallenge {
|
|
145
|
+
pub nonce: String,
|
|
146
|
+
pub caller_proof: String,
|
|
147
|
+
pub expected_listener_proof: String,
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
pub fn create_relay_challenge(
|
|
151
|
+
fingerprint_hex: &str,
|
|
152
|
+
key_id: &str,
|
|
153
|
+
link_id: &str,
|
|
154
|
+
) -> Result<RelayChallenge, String> {
|
|
155
|
+
let nonce = random_hex(32)?;
|
|
156
|
+
let caller_proof = compute_relay_proof(fingerprint_hex, "caller", key_id, link_id, &nonce)
|
|
157
|
+
.ok_or_else(|| "invalid link relay identity".to_owned())?;
|
|
158
|
+
let expected_listener_proof =
|
|
159
|
+
compute_relay_proof(fingerprint_hex, "listener", key_id, link_id, &nonce)
|
|
160
|
+
.ok_or_else(|| "invalid link relay identity".to_owned())?;
|
|
161
|
+
Ok(RelayChallenge {
|
|
162
|
+
nonce,
|
|
163
|
+
caller_proof,
|
|
164
|
+
expected_listener_proof,
|
|
165
|
+
})
|
|
166
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
2
|
+
pub enum RelayAuthKind {
|
|
3
|
+
Bearer,
|
|
4
|
+
ApiKeyHeader,
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
8
|
+
pub enum RouteDecision {
|
|
9
|
+
LocalHealthz,
|
|
10
|
+
LocalReadyz,
|
|
11
|
+
LocalShutdown,
|
|
12
|
+
Reject403Origin,
|
|
13
|
+
Reject426Websocket,
|
|
14
|
+
Reject404,
|
|
15
|
+
RelayData { auth: RelayAuthKind },
|
|
16
|
+
RelayManagement,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const CONTEXT_PATHS: &[&str] = &[
|
|
20
|
+
"/v1/alpha/history/v2/list_windows",
|
|
21
|
+
"/v1/alpha/history/v2/list_items",
|
|
22
|
+
"/v1/alpha/history/v2/read_item",
|
|
23
|
+
"/v1/alpha/history/v2/search_contents",
|
|
24
|
+
"/v1/alpha/notes/v2/thread_hint",
|
|
25
|
+
"/v1/alpha/notes/v2/list_files_by_prefix",
|
|
26
|
+
"/v1/alpha/notes/v2/read_file",
|
|
27
|
+
"/v1/alpha/notes/v2/search_contents",
|
|
28
|
+
"/v1/alpha/notes/v2/append_to_file",
|
|
29
|
+
"/v1/alpha/notes/v2/write_file",
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const POST_DATA_PATHS: &[&str] = &[
|
|
33
|
+
"/v1/responses",
|
|
34
|
+
"/v1/responses/compact",
|
|
35
|
+
"/v1/messages",
|
|
36
|
+
"/v1/messages/count_tokens",
|
|
37
|
+
"/v1/chat/completions",
|
|
38
|
+
"/v1/audio/transcriptions",
|
|
39
|
+
"/v1/alpha/search",
|
|
40
|
+
"/v1/images/generations",
|
|
41
|
+
"/v1/images/edits",
|
|
42
|
+
"/v1/live",
|
|
43
|
+
"/v1/realtime/calls",
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
const LINK_MANAGEMENT_READ_PATHS: &[&str] = &[
|
|
47
|
+
"/api/system/health",
|
|
48
|
+
"/api/startup-health",
|
|
49
|
+
"/api/providers",
|
|
50
|
+
"/api/settings",
|
|
51
|
+
"/api/usage",
|
|
52
|
+
"/api/effort-caps",
|
|
53
|
+
"/api/injection-model",
|
|
54
|
+
"/api/shadow-call-settings",
|
|
55
|
+
"/api/sidecar-settings",
|
|
56
|
+
"/api/v2",
|
|
57
|
+
"/api/provider-quotas",
|
|
58
|
+
"/api/quota-pool",
|
|
59
|
+
"/api/fallback",
|
|
60
|
+
"/api/codex-auth/active",
|
|
61
|
+
"/api/codex-auth/accounts",
|
|
62
|
+
"/api/oauth/accounts",
|
|
63
|
+
"/api/providers/keys",
|
|
64
|
+
"/api/oauth/providers",
|
|
65
|
+
"/api/catalog",
|
|
66
|
+
"/api/system/memory",
|
|
67
|
+
"/api/provider-presets",
|
|
68
|
+
"/api/provider-context-caps",
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
const FORBIDDEN_MANAGEMENT_QUERY_KEYS: &[&str] = &["refresh", "reload", "force", "quota"];
|
|
72
|
+
|
|
73
|
+
pub fn link_data_route_allowed(method: &str, path: &str, has_upgrade: bool) -> bool {
|
|
74
|
+
if has_upgrade {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
if path == "/readyz" {
|
|
78
|
+
return method == "GET";
|
|
79
|
+
}
|
|
80
|
+
if path == "/v1/catalog" || path == "/v1/hub-state" {
|
|
81
|
+
return method == "GET" || method == "HEAD";
|
|
82
|
+
}
|
|
83
|
+
if path == "/v1/usage" || path == "/v1/models" {
|
|
84
|
+
return method == "GET";
|
|
85
|
+
}
|
|
86
|
+
if POST_DATA_PATHS.contains(&path) || CONTEXT_PATHS.contains(&path) {
|
|
87
|
+
return method == "POST";
|
|
88
|
+
}
|
|
89
|
+
method == "GET" && path.starts_with("/v1/opencodex/artifacts/")
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
pub fn link_management_route_allowed(
|
|
93
|
+
method: &str,
|
|
94
|
+
path: &str,
|
|
95
|
+
query: &str,
|
|
96
|
+
has_upgrade: bool,
|
|
97
|
+
) -> bool {
|
|
98
|
+
if has_upgrade {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if path == "/v1/link-management/codex-settings" {
|
|
102
|
+
return method == "GET" && query.is_empty();
|
|
103
|
+
}
|
|
104
|
+
let Some(suffix) = path.strip_prefix("/v1/link-management") else {
|
|
105
|
+
return false;
|
|
106
|
+
};
|
|
107
|
+
if (method != "GET" && method != "HEAD") || !LINK_MANAGEMENT_READ_PATHS.contains(&suffix) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
let raw_q = query.strip_prefix('?').unwrap_or(query);
|
|
111
|
+
if raw_q.is_empty() {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
for pair in raw_q.split('&') {
|
|
115
|
+
let key = pair.split('=').next().unwrap_or("").to_ascii_lowercase();
|
|
116
|
+
if FORBIDDEN_MANAGEMENT_QUERY_KEYS.contains(&key.as_str()) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
true
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
fn is_loopback_hostname(host: &str) -> bool {
|
|
124
|
+
matches!(
|
|
125
|
+
host.to_ascii_lowercase().as_str(),
|
|
126
|
+
"127.0.0.1" | "localhost" | "[::1]" | "::1"
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fn parse_authority_hostname(authority: &str) -> Option<&str> {
|
|
131
|
+
let trimmed = authority.trim();
|
|
132
|
+
if trimmed.is_empty() || trimmed.contains('/') || trimmed.contains('@') || trimmed.contains(' ')
|
|
133
|
+
{
|
|
134
|
+
return None;
|
|
135
|
+
}
|
|
136
|
+
if let Some(rest) = trimmed.strip_prefix('[') {
|
|
137
|
+
let end = rest.find(']')?;
|
|
138
|
+
let tail = &rest[end + 1..];
|
|
139
|
+
if !tail.is_empty() {
|
|
140
|
+
let port = tail.strip_prefix(':')?;
|
|
141
|
+
if port.is_empty() || !port.bytes().all(|b| b.is_ascii_digit()) {
|
|
142
|
+
return None;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return Some(&trimmed[..=end + 1]);
|
|
146
|
+
}
|
|
147
|
+
if let Some((h, p)) = trimmed.rsplit_once(':') {
|
|
148
|
+
if h.contains(':')
|
|
149
|
+
|| h.is_empty()
|
|
150
|
+
|| (!p.is_empty() && !p.bytes().all(|b| b.is_ascii_digit()))
|
|
151
|
+
{
|
|
152
|
+
return None;
|
|
153
|
+
}
|
|
154
|
+
return Some(h);
|
|
155
|
+
}
|
|
156
|
+
Some(trimmed)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/// Mirrors `isAllowedRequestOrigin` (`src/server/auth-cors.ts:83-125`) for loopback listeners.
|
|
160
|
+
pub fn is_allowed_loopback_request_origin(
|
|
161
|
+
host_header: Option<&str>,
|
|
162
|
+
origin_header: Option<&str>,
|
|
163
|
+
) -> bool {
|
|
164
|
+
if let Some(host_val) = host_header
|
|
165
|
+
&& let Some(hostname) = parse_authority_hostname(host_val)
|
|
166
|
+
&& !is_loopback_hostname(hostname)
|
|
167
|
+
{
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
let Some(origin) = origin_header else {
|
|
171
|
+
return true;
|
|
172
|
+
};
|
|
173
|
+
let trimmed = origin.trim();
|
|
174
|
+
let rest = if let Some(r) = trimmed.strip_prefix("http://") {
|
|
175
|
+
r
|
|
176
|
+
} else if let Some(r) = trimmed.strip_prefix("https://") {
|
|
177
|
+
r
|
|
178
|
+
} else {
|
|
179
|
+
return false;
|
|
180
|
+
};
|
|
181
|
+
let authority = rest.split('/').next().unwrap_or("");
|
|
182
|
+
let Some(hostname) = parse_authority_hostname(authority) else {
|
|
183
|
+
return false;
|
|
184
|
+
};
|
|
185
|
+
is_loopback_hostname(hostname)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
pub fn classify_child_ingress(
|
|
189
|
+
method: &str,
|
|
190
|
+
path: &str,
|
|
191
|
+
query: &str,
|
|
192
|
+
host_header: Option<&str>,
|
|
193
|
+
origin_header: Option<&str>,
|
|
194
|
+
upgrade_header: Option<&str>,
|
|
195
|
+
) -> RouteDecision {
|
|
196
|
+
if method == "POST" && path == "/__ocx_child/shutdown" && upgrade_header.is_none() {
|
|
197
|
+
return RouteDecision::LocalShutdown;
|
|
198
|
+
}
|
|
199
|
+
if let Some(upgrade) = upgrade_header {
|
|
200
|
+
if path == "/v1/responses" && upgrade.trim().eq_ignore_ascii_case("websocket") {
|
|
201
|
+
if !is_allowed_loopback_request_origin(host_header, origin_header) {
|
|
202
|
+
return RouteDecision::Reject403Origin;
|
|
203
|
+
}
|
|
204
|
+
return RouteDecision::Reject426Websocket;
|
|
205
|
+
}
|
|
206
|
+
return RouteDecision::Reject404;
|
|
207
|
+
}
|
|
208
|
+
if path == "/healthz" && method == "GET" {
|
|
209
|
+
return RouteDecision::LocalHealthz;
|
|
210
|
+
}
|
|
211
|
+
if path == "/readyz" {
|
|
212
|
+
return if method == "GET" {
|
|
213
|
+
RouteDecision::LocalReadyz
|
|
214
|
+
} else {
|
|
215
|
+
RouteDecision::Reject404
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
if link_data_route_allowed(method, path, false) {
|
|
219
|
+
if !is_allowed_loopback_request_origin(host_header, origin_header) {
|
|
220
|
+
return RouteDecision::Reject403Origin;
|
|
221
|
+
}
|
|
222
|
+
let auth = if path == "/v1/usage" {
|
|
223
|
+
RelayAuthKind::ApiKeyHeader
|
|
224
|
+
} else {
|
|
225
|
+
RelayAuthKind::Bearer
|
|
226
|
+
};
|
|
227
|
+
return RouteDecision::RelayData { auth };
|
|
228
|
+
}
|
|
229
|
+
if link_management_route_allowed(method, path, query, false) {
|
|
230
|
+
if !is_allowed_loopback_request_origin(host_header, origin_header) {
|
|
231
|
+
return RouteDecision::Reject403Origin;
|
|
232
|
+
}
|
|
233
|
+
return RouteDecision::RelayManagement;
|
|
234
|
+
}
|
|
235
|
+
RouteDecision::Reject404
|
|
236
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
use std::fs;
|
|
2
|
+
use std::path::{Path, PathBuf};
|
|
3
|
+
|
|
4
|
+
use serde::{Deserialize, Serialize};
|
|
5
|
+
|
|
6
|
+
use crate::codex_settings::HomeCodexSettingsDocument;
|
|
7
|
+
use crate::platform::{
|
|
8
|
+
SecretBytes, SecretString, reject_symlink, volatile_zero_bytes, write_private_atomic,
|
|
9
|
+
};
|
|
10
|
+
use crate::relay_auth::token_fingerprint_hex;
|
|
11
|
+
|
|
12
|
+
pub const CHILD_STATE_SCHEMA: &str = "ocx.child.state/1";
|
|
13
|
+
|
|
14
|
+
#[derive(Debug, Clone)]
|
|
15
|
+
pub struct ChildPaths {
|
|
16
|
+
pub opencodex_home: PathBuf,
|
|
17
|
+
pub codex_home: PathBuf,
|
|
18
|
+
pub child_dir: PathBuf,
|
|
19
|
+
pub lock_file: PathBuf,
|
|
20
|
+
pub state_file: PathBuf,
|
|
21
|
+
pub key_file: PathBuf,
|
|
22
|
+
pub journal_file: PathBuf,
|
|
23
|
+
pub rollback_file: PathBuf,
|
|
24
|
+
pub catalog_backup_file: PathBuf,
|
|
25
|
+
/// Random owner id this OpenCodex home uses in a `CODEX_HOME` ownership record.
|
|
26
|
+
pub owner_id_file: PathBuf,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
impl ChildPaths {
|
|
30
|
+
pub fn new(opencodex_home: PathBuf, codex_home: PathBuf) -> Self {
|
|
31
|
+
let child_dir = opencodex_home.join("child");
|
|
32
|
+
Self {
|
|
33
|
+
lock_file: opencodex_home.join("child.lock"),
|
|
34
|
+
state_file: child_dir.join("state.json"),
|
|
35
|
+
key_file: child_dir.join("link-key"),
|
|
36
|
+
journal_file: child_dir.join("codex-journal.json"),
|
|
37
|
+
rollback_file: child_dir.join("attach-rollback.json"),
|
|
38
|
+
catalog_backup_file: child_dir.join("last-catalog.json"),
|
|
39
|
+
owner_id_file: child_dir.join("codex-home-owner-id"),
|
|
40
|
+
opencodex_home,
|
|
41
|
+
codex_home,
|
|
42
|
+
child_dir,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
pub fn codex_config_path(&self) -> PathBuf {
|
|
47
|
+
self.codex_home.join("config.toml")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
pub fn codex_catalog_path(&self) -> PathBuf {
|
|
51
|
+
self.codex_home.join("opencodex-catalog.json")
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
pub fn codex_models_cache_path(&self) -> PathBuf {
|
|
55
|
+
self.codex_home.join("models_cache.json")
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// Fixed lock serializing every ocx-child that writes this `CODEX_HOME`; never removed.
|
|
59
|
+
pub fn codex_lock_path(&self) -> PathBuf {
|
|
60
|
+
self.codex_home
|
|
61
|
+
.join(crate::codex_home::CODEX_HOME_LOCK_FILE)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
pub fn codex_owner_path(&self) -> PathBuf {
|
|
65
|
+
self.codex_home
|
|
66
|
+
.join(crate::codex_home::CODEX_HOME_OWNER_FILE)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
71
|
+
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
72
|
+
pub struct AppliedSettingsRecord {
|
|
73
|
+
pub link_id: String,
|
|
74
|
+
pub fetched_at: String,
|
|
75
|
+
pub applied_at: String,
|
|
76
|
+
pub applied_at_ms: u64,
|
|
77
|
+
pub digest: String,
|
|
78
|
+
pub document: HomeCodexSettingsDocument,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
82
|
+
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
83
|
+
pub struct ChildStateV1 {
|
|
84
|
+
pub schema: String,
|
|
85
|
+
pub link_id: String,
|
|
86
|
+
pub api_key_id: String,
|
|
87
|
+
pub token_fingerprint: String,
|
|
88
|
+
pub tunnel_port: u16,
|
|
89
|
+
pub listen_port: u16,
|
|
90
|
+
pub connected_at: String,
|
|
91
|
+
pub catalog_fingerprint: String,
|
|
92
|
+
pub catalog_synced_at: String,
|
|
93
|
+
pub daemon_pid: Option<u32>,
|
|
94
|
+
pub daemon_started_at_ms: Option<u64>,
|
|
95
|
+
pub shutdown_secret: SecretString,
|
|
96
|
+
pub parity_sequence: u64,
|
|
97
|
+
pub pending_operation: Option<String>,
|
|
98
|
+
pub applied_settings: Option<AppliedSettingsRecord>,
|
|
99
|
+
/// Random nonce of the most recent daemon start. A lifecycle-started daemon serves only
|
|
100
|
+
/// if this still names its generation and its parent sent the matching commit line.
|
|
101
|
+
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
102
|
+
pub daemon_generation: Option<String>,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
pub fn is_valid_daemon_generation(value: &str) -> bool {
|
|
106
|
+
value.len() == 32
|
|
107
|
+
&& value
|
|
108
|
+
.bytes()
|
|
109
|
+
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
#[derive(Debug)]
|
|
113
|
+
pub enum StateReadOutcome {
|
|
114
|
+
Absent,
|
|
115
|
+
Valid(Box<ChildStateV1>),
|
|
116
|
+
Invalid(String),
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
pub fn read_child_state(path: &Path) -> StateReadOutcome {
|
|
120
|
+
if let Err(e) = reject_symlink(path) {
|
|
121
|
+
return StateReadOutcome::Invalid(e);
|
|
122
|
+
}
|
|
123
|
+
let bytes = match fs::read(path) {
|
|
124
|
+
Ok(b) => SecretBytes::new(b),
|
|
125
|
+
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return StateReadOutcome::Absent,
|
|
126
|
+
Err(e) => return StateReadOutcome::Invalid(format!("unreadable state.json: {e}")),
|
|
127
|
+
};
|
|
128
|
+
match serde_json::from_slice::<ChildStateV1>(bytes.as_slice()) {
|
|
129
|
+
Ok(st) if st.schema != CHILD_STATE_SCHEMA => {
|
|
130
|
+
StateReadOutcome::Invalid(format!("unsupported schema: {}", st.schema))
|
|
131
|
+
}
|
|
132
|
+
Ok(st)
|
|
133
|
+
if st.shutdown_secret.len() == 48
|
|
134
|
+
&& st
|
|
135
|
+
.shutdown_secret
|
|
136
|
+
.bytes()
|
|
137
|
+
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) =>
|
|
138
|
+
{
|
|
139
|
+
StateReadOutcome::Valid(Box::new(st))
|
|
140
|
+
}
|
|
141
|
+
Ok(_) => StateReadOutcome::Invalid("invalid child shutdown secret".to_owned()),
|
|
142
|
+
Err(e) => StateReadOutcome::Invalid(format!("malformed state.json: {e}")),
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
pub fn write_child_state(path: &Path, state: &ChildStateV1) -> Result<(), String> {
|
|
147
|
+
let mut bytes = serde_json::to_vec_pretty(state)
|
|
148
|
+
.map_err(|e| format!("failed to serialize state.json: {e}"))?;
|
|
149
|
+
bytes.push(b'\n');
|
|
150
|
+
let result = write_private_atomic(path, &bytes);
|
|
151
|
+
volatile_zero_bytes(&mut bytes);
|
|
152
|
+
result
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
pub fn is_valid_link_token_format(raw_token: &str) -> bool {
|
|
156
|
+
raw_token.len() == 49
|
|
157
|
+
&& raw_token.starts_with("ocx_data_")
|
|
158
|
+
&& raw_token[9..]
|
|
159
|
+
.bytes()
|
|
160
|
+
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
pub fn write_link_key(path: &Path, raw_token: &str) -> Result<String, String> {
|
|
164
|
+
if !is_valid_link_token_format(raw_token) {
|
|
165
|
+
return Err("invalid link credential format".to_owned());
|
|
166
|
+
}
|
|
167
|
+
let fp = token_fingerprint_hex(raw_token);
|
|
168
|
+
write_private_atomic(path, raw_token.as_bytes())?;
|
|
169
|
+
Ok(fp)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
pub fn read_verified_link_key(
|
|
173
|
+
path: &Path,
|
|
174
|
+
expected_fingerprint: &str,
|
|
175
|
+
) -> Result<SecretBytes, String> {
|
|
176
|
+
reject_symlink(path)?;
|
|
177
|
+
let raw = fs::read(path).map_err(|e| format!("link key unavailable: {e}"))?;
|
|
178
|
+
let secret = SecretBytes::new(raw);
|
|
179
|
+
let text = std::str::from_utf8(secret.as_slice())
|
|
180
|
+
.map_err(|_| "link key invalid utf8".to_owned())?
|
|
181
|
+
.trim();
|
|
182
|
+
if !is_valid_link_token_format(text) {
|
|
183
|
+
return Err("link key malformed".to_owned());
|
|
184
|
+
}
|
|
185
|
+
let actual_fp = token_fingerprint_hex(text);
|
|
186
|
+
if actual_fp != expected_fingerprint {
|
|
187
|
+
return Err("link key fingerprint mismatch".to_owned());
|
|
188
|
+
}
|
|
189
|
+
Ok(SecretBytes::new(text.as_bytes().to_vec()))
|
|
190
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
//! Env-gated synchronization points used by process-level lifecycle tests.
|
|
2
|
+
//!
|
|
3
|
+
//! Compiled only with `debug_assertions` (the profile `cargo test` builds the binary with);
|
|
4
|
+
//! a release build has no barrier and reads no barrier environment variable.
|
|
5
|
+
//!
|
|
6
|
+
//! Every call is a no-op unless both `OCX_CHILD_TEST_BARRIER_DIR` and
|
|
7
|
+
//! `OCX_CHILD_TEST_BARRIERS` are set. When the current point is listed, the process writes
|
|
8
|
+
//! `<dir>/<name>.reached` (containing its PID) and waits until `<dir>/<name>.release` exists,
|
|
9
|
+
//! for at most 60 seconds. A point whose release file already exists passes immediately, so
|
|
10
|
+
//! a point reached repeatedly blocks only once.
|
|
11
|
+
|
|
12
|
+
#[cfg(debug_assertions)]
|
|
13
|
+
pub const BARRIER_DIR_ENV: &str = "OCX_CHILD_TEST_BARRIER_DIR";
|
|
14
|
+
#[cfg(debug_assertions)]
|
|
15
|
+
pub const BARRIERS_ENV: &str = "OCX_CHILD_TEST_BARRIERS";
|
|
16
|
+
|
|
17
|
+
#[cfg(debug_assertions)]
|
|
18
|
+
pub fn barrier(name: &str) {
|
|
19
|
+
use std::path::PathBuf;
|
|
20
|
+
use std::time::{Duration, Instant};
|
|
21
|
+
|
|
22
|
+
let Some(dir) = std::env::var_os(BARRIER_DIR_ENV).filter(|v| !v.is_empty()) else {
|
|
23
|
+
return;
|
|
24
|
+
};
|
|
25
|
+
let Ok(list) = std::env::var(BARRIERS_ENV) else {
|
|
26
|
+
return;
|
|
27
|
+
};
|
|
28
|
+
if !list.split(',').any(|entry| entry.trim() == name) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
let dir = PathBuf::from(dir);
|
|
32
|
+
let release = dir.join(format!("{name}.release"));
|
|
33
|
+
if release.exists() {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
let _ = std::fs::write(
|
|
37
|
+
dir.join(format!("{name}.reached")),
|
|
38
|
+
std::process::id().to_string(),
|
|
39
|
+
);
|
|
40
|
+
let deadline = Instant::now() + Duration::from_secs(60);
|
|
41
|
+
while !release.exists() && Instant::now() < deadline {
|
|
42
|
+
std::thread::sleep(Duration::from_millis(20));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
#[cfg(not(debug_assertions))]
|
|
47
|
+
#[inline(always)]
|
|
48
|
+
pub fn barrier(_name: &str) {}
|
|
49
|
+
|
|
50
|
+
/// Keep a spawned relay from inheriting the barrier configuration of its lifecycle parent.
|
|
51
|
+
#[cfg(debug_assertions)]
|
|
52
|
+
pub fn strip_barrier_env(cmd: &mut std::process::Command) {
|
|
53
|
+
cmd.env_remove(BARRIER_DIR_ENV).env_remove(BARRIERS_ENV);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#[cfg(not(debug_assertions))]
|
|
57
|
+
#[inline(always)]
|
|
58
|
+
pub fn strip_barrier_env(_cmd: &mut std::process::Command) {}
|