Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Code hardening

Blue Mousede30e6e7d054
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 19f0ec830..aa68dd580 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -387,9 +387,8 @@ export class AnalyticsService {
if (result.isBot && result.reason) {
const { country } = getGeoDetails(ip)
- // Fire-and-forget so the bot path stays as fast as possible.
- clickhouse
- .insert({
+ try {
+ await clickhouse.insert({
table: 'bot_blocks',
format: 'JSONEachRow',
values: [
@@ -402,12 +401,12 @@ export class AnalyticsService {
],
clickhouse_settings: { async_insert: 1 },
})
- .catch((reason) => {
- this.logger.warn(
- `Failed to log bot_block for pid ${pid}: ${reason}`,
- 'checkBot',
- )
- })
+ } catch (reason) {
+ this.logger.warn(
+ `Failed to log bot_block for pid ${pid}: ${reason}`,
+ 'checkBot',
+ )
+ }
}
return result
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index b68a0f1b3..eee0f7a25 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -382,8 +382,8 @@ export class AnalyticsService {
if (result.isBot && result.reason) {
const { country } = getGeoDetails(ip)
- clickhouse
- .insert({
+ try {
+ await clickhouse.insert({
table: 'bot_blocks',
format: 'JSONEachRow',
values: [
@@ -396,12 +396,12 @@ export class AnalyticsService {
],
clickhouse_settings: { async_insert: 1 },
})
- .catch((reason) => {
- this.logger.warn(
- `Failed to log bot_block for pid ${pid}: ${reason}`,
- 'checkBot',
- )
- })
+ } catch (reason) {
+ this.logger.warn(
+ `Failed to log bot_block for pid ${pid}: ${reason}`,
+ 'checkBot',
+ )
+ }
}
return result
diff --git a/backend/migrations/clickhouse/setup.js b/backend/migrations/clickhouse/setup.js
index 0a7cf240b..01f683db9 100644
--- a/backend/migrations/clickhouse/setup.js
+++ b/backend/migrations/clickhouse/setup.js
@@ -45,30 +45,25 @@ const clickhouseNoDatabase = createClient({
})
const databaselessQueriesRunner = async (queries, log = true) => {
- let failed = false
for (const query of queries) {
- if (failed) {
- return
- }
+ if (!query) continue
- if (query) {
- try {
- if (log) {
- console.log(chalk.purple('Running query:'), query)
- }
- await clickhouseNoDatabase.command({
- query,
- })
- if (log) {
- console.log(chalk.green('Query OK'))
- }
- } catch (error) {
- if (log) {
- console.error(chalk.red('Query ERROR: '), query)
- console.error(error)
- }
- failed = true
+ try {
+ if (log) {
+ console.log(chalk.purple('Running query:'), query)
+ }
+ await clickhouseNoDatabase.command({
+ query,
+ })
+ if (log) {
+ console.log(chalk.green('Query OK'))
}
+ } catch (error) {
+ if (log) {
+ console.error(chalk.red('Query ERROR: '), query)
+ console.error(error)
+ }
+ throw error
}
}
}
@@ -82,33 +77,27 @@ const databaselessQueriesRunner = async (queries, log = true) => {
* @returns
*/
const queriesRunner = async (queries, log = true, params = {}) => {
- let failed = false
-
for (const query of queries) {
- if (failed) {
- return false
- }
+ if (!query) continue
- if (query) {
- try {
- if (log) {
- console.log(chalk.purple('Running query:'), query)
- }
- await clickhouse.command({
- ...params,
- query,
- })
+ try {
+ if (log) {
+ console.log(chalk.purple('Running query:'), query)
+ }
+ await clickhouse.command({
+ ...params,
+ query,
+ })
- if (log) {
- console.log(chalk.green('Query OK'))
- }
- } catch (error) {
- if (log) {
- console.error(chalk.red('Query ERROR'))
- console.error(error)
- }
- failed = true
+ if (log) {
+ console.log(chalk.green('Query OK'))
+ }
+ } catch (error) {
+ if (log) {
+ console.error(chalk.red('Query ERROR'))
+ console.error(error)
}
+ throw error
}
}
diff --git a/web/app/pages/Project/Settings/components/BotProtectionReport.tsx b/web/app/pages/Project/Settings/components/BotProtectionReport.tsx
index f54805268..29ddac5de 100644
--- a/web/app/pages/Project/Settings/components/BotProtectionReport.tsx
+++ b/web/app/pages/Project/Settings/components/BotProtectionReport.tsx
@@ -77,14 +77,6 @@ const BotProtectionReport = ({ pid }: BotProtectionReportProps) => {
const total = stats?.total ?? 0
const formattedTotal = useMemo(() => nFormatter(total, 2), [total])
- const reasonTotal = useMemo(
- () => (stats?.byReason ?? []).reduce((sum, r) => sum + r.count, 0),
- [stats],
- )
- const countryTotal = useMemo(
- () => (stats?.byCountry ?? []).reduce((sum, r) => sum + r.count, 0),
- [stats],
- )
const byReason = stats?.byReason ?? []
const byCountry = stats?.byCountry ?? []
@@ -188,7 +180,7 @@ const BotProtectionReport = ({ pid }: BotProtectionReportProps) => {
</Text>
}
count={row.count}
- total={reasonTotal}
+ total={total}
/>
))}
</ReportColumn>
@@ -208,7 +200,7 @@ const BotProtectionReport = ({ pid }: BotProtectionReportProps) => {
</span>
}
count={row.count}
- total={countryTotal}
+ total={total}
/>
))}
</ReportColumn>
@@ -220,7 +212,7 @@ const BotProtectionReport = ({ pid }: BotProtectionReportProps) => {
{!error ? (
<div className='flex justify-end border-t border-gray-200 px-4 py-2 dark:border-slate-800/60'>
<a
- href='https://docs.swetrix.com/analytics-dashboard/bot-protection'
+ href='https://docs.swetrix.com/bot-protection'
target='_blank'
rel='noreferrer noopener'
className='inline-flex items-center gap-1.5 text-xs font-medium text-gray-500 underline-offset-2 hover:text-gray-900 hover:underline dark:text-slate-400 dark:hover:text-slate-100'
diff --git a/web/app/pages/Project/Settings/tabs/Shields.tsx b/web/app/pages/Project/Settings/tabs/Shields.tsx
index e778ee3c0..04edc3866 100644
--- a/web/app/pages/Project/Settings/tabs/Shields.tsx
+++ b/web/app/pages/Project/Settings/tabs/Shields.tsx
@@ -150,9 +150,7 @@ const Shields = ({
<Select<BotsProtectionLevelOption>
id='botsProtectionLevel'
label={t('project.settings.botsProtectionLevel.title')}
- items={
- botsProtectionLevels as readonly BotsProtectionLevelOption[] as BotsProtectionLevelOption[]
- }
+ items={botsProtectionLevels}
title={
botsProtectionLevels.find(
(predicate) => predicate.name === form.botsProtectionLevel,
diff --git a/web/app/ui/Select.tsx b/web/app/ui/Select.tsx
index e25abba5a..6d42a92c5 100644
--- a/web/app/ui/Select.tsx
+++ b/web/app/ui/Select.tsx
@@ -22,7 +22,7 @@ interface SelectProps<T> {
buttonClassName?: string
hintClassName?: string
capitalise?: boolean
- items: T[]
+ items: readonly T[]
id?: string
labelExtractor?: (item: T, index: number) => React.ReactNode
keyExtractor?: (item: T, index: number) => string