Harden admin telemetry and deployment safeguards
TaxBaik CI/CD / build-and-deploy (push) Successful in 4m30s
TaxBaik CI/CD / build-and-deploy (push) Successful in 4m30s
This commit is contained in:
@@ -1,4 +1,191 @@
|
||||
window.taxbaikAdminSession = {
|
||||
clientLogState: {
|
||||
enabled: true,
|
||||
windowStart: 0,
|
||||
sentCount: 0,
|
||||
suppressedCount: 0,
|
||||
fingerprints: {},
|
||||
eventCounts: {},
|
||||
screen: '',
|
||||
feature: '',
|
||||
action: '',
|
||||
step: '',
|
||||
entity: '',
|
||||
entityId: '',
|
||||
dataKey: ''
|
||||
},
|
||||
|
||||
initErrorLogging: function () {
|
||||
if (window._taxbaikClientLogInitialized) return;
|
||||
window._taxbaikClientLogInitialized = true;
|
||||
|
||||
const postLog = function (payload) {
|
||||
try {
|
||||
if (!window.taxbaikAdminSession.shouldSendClientLog(payload)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const body = JSON.stringify(payload);
|
||||
if (navigator.sendBeacon) {
|
||||
const blob = new Blob([body], { type: 'application/json' });
|
||||
if (navigator.sendBeacon('/taxbaik/api/client-logs', blob)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fetch('/taxbaik/api/client-logs', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body,
|
||||
keepalive: true
|
||||
}).catch(function () { });
|
||||
} catch {
|
||||
// Logging must never break the UI.
|
||||
}
|
||||
};
|
||||
|
||||
window.taxbaikAdminSession.postClientLog = postLog;
|
||||
|
||||
window.addEventListener('error', function (event) {
|
||||
postLog({
|
||||
level: 'error',
|
||||
source: 'window.error',
|
||||
message: event.message || 'unknown error',
|
||||
url: event.filename || window.location.href,
|
||||
route: window.location.pathname + window.location.search,
|
||||
screen: window.taxbaikAdminSession.clientLogState.screen || '',
|
||||
feature: window.taxbaikAdminSession.clientLogState.feature || '',
|
||||
action: window.taxbaikAdminSession.clientLogState.action || '',
|
||||
step: window.taxbaikAdminSession.clientLogState.step || '',
|
||||
entity: window.taxbaikAdminSession.clientLogState.entity || '',
|
||||
entityId: window.taxbaikAdminSession.clientLogState.entityId || '',
|
||||
dataKey: window.taxbaikAdminSession.clientLogState.dataKey || '',
|
||||
buildVersion: window.taxbaikAdminBuildVersion || '',
|
||||
component: window.taxbaikAdminComponent || '',
|
||||
viewportWidth: window.taxbaikAdminSession.getViewportWidth(),
|
||||
userAgent: navigator.userAgent || '',
|
||||
stack: event.error?.stack || ''
|
||||
});
|
||||
});
|
||||
|
||||
window.addEventListener('unhandledrejection', function (event) {
|
||||
const reason = event.reason;
|
||||
postLog({
|
||||
level: 'error',
|
||||
source: 'window.unhandledrejection',
|
||||
message: reason?.message || String(reason || 'unknown rejection'),
|
||||
url: window.location.href,
|
||||
route: window.location.pathname + window.location.search,
|
||||
screen: window.taxbaikAdminSession.clientLogState.screen || '',
|
||||
feature: window.taxbaikAdminSession.clientLogState.feature || '',
|
||||
action: window.taxbaikAdminSession.clientLogState.action || '',
|
||||
step: window.taxbaikAdminSession.clientLogState.step || '',
|
||||
entity: window.taxbaikAdminSession.clientLogState.entity || '',
|
||||
entityId: window.taxbaikAdminSession.clientLogState.entityId || '',
|
||||
dataKey: window.taxbaikAdminSession.clientLogState.dataKey || '',
|
||||
buildVersion: window.taxbaikAdminBuildVersion || '',
|
||||
component: window.taxbaikAdminComponent || '',
|
||||
viewportWidth: window.taxbaikAdminSession.getViewportWidth(),
|
||||
userAgent: navigator.userAgent || '',
|
||||
stack: reason?.stack || ''
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
setContext: function (screen, feature, action, step, entity, entityId, dataKey) {
|
||||
const state = window.taxbaikAdminSession.clientLogState;
|
||||
state.screen = screen || '';
|
||||
state.feature = feature || '';
|
||||
state.action = action || '';
|
||||
state.step = step || '';
|
||||
state.entity = entity || '';
|
||||
state.entityId = entityId || '';
|
||||
state.dataKey = dataKey || '';
|
||||
},
|
||||
|
||||
shouldSendClientLog: function (payload) {
|
||||
try {
|
||||
const state = window.taxbaikAdminSession.clientLogState;
|
||||
if (!state.enabled) return false;
|
||||
|
||||
const now = Date.now();
|
||||
if (!state.windowStart || now - state.windowStart >= 60000) {
|
||||
state.windowStart = now;
|
||||
state.sentCount = 0;
|
||||
state.suppressedCount = 0;
|
||||
state.fingerprints = {};
|
||||
}
|
||||
|
||||
const fingerprint = [
|
||||
payload?.source || '',
|
||||
payload?.message || '',
|
||||
payload?.route || '',
|
||||
payload?.component || '',
|
||||
payload?.screen || '',
|
||||
payload?.feature || '',
|
||||
payload?.action || '',
|
||||
payload?.entity || '',
|
||||
payload?.entityId || ''
|
||||
].join('|').slice(0, 256);
|
||||
|
||||
state.fingerprints[fingerprint] = (state.fingerprints[fingerprint] || 0) + 1;
|
||||
|
||||
if (state.sentCount >= 8) {
|
||||
state.suppressedCount += 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (state.fingerprints[fingerprint] > 2) {
|
||||
state.suppressedCount += 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
state.sentCount += 1;
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
traceUiState: function (source, details) {
|
||||
try {
|
||||
const payload = {
|
||||
level: 'info',
|
||||
source: source || 'ui-state',
|
||||
message: details || '',
|
||||
url: window.location.href,
|
||||
route: window.location.pathname + window.location.search,
|
||||
screen: window.taxbaikAdminSession.clientLogState.screen || '',
|
||||
feature: window.taxbaikAdminSession.clientLogState.feature || '',
|
||||
action: window.taxbaikAdminSession.clientLogState.action || '',
|
||||
step: window.taxbaikAdminSession.clientLogState.step || '',
|
||||
entity: window.taxbaikAdminSession.clientLogState.entity || '',
|
||||
entityId: window.taxbaikAdminSession.clientLogState.entityId || '',
|
||||
dataKey: window.taxbaikAdminSession.clientLogState.dataKey || '',
|
||||
buildVersion: window.taxbaikAdminBuildVersion || '',
|
||||
component: window.taxbaikAdminComponent || '',
|
||||
viewportWidth: window.taxbaikAdminSession.getViewportWidth(),
|
||||
userAgent: navigator.userAgent || '',
|
||||
stack: ''
|
||||
};
|
||||
|
||||
const state = window.taxbaikAdminSession.clientLogState;
|
||||
const key = `${payload.source}|${payload.route}|${payload.message}`.slice(0, 256);
|
||||
state.eventCounts[key] = (state.eventCounts[key] || 0) + 1;
|
||||
if (state.eventCounts[key] > 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.taxbaikAdminSession.postClientLog(payload);
|
||||
} catch {
|
||||
// diagnostics must never break UI.
|
||||
}
|
||||
},
|
||||
|
||||
postClientLog: function () {
|
||||
// Replaced during initialization.
|
||||
},
|
||||
|
||||
syncRouteClass: function () {
|
||||
document.documentElement.classList.toggle(
|
||||
'admin-login-route',
|
||||
@@ -23,6 +210,7 @@ window.taxbaikAdminSession = {
|
||||
showLoading: function () {
|
||||
// Route transitions are handled by Blazor; avoid full-screen overlays
|
||||
// that block drawer interaction and make the app feel frozen.
|
||||
window.taxbaikAdminSession.traceUiState('admin-loading', 'showLoading requested');
|
||||
window.taxbaikAdminSession.hideLoading();
|
||||
},
|
||||
|
||||
@@ -41,11 +229,14 @@ window.taxbaikAdminSession = {
|
||||
window._taxbaikLoadingObserver.disconnect();
|
||||
window._taxbaikLoadingObserver = null;
|
||||
}
|
||||
|
||||
window.taxbaikAdminSession.traceUiState('admin-loading', 'hideLoading completed');
|
||||
},
|
||||
|
||||
watchReconnect: function () {
|
||||
window.taxbaikAdminSession.syncRouteClass();
|
||||
window.addEventListener('popstate', window.taxbaikAdminSession.syncRouteClass);
|
||||
window.addEventListener('hashchange', window.taxbaikAdminSession.syncRouteClass);
|
||||
|
||||
if (document.documentElement.classList.contains('admin-login-route')) {
|
||||
window.taxbaikAdminSession.hideLoading();
|
||||
@@ -74,6 +265,7 @@ window.taxbaikAdminSession = {
|
||||
if (!form || form.dataset.bound === '1') return;
|
||||
|
||||
form.dataset.bound = '1';
|
||||
window.taxbaikAdminSession.traceUiState('admin-login', 'bindLoginForm attached');
|
||||
form.addEventListener('submit', async function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
@@ -87,6 +279,11 @@ window.taxbaikAdminSession = {
|
||||
if (submitButton) submitButton.disabled = true;
|
||||
|
||||
try {
|
||||
if (!username || !password) {
|
||||
throw new Error('username/password missing');
|
||||
}
|
||||
|
||||
window.taxbaikAdminSession.traceUiState('admin-login', 'submit started');
|
||||
const response = await fetch('/taxbaik/api/auth/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -102,9 +299,11 @@ window.taxbaikAdminSession = {
|
||||
throw new Error('invalid response');
|
||||
}
|
||||
|
||||
window.taxbaikAdminSession.traceUiState('admin-login', 'submit success');
|
||||
const expiryTicks = 621355968000000000 + ((Date.now() + (data.expiresIn || 3600) * 1000) * 10000);
|
||||
localStorage.setItem('accessToken', data.accessToken);
|
||||
localStorage.setItem('refreshToken', data.refreshToken);
|
||||
localStorage.setItem('tokenExpiry', String(Date.now() + (data.expiresIn || 3600) * 1000));
|
||||
localStorage.setItem('tokenExpiry', String(expiryTicks));
|
||||
|
||||
if (rememberMe) {
|
||||
localStorage.setItem('admin-remembered-username', username);
|
||||
@@ -113,11 +312,24 @@ window.taxbaikAdminSession = {
|
||||
}
|
||||
|
||||
window.location.href = '/taxbaik/admin/dashboard';
|
||||
} catch {
|
||||
const error = document.createElement('div');
|
||||
error.className = 'mud-alert mud-alert-filled-error login-error-message mb-4';
|
||||
error.textContent = '로그인 중 오류가 발생했습니다.';
|
||||
form.parentElement.insertBefore(error, form);
|
||||
} catch (error) {
|
||||
window.taxbaikAdminSession.traceUiState('admin-login', `submit failed: ${error?.message || 'login failed'}`);
|
||||
postLog({
|
||||
level: 'error',
|
||||
source: 'admin-login-form',
|
||||
message: error?.message || 'login failed',
|
||||
url: window.location.href,
|
||||
route: window.location.pathname + window.location.search,
|
||||
buildVersion: window.taxbaikAdminBuildVersion || '',
|
||||
component: 'AdminLoginForm',
|
||||
viewportWidth: window.taxbaikAdminSession.getViewportWidth(),
|
||||
userAgent: navigator.userAgent || '',
|
||||
stack: error?.stack || ''
|
||||
});
|
||||
const errorMessage = document.createElement('div');
|
||||
errorMessage.className = 'mud-alert mud-alert-filled-error login-error-message mb-4';
|
||||
errorMessage.textContent = '로그인 중 오류가 발생했습니다.';
|
||||
form.parentElement.insertBefore(errorMessage, form);
|
||||
} finally {
|
||||
if (submitButton) submitButton.disabled = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user