wangzhibo
2026-10-08 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
export const SNAPSHOT_SECRET_REDACTED = "__SNAPSHOT_SECRET_REDACTED__";
 
export function hasValidCurrentStateToken(value) {
  return typeof value === "string" && value.trim().length > 0;
}
 
export function redactSnapshotDisplayValue(value, redactedLabel, parentKey = "") {
  if (value === SNAPSHOT_SECRET_REDACTED || isSensitiveSnapshotKey(parentKey)) {
    return redactedLabel;
  }
  if (Array.isArray(value)) {
    return value.map((item) => redactSnapshotDisplayValue(item, redactedLabel, parentKey));
  }
  if (isPlainObject(value)) {
    const sensitiveEntry = Object.keys(value).some((key) => {
      const normalizedKey = key.toLowerCase();
      return (normalizedKey === "key" || normalizedKey === "name")
        && typeof value[key] === "string"
        && isSensitiveSnapshotKey(value[key]);
    });
    return Object.keys(value).reduce((result, key) => {
      const semanticKey = resolveUrlSemanticKey(parentKey, key);
      if (sensitiveEntry && key.toLowerCase() === "value") {
        result[key] = redactedLabel;
      } else {
        result[key] = redactSnapshotDisplayValue(value[key], redactedLabel, semanticKey);
      }
      return result;
    }, {});
  }
  if (typeof value === "string" && isSnapshotUrlValue(parentKey, value)) {
    return redactSnapshotUrl(value, redactedLabel, parentKey);
  }
  return value;
}
 
export function normalizeSnapshotOrderedValue(value) {
  if (Array.isArray(value)) {
    return value.map((item) => normalizeSnapshotOrderedValue(item));
  }
  if (isPlainObject(value)) {
    return Object.keys(value).sort().reduce((result, key) => {
      result[key] = normalizeSnapshotOrderedValue(value[key]);
      return result;
    }, {});
  }
  return value === undefined ? null : value;
}
 
export function isSensitiveSnapshotKey(key) {
  const normalized = String(key || "").toLowerCase().replace(/[^a-z0-9]/g, "");
  return normalized === "authorization"
    || normalized.includes("authorization")
    || normalized.includes("authentication")
    || normalized === "auth"
    || normalized.endsWith("auth")
    || normalized === "cookie"
    || normalized === "cookie2"
    || normalized === "setcookie"
    || normalized === "setcookie2"
    || normalized.endsWith("cookie")
    || normalized === "session"
    || normalized.endsWith("session")
    || normalized.includes("sessionid")
    || normalized.includes("sessionkey")
    || normalized.includes("sessiontoken")
    || normalized.includes("sessioncookie")
    || normalized.endsWith("sessid")
    || normalized === "token"
    || normalized.endsWith("token")
    || normalized.includes("apikey")
    || normalized.includes("appkey")
    || normalized.includes("accesskey")
    || normalized.includes("subscriptionkey")
    || normalized.includes("privatekey")
    || normalized.includes("password")
    || normalized.includes("passwd")
    || normalized.includes("secret")
    || normalized.includes("credential");
}
 
function isSnapshotUrlValue(parentKey, value) {
  const normalizedKey = String(parentKey || "").toLowerCase().replace(/[^a-z0-9]/g, "");
  return normalizedKey.includes("url")
    || normalizedKey.endsWith("uri")
    || normalizedKey.includes("endpoint")
    || normalizedKey.includes("webhook")
    || /^(?:[a-z][a-z0-9+.-]*:)*\/\//i.test(value);
}
 
function resolveUrlSemanticKey(parentKey, childKey) {
  if (isWebhookSemanticKey(childKey)) {
    return childKey;
  }
  if (isWebhookSemanticKey(parentKey)) {
    return childKey ? `${parentKey}.${childKey}` : parentKey;
  }
  return childKey;
}
 
function isWebhookSemanticKey(value) {
  const normalized = String(value || "").toLowerCase().replace(/[^a-z0-9]/g, "");
  return normalized.includes("webhook")
    || normalized === "hook"
    || normalized === "hooks"
    || normalized.endsWith("hook")
    || normalized.endsWith("hooks");
}
 
function redactSnapshotUrl(value, redactedLabel, parentKey) {
  const withoutCredentials = value.replace(
    /^((?:[a-z][a-z0-9+.-]*:)*\/\/)([^/?#\s]*@)/i,
    (match, prefix) => `${prefix}${redactedLabel}@`
  );
  const suffixIndex = withoutCredentials.search(/[?#]/);
  const base = suffixIndex < 0 ? withoutCredentials : withoutCredentials.slice(0, suffixIndex);
  const redactedBase = redactSnapshotCapabilityPath(base, redactedLabel, parentKey);
  return suffixIndex < 0
    ? redactedBase
    : `${redactedBase}${withoutCredentials[suffixIndex]}${redactedLabel}`;
}
 
function redactSnapshotCapabilityPath(value, redactedLabel, parentKey) {
  const absoluteUrl = value.match(/^((?:[a-z][a-z0-9+.-]*:)*\/\/)([^/?#]*)([^?#]*)$/i);
  if (!absoluteUrl) {
    return redactGenericCapabilityPath(value, redactedLabel, parentKey);
  }
 
  const [, scheme, authority, path = ""] = absoluteUrl;
  const hostWithPort = authority.slice(authority.lastIndexOf("@") + 1);
  const host = hostWithPort.replace(/:\d+$/, "").toLowerCase();
  let redactedPath = path;
  let providerMatched = false;
 
  if (host === "hooks.slack.com" || host === "hooks.slack-gov.com") {
    redactedPath = redactedPath.replace(
      /^(\/services\/[^/]+\/[^/]+\/)([^/]+)(.*)$/i,
      (match, prefix, secret, suffix) => `${prefix}${redactedLabel}${suffix}`
    );
    providerMatched = redactedPath !== path;
  } else if (
    host === "discord.com"
    || host.endsWith(".discord.com")
    || host === "discordapp.com"
    || host.endsWith(".discordapp.com")
  ) {
    redactedPath = redactedPath.replace(
      /^(\/api(?:\/v\d+)?\/webhooks\/[^/]+\/)([^/]+)(.*)$/i,
      (match, prefix, secret, suffix) => `${prefix}${redactedLabel}${suffix}`
    );
    providerMatched = redactedPath !== path;
  } else if (host === "api.telegram.org") {
    redactedPath = redactedPath.replace(
      /^((?:\/file)?\/bot[^/:]+:)([^/]+)(.*)$/i,
      (match, prefix, secret, suffix) => `${prefix}${redactedLabel}${suffix}`
    );
    providerMatched = redactedPath !== path;
  }
 
  if (!providerMatched) {
    redactedPath = redactGenericCapabilityPath(redactedPath, redactedLabel, parentKey);
  }
 
  return `${scheme}${authority}${redactedPath}`;
}
 
function redactGenericCapabilityPath(path, redactedLabel, parentKey) {
  const markerRedacted = path.replace(
    /(^|\/)(webhooks?|hooks?)(\/)(.+)$/i,
    (match, boundary, marker, separator) => `${boundary}${marker}${separator}${redactedLabel}`
  );
  if (markerRedacted !== path) {
    return markerRedacted;
  }
  if (!isWebhookSemanticKey(parentKey)) {
    return path;
  }
  return path.replace(
    /^(.*\/)([^/]+)(\/?)$/,
    (match, prefix, secret, suffix) => `${prefix}${redactedLabel}${suffix}`
  );
}
 
function isPlainObject(value) {
  return value !== null && typeof value === "object" && !Array.isArray(value);
}