Repository navigation
Expand file tree
/
Copy patherrors.osl
More file actions
362 lines (342 loc) · 11.6 KB
/
Copy patherrors.osl
File metadata and controls
362 lines (342 loc) · 11.6 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
number siteErrorRetentionMs = 7 * 86400000
array siteErrorPathWords = [
"about", "account-frame.html", "addon-settings.html", "addons", "admin", "assignments", "bounties", "changes",
"claim", "classroom", "commits", "compare", "dpa", "editor", "editor.html", "embed", "embed.html", "entry",
"explore", "followers", "following", "fullscreen", "fullscreen.html", "groups", "index.html", "join",
"leaderboard", "library", "manage", "mystuff", "news", "notifications", "p", "perks", "player", "player.html",
"posts", "privacy", "project", "pulls", "purchases", "random", "remixes", "roadmap", "school", "search",
"settings", "simple.html", "spaces", "stats", "status", "students", "subprocessors", "support", "terms",
"themes", "trust", "users", "wallet"
]
array siteErrorIdParents = [
"assignments", "bounties", "claim", "commits", "entry", "groups", "join", "news", "p", "posts", "project",
"pulls", "spaces", "students", "themes", "users"
]
def withoutQuery(string url) string (
return url.strip().split("#")[1].split("?")[1]
)
def siteErrorUrl(string url) string (
array parts = withoutQuery(url).split("/")
if parts.len < 3 or parts[2] != "" or parts[3] == "" (
return ""
)
string scheme = parts[1].toStr().toLower()
if scheme != "https:" and scheme != "http:" (
return ""
)
array hostParts = parts[3].toStr().toLower().split("@")
string scrubbed = scheme ++ "//" ++ hostParts[hostParts.len].toStr()
string path = ""
boolean idNext = false
for i parts.len (
if i > 3 and parts[i].toStr() != "" (
string segment = parts[i].toStr().toLower()
if !idNext and siteErrorPathWords.contains(segment) (
path ++= "/" ++ segment
idNext = siteErrorIdParents.contains(segment)
) else (
path ++= "/:id"
idNext = false
)
)
)
if path == "" (
path = "/"
)
return scrubbed ++ path
)
def siteErrorText(string text, string pageUrl, string scrubbedUrl) string (
string value = text
string page = withoutQuery(pageUrl)
if scrubbedUrl != "" and page.contains("://") and page != scrubbedUrl (
value = regex.replace(regex.escape(page) ++ "([^A-Za-z0-9._~%/-]|$)", value, scrubbedUrl ++ "$1")
)
value = regex.replace("(https?://[^[:space:]?#()<>\"']+)[?#][^[:space:]()<>\"']*?((:[0-9]+){1,2})?([[:space:])<>\"']|$)", value, "$1$2$4")
value = regex.replace("(?i)(https?://([a-z0-9-]+[.])*mistwarp[.]org)/(p|project|users|spaces|groups|bounties|news|posts|themes|claim|join)/[^/?#[:space:]:()<>\"']+", value, "$1/$3/:id")
value = regex.replace("(?i)(https?://editor[.]mistwarp[.]org)/[0-9]+", value, "$1/:id")
value = regex.replace("(https?://avatars[.]rotur[.]dev)/[^/?#[:space:]:()<>\"']+", value, "$1/:id")
value = regex.replace("(?i)/(users|blocks|mutes|followers|following|curators|invitations|judges|profiles?|projects|classes|students|teachers|claim|join|vanity|game-saves|library)/[^/?#[:space:]:()<>\"']+", value, "/$1/:id")
return regex.replace("[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+([.][A-Za-z0-9-]+)*[.][A-Za-z]{2,}", value, "[email]")
)
def siteErrorBrowser(string userAgent) string (
string name = "Other"
string token = ""
if userAgent.contains("Edg/") (
name = "Edge"
token = "Edg/"
) else if userAgent.contains("EdgA/") (
name = "Edge"
token = "EdgA/"
) else if userAgent.contains("EdgiOS/") (
name = "Edge"
token = "EdgiOS/"
) else if userAgent.contains("SamsungBrowser/") (
name = "Samsung Internet"
token = "SamsungBrowser/"
) else if userAgent.contains("OPR/") (
name = "Opera"
token = "OPR/"
) else if userAgent.contains("FxiOS/") (
name = "Firefox"
token = "FxiOS/"
) else if userAgent.contains("Firefox/") (
name = "Firefox"
token = "Firefox/"
) else if userAgent.contains("CriOS/") (
name = "Chrome"
token = "CriOS/"
) else if userAgent.contains("Chrome/") (
name = "Chrome"
token = "Chrome/"
) else if userAgent.contains("Safari/") (
name = "Safari"
token = "Version/"
)
if token != "" and userAgent.contains(token) (
string version = regex.findFirst("^[0-9]+", userAgent.split(token)[2]) ?? ""
if version != "" (
name ++= " " ++ version
)
)
string system = "other"
if userAgent.contains("iPhone") or userAgent.contains("iPad") (
system = "iOS"
) else if userAgent.contains("Android") (
system = "Android"
) else if userAgent.contains("CrOS") (
system = "ChromeOS"
) else if userAgent.contains("Windows") (
system = "Windows"
) else if userAgent.contains("Macintosh") or userAgent.contains("Mac OS X") (
system = "macOS"
) else if userAgent.contains("Linux") (
system = "Linux"
)
if userAgent.strip() == "" (
return ""
)
return name ++ " on " ++ system
)
def anonymousSiteError(object entry) object (
string pageUrl = (entry.url ?? "").toStr()
string url = siteErrorUrl(pageUrl)
string browser = (entry.browser ?? "").toStr()
if browser == "" and entry.contains("userAgent") (
browser = siteErrorBrowser((entry.userAgent ?? "").toStr())
)
object clean = {
_id: entry["_id"],
message: siteErrorText((entry.message ?? "").toStr(), pageUrl, url),
stack: siteErrorText((entry.stack ?? "").toStr(), pageUrl, url),
url: url,
kind: (entry.kind ?? "uncaught").toStr(),
browser: browser,
viewport: (entry.viewport ?? "").toStr(),
appVersion: (entry.appVersion ?? "").toStr(),
componentStack: siteErrorText((entry.componentStack ?? "").toStr(), pageUrl, url),
created: entry.created,
resolved: entry.resolved == true
}
return clean
)
def hasIdentifyingFields(object entry) boolean (
return entry.contains("username") or entry.contains("userAgent") or entry.contains("projectId") or entry.contains("resolvedBy")
)
def siteErrorCutoff(number now) number (
return now - siteErrorRetentionMs
)
def pruneSiteErrors() (
array expired = errorDocs.where("created", "<", siteErrorCutoff(timestamp)).all()
for i expired.len (
object entry = expired[i].assert(object)
void errorDocs.deleteOne({_id: entry["_id"]})
)
array stored = errorDocs.sort("created", "desc").limit(1100).all()
for i stored.len (
object entry = stored[i].assert(object)
if i > 1000 (
void errorDocs.deleteOne({_id: entry["_id"]})
) else if hasIdentifyingFields(entry) (
void errorDocs.replaceOne({_id: entry["_id"]}, anonymousSiteError(entry))
)
)
)
def sweepSiteErrors() boolean (
sync.withLock("site-errors", pruneSiteErrors) catch (err) (
log "Could not prune error reports: " ++ err.toStr()
return false
)
return true
)
def shouldIgnoreSiteError(string message) boolean (
string normalized = message.toLower().strip()
return normalized == "unsandboxed is not defined" or normalized == "referenceerror: unsandboxed is not defined" or normalized == "can't find variable: unsandboxed"
)
def siteErrorOrigin(string origin, string referer) string (
string value = origin.strip().toLower()
if value == "" or value == "null" (
array parts = referer.strip().toLower().split("/")
if parts.len < 3 or parts[2] != "" or parts[3] == "" (
return ""
)
value = parts[1] ++ "//" ++ parts[3]
)
if value.endsWith("/") (
value = value.trim(1, value.len - 1)
)
return value
)
def isAllowedErrorOrigin(string origin, array allowed) boolean (
return origin != "" and allowed.contains(origin)
)
def handleRecordSiteError(*serve.Context c) (
string origin = siteErrorOrigin((c.header("Origin") ?? ""), (c.header("Referer") ?? ""))
if !isAllowedErrorOrigin(origin, errorReportOrigins) (
c.json(200, {ok: true, ignored: true})
return
)
object body = requestJSON(c)
string message = body.message.toStr().strip()
if message == "" (
c.badRequest("error message is required")
return
)
if message.len > 2000 (
message = message.trim(1, 2000)
)
if shouldIgnoreSiteError(message) (
c.json(200, {ok: true, ignored: true})
return
)
string stack = ""
if body.contains("stack") and body.stack != null (
stack = body.stack.toStr().trim(1, 20000)
)
if stack == "" and body.contains("trace") and body.trace != null (
stack = body.trace.toStr().trim(1, 20000)
)
string url = ""
if body.contains("url") and body.url != null (
url = body.url.toStr().strip().trim(1, 2000)
)
string kind = "uncaught"
if body.contains("kind") and body.kind != null (
kind = body.kind.toStr().toLower().strip().trim(1, 40)
)
if kind != "uncaught" and kind != "rejection" and kind != "react" and kind != "manual" (
kind = "uncaught"
)
string viewport = ""
if body.contains("viewport") and body.viewport != null (
viewport = body.viewport.toStr().strip().trim(1, 40)
)
string appVersion = ""
if body.contains("version") and body.version != null (
appVersion = body.version.toStr().strip().trim(1, 120)
)
if appVersion == "" and body.contains("appVersion") and body.appVersion != null (
appVersion = body.appVersion.toStr().strip().trim(1, 120)
)
string componentStack = ""
if body.contains("componentStack") and body.componentStack != null (
componentStack = body.componentStack.toStr().trim(1, 10000)
)
object entry = anonymousSiteError({
_id: "e" ++ randomString(16),
message: message,
stack: stack,
url: url,
kind: kind,
browser: siteErrorBrowser((c.header("User-Agent") ?? "").trim(1, 500)),
viewport: viewport,
appVersion: appVersion,
componentStack: componentStack,
created: timestamp,
resolved: false
})
sync.lock("site-errors")
void errorDocs.insertOne(entry)
sync.unlock("site-errors")
void sweepSiteErrors()
c.json(201, {ok: true})
)
def handleAdminGetErrors(*serve.Context c) (
string show = (c.query("show") ?? "").toLower().strip()
array items = errorDocs.sort("created", "desc").limit(200).all()
if show != "all" and show != "resolved" (
array open = []
for i items.len (
object item = items[i].assert(object)
if item.resolved != true (
open.append(item)
)
)
items = open
) else if show == "resolved" (
array resolved = []
for i items.len (
object item = items[i].assert(object)
if item.resolved == true (
resolved.append(item)
)
)
items = resolved
)
number openCount = errorDocs.where("resolved", "==", false).count()
c.json(200, {ok: true, errors: items, openCount: openCount})
)
def handleAdminResolveSiteError(*serve.Context c) (
object body = requestJSON(c)
string id = body.id.toStr()
if id == "" (
c.badRequest("error id is required")
return
)
boolean resolved = true
if body.contains("resolved") (
resolved = body.resolved == true
)
sync.lock("site-errors")
object entry = (errorDocs.findById(id) ?? {})
if entry.len == 0 (
sync.unlock("site-errors")
c.notFound("error not found")
return
)
entry.resolved = resolved
boolean saved = putDocument(errorDocs, id, entry)
sync.unlock("site-errors")
if saved (
c.json(200, {ok: true})
return
)
c.internalError("could not update error")
)
def deleteAllSiteErrors() number (
sync.lock("site-errors")
array entries = errorDocs.all()
for i entries.len (
object entry = entries[i].assert(object)
void errorDocs.deleteOne({_id: entry["_id"]})
)
sync.unlock("site-errors")
return entries.len
)
def handleAdminDeleteAllSiteErrors(*serve.Context c) (
number deleted = deleteAllSiteErrors()
c.json(200, {ok: true, deleted: deleted})
)
def handleAdminDeleteSiteError(*serve.Context c) (
object body = requestJSON(c)
string id = body.id.toStr()
if id == "" (
c.badRequest("error id is required")
return
)
if (errorDocs.findById(id) ?? {}).len == 0 (
c.notFound("error not found")
return
)
void errorDocs.deleteOne({_id: id})
c.json(200, {ok: true})
)