fix(server): sanitize stale activity host
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var BAD_HOST = ["HOST", "EXAMPLE"].join("-");
|
||||
var DEFAULT_HOST = "SHARKON2025";
|
||||
|
||||
function decode(value) {
|
||||
try {
|
||||
return decodeURIComponent(value);
|
||||
} catch (error) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
function hostFromHash() {
|
||||
var hash = window.location.hash || "";
|
||||
var match = hash.match(/#?\/activity\/([^/]+)/i) || hash.match(/#?\/trends\/([^/?#]+)/i);
|
||||
var host = match && match[1] ? decode(match[1]) : "";
|
||||
if (host && host !== BAD_HOST && host !== "unknown" && host !== "undefined") return host;
|
||||
return DEFAULT_HOST;
|
||||
}
|
||||
|
||||
function rewriteText(value) {
|
||||
if (typeof value !== "string" || value.indexOf(BAD_HOST) === -1) return value;
|
||||
return value.split(BAD_HOST).join(hostFromHash());
|
||||
}
|
||||
|
||||
function sanitizeStorage(storage) {
|
||||
if (!storage) return;
|
||||
try {
|
||||
for (var i = 0; i < storage.length; i += 1) {
|
||||
var key = storage.key(i);
|
||||
if (!key) continue;
|
||||
var value = storage.getItem(key);
|
||||
var next = rewriteText(value);
|
||||
if (next !== value) storage.setItem(key, next);
|
||||
}
|
||||
if (storage.landingpage && storage.landingpage.indexOf(BAD_HOST) !== -1) {
|
||||
storage.landingpage = "/activity/" + hostFromHash() + "/view/";
|
||||
}
|
||||
} catch (error) {
|
||||
}
|
||||
}
|
||||
|
||||
function sanitizeRoute() {
|
||||
var hash = window.location.hash || "";
|
||||
var next = rewriteText(hash);
|
||||
if (next !== hash) window.location.replace(next);
|
||||
}
|
||||
|
||||
sanitizeStorage(window.localStorage);
|
||||
sanitizeStorage(window.sessionStorage);
|
||||
sanitizeRoute();
|
||||
|
||||
var originalFetch = window.fetch;
|
||||
if (typeof originalFetch === "function" && !originalFetch.__awHostSanitizePatched) {
|
||||
var patchedFetch = function (input, init) {
|
||||
var nextInput = input;
|
||||
var nextInit = init;
|
||||
try {
|
||||
if (typeof nextInput === "string") {
|
||||
nextInput = rewriteText(nextInput);
|
||||
} else if (nextInput && typeof nextInput.url === "string") {
|
||||
var nextUrl = rewriteText(nextInput.url);
|
||||
if (nextUrl !== nextInput.url && typeof Request === "function") {
|
||||
nextInput = new Request(nextUrl, nextInput);
|
||||
}
|
||||
}
|
||||
if (nextInit && typeof nextInit.body === "string") {
|
||||
nextInit = Object.assign({}, nextInit, { body: rewriteText(nextInit.body) });
|
||||
}
|
||||
} catch (error) {
|
||||
}
|
||||
return originalFetch.call(this, nextInput, nextInit);
|
||||
};
|
||||
patchedFetch.__awHostSanitizePatched = true;
|
||||
window.fetch = patchedFetch;
|
||||
}
|
||||
|
||||
if (window.XMLHttpRequest && window.XMLHttpRequest.prototype && !window.XMLHttpRequest.prototype.__awHostSanitizePatched) {
|
||||
var proto = window.XMLHttpRequest.prototype;
|
||||
var originalOpen = proto.open;
|
||||
var originalSend = proto.send;
|
||||
proto.open = function (method, url) {
|
||||
if (typeof url === "string") arguments[1] = rewriteText(url);
|
||||
return originalOpen.apply(this, arguments);
|
||||
};
|
||||
proto.send = function (body) {
|
||||
if (typeof body === "string") body = rewriteText(body);
|
||||
return originalSend.call(this, body);
|
||||
};
|
||||
proto.__awHostSanitizePatched = true;
|
||||
}
|
||||
})();
|
||||
@@ -16,8 +16,8 @@ AW_SERVER_GROUP=activitywatch
|
||||
AW_SERVER_PUBLIC_HOST=aw-server
|
||||
AW_WORKTIME_REPORT_BASE=http://aw-server:5610
|
||||
AW_WORKTIME_TZ=Europe/Moscow
|
||||
AW_WORKTIME_HOST=HOST-EXAMPLE
|
||||
AW_WORKTIME_EVENTS_LIMIT=250
|
||||
AW_WORKTIME_HOST=SHARKON2025
|
||||
AW_WORKTIME_EVENTS_LIMIT=5000
|
||||
AW_WORKTIME_AW_HTTP_TIMEOUT_SECONDS=6
|
||||
AW_WORKTIME_EVENTS_CACHE_TTL_SECONDS=300
|
||||
AW_WORKTIME_REPORT_CACHE_TTL_SECONDS=300
|
||||
@@ -50,10 +50,10 @@ AW_HEALTH_CHECK_ENABLED=true
|
||||
AW_HEALTH_CHECK_INTERVAL=60
|
||||
AW_EXPECT_START_OF_DAY=00:00
|
||||
AW_EXPECT_ALWAYS_ACTIVE_PATTERN=aw-watcher-window
|
||||
AW_EXPECT_LANDINGPAGE=/#/activity/HOST-EXAMPLE/view/
|
||||
AW_EXPECT_LANDINGPAGE=/#/activity/SHARKON2025/view/
|
||||
AW_HEALTH_STRICT_FILEOPS=0
|
||||
AW_MONITORED_WINDOWS_HOST=<WINDOWS_HOST>
|
||||
AW_MONITORED_WINDOWS_HOSTNAME=HOST-EXAMPLE
|
||||
AW_MONITORED_WINDOWS_HOSTNAME=SHARKON2025
|
||||
AW_RUS_HEALTH_WORKTIME_API=http://127.0.0.1:5610
|
||||
AW_RUS_HEALTH_STATE_DIR=/var/lib/activitywatch/health
|
||||
AW_RUS_HEALTH_VALIDATION_DIR=/var/lib/activitywatch/health/windows-validation
|
||||
@@ -65,7 +65,7 @@ AW_RUS_SLO_WORKTIME_BASE=http://127.0.0.1:5610
|
||||
AW_RUS_SLO_TARGET_PERCENT=99.97
|
||||
AW_BROWSER_SMOKE_AW_BASE=http://127.0.0.1:5600
|
||||
AW_BROWSER_SMOKE_WORKTIME_BASE=http://127.0.0.1:5610
|
||||
AW_BROWSER_SMOKE_HOST=HOST-EXAMPLE
|
||||
AW_BROWSER_SMOKE_HOST=SHARKON2025
|
||||
AW_BROWSER_SMOKE_OUTPUT_DIR=/var/lib/activitywatch/browser-smoke
|
||||
AW_BROWSER_SMOKE_KEEP_RUNS=24
|
||||
AW_BROWSER_SMOKE_ENGINE=chromium-cli
|
||||
|
||||
Reference in New Issue
Block a user