@ran-sh/dsh-crew 1.0.1 → 1.0.3

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.
@@ -0,0 +1,38 @@
1
+ // Credential references are identifiers, never credential values. Keep this
2
+ // boundary strict because provider metadata is returned to the browser/UI.
3
+
4
+ const ENV_REFERENCE = /^[A-Za-z_][A-Za-z0-9_]{0,127}$/u;
5
+ const HANDLE_REFERENCE = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,127}$/u;
6
+ const SECRET_LIKE = /^(?:sk|pk|rk|sess|tok|token|key|secret)[_-]/iu;
7
+ const EMBEDDED_SECRET = /(?:^|\s)(?:bearer|basic)\s+|(?:https?|data):|=/iu;
8
+
9
+ function rawText(value) {
10
+ if (typeof value === 'string') return value.trim() || null;
11
+ if (value && typeof value === 'object') {
12
+ const candidate = value.name_or_handle ?? value.name ?? value.reference;
13
+ return typeof candidate === 'string' ? candidate.trim() || null : null;
14
+ }
15
+ return null;
16
+ }
17
+
18
+ /**
19
+ * Classify a reference without returning an unsafe value. `kind: env` is the
20
+ * common apiKeyEnv form; non-env stores may use a bounded opaque handle.
21
+ */
22
+ export function classifyCredentialReference(value, { kind = 'env' } = {}) {
23
+ const raw = rawText(value);
24
+ if (!raw) return { present: false, value: null, redacted: false };
25
+ const normalizedKind = typeof kind === 'string' ? kind.trim().toLowerCase() : 'env';
26
+ const valid = normalizedKind === 'env'
27
+ ? ENV_REFERENCE.test(raw) && !SECRET_LIKE.test(raw) && !EMBEDDED_SECRET.test(raw)
28
+ : HANDLE_REFERENCE.test(raw) && !SECRET_LIKE.test(raw) && !EMBEDDED_SECRET.test(raw);
29
+ return { present: true, value: valid ? raw : null, redacted: !valid };
30
+ }
31
+
32
+ export function sanitizeCredentialReference(value, options = {}) {
33
+ return classifyCredentialReference(value, options).value;
34
+ }
35
+
36
+ export function hasCredentialReference(value, options = {}) {
37
+ return classifyCredentialReference(value, options).present;
38
+ }