Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
API security updates.
Mike Cao8e3286179a46
diff --git a/pages/api/account/[id].js b/pages/api/account/[id].jsindex b8fe22458..c87f948b1 100644--- a/pages/api/account/[id].js+++ b/pages/api/account/[id].js@@ -9,24 +9,20 @@ export default async (req, res) => {const { id } = req.query;const user_id = +id;- if (req.method === 'GET') {- if (is_admin) {- const account = await getAccountById(user_id);+ if (is_admin) {+ return unauthorized(res);+ }- return ok(res, account);- }+ if (req.method === 'GET') {+ const account = await getAccountById(user_id);- return unauthorized(res);+ return ok(res, account);}if (req.method === 'DELETE') {- if (is_admin) {- await deleteAccount(user_id);-- return ok(res);- }+ await deleteAccount(user_id);- return unauthorized(res);+ return ok(res);}return methodNotAllowed(res);diff --git a/pages/api/account/password.js b/pages/api/account/password.jsindex 32f87960b..c9c955fa4 100644--- a/pages/api/account/password.js+++ b/pages/api/account/password.js@@ -1,14 +1,18 @@import { getAccountById, updateAccount } from 'lib/queries';import { useAuth } from 'lib/middleware';-import { badRequest, methodNotAllowed, ok } from 'lib/response';+import { badRequest, methodNotAllowed, ok, unauthorized } from 'lib/response';import { checkPassword, hashPassword } from 'lib/crypto';export default async (req, res) => {await useAuth(req, res);- const { user_id } = req.auth;+ const { user_id, is_admin } = req.auth;const { current_password, new_password } = req.body;+ if (is_admin) {+ return unauthorized(res);+ }+if (req.method === 'POST') {const account = await getAccountById(user_id);const valid = await checkPassword(current_password, account.password);diff --git a/pages/api/website/[id]/active.js b/pages/api/website/[id]/active.jsindex 06731408c..492074a9e 100644--- a/pages/api/website/[id]/active.js+++ b/pages/api/website/[id]/active.js@@ -1,11 +1,18 @@import { getActiveVisitors } from 'lib/queries';-import { ok } from 'lib/response';+import { methodNotAllowed, ok } from 'lib/response';+import { useAuth } from 'lib/middleware';export default async (req, res) => {- const { id } = req.query;- const website_id = +id;+ await useAuth(req, res);- const result = await getActiveVisitors(website_id);+ if (req.method === 'GET') {+ const { id } = req.query;+ const website_id = +id;- return ok(res, result);+ const result = await getActiveVisitors(website_id);++ return ok(res, result);+ }++ return methodNotAllowed(res);};diff --git a/pages/api/website/[id]/events.js b/pages/api/website/[id]/events.jsindex d7ead27c5..c230acd1c 100644--- a/pages/api/website/[id]/events.js+++ b/pages/api/website/[id]/events.js@@ -1,21 +1,28 @@import moment from 'moment-timezone';import { getEvents } from 'lib/queries';-import { ok, badRequest } from 'lib/response';+import { ok, badRequest, methodNotAllowed } from 'lib/response';+import { useAuth } from 'lib/middleware';const unitTypes = ['month', 'hour', 'day'];export default async (req, res) => {- const { id, start_at, end_at, unit, tz } = req.query;+ await useAuth(req, res);- if (!moment.tz.zone(tz) || !unitTypes.includes(unit)) {- return badRequest(res);- }+ if (req.method === 'GET') {+ const { id, start_at, end_at, unit, tz } = req.query;++ if (!moment.tz.zone(tz) || !unitTypes.includes(unit)) {+ return badRequest(res);+ }- const websiteId = +id;- const startDate = new Date(+start_at);- const endDate = new Date(+end_at);+ const websiteId = +id;+ const startDate = new Date(+start_at);+ const endDate = new Date(+end_at);- const events = await getEvents(websiteId, startDate, endDate, tz, unit);+ const events = await getEvents(websiteId, startDate, endDate, tz, unit);++ return ok(res, events);+ }- return ok(res, events);+ return methodNotAllowed(res);};diff --git a/pages/api/website/[id]/metrics.js b/pages/api/website/[id]/metrics.jsindex 4b0d71e11..70a9d1e63 100644--- a/pages/api/website/[id]/metrics.js+++ b/pages/api/website/[id]/metrics.js@@ -1,18 +1,25 @@import { getMetrics } from 'lib/queries';-import { ok } from 'lib/response';+import { methodNotAllowed, ok } from 'lib/response';+import { useAuth } from 'lib/middleware';export default async (req, res) => {- const { id, start_at, end_at } = req.query;- const websiteId = +id;- const startDate = new Date(+start_at);- const endDate = new Date(+end_at);+ await useAuth(req, res);- const metrics = await getMetrics(websiteId, startDate, endDate);+ if (req.method === 'GET') {+ const { id, start_at, end_at } = req.query;+ const websiteId = +id;+ const startDate = new Date(+start_at);+ const endDate = new Date(+end_at);- const stats = Object.keys(metrics[0]).reduce((obj, key) => {- obj[key] = Number(metrics[0][key]) || 0;- return obj;- }, {});+ const metrics = await getMetrics(websiteId, startDate, endDate);- return ok(res, stats);+ const stats = Object.keys(metrics[0]).reduce((obj, key) => {+ obj[key] = Number(metrics[0][key]) || 0;+ return obj;+ }, {});++ return ok(res, stats);+ }++ return methodNotAllowed(res);};diff --git a/pages/api/website/[id]/pageviews.js b/pages/api/website/[id]/pageviews.jsindex c90e1c6b5..ff3b731ee 100644--- a/pages/api/website/[id]/pageviews.js+++ b/pages/api/website/[id]/pageviews.js@@ -1,24 +1,31 @@import moment from 'moment-timezone';import { getPageviews } from 'lib/queries';-import { ok, badRequest } from 'lib/response';+import { ok, badRequest, methodNotAllowed } from 'lib/response';+import { useAuth } from 'lib/middleware';const unitTypes = ['month', 'hour', 'day'];export default async (req, res) => {- const { id, start_at, end_at, unit, tz } = req.query;+ await useAuth(req, res);- if (!moment.tz.zone(tz) || !unitTypes.includes(unit)) {- return badRequest(res);- }+ if (req.method === 'GET') {+ const { id, start_at, end_at, unit, tz } = req.query;++ if (!moment.tz.zone(tz) || !unitTypes.includes(unit)) {+ return badRequest(res);+ }- const websiteId = +id;- const startDate = new Date(+start_at);- const endDate = new Date(+end_at);+ const websiteId = +id;+ const startDate = new Date(+start_at);+ const endDate = new Date(+end_at);- const [pageviews, uniques] = await Promise.all([- getPageviews(websiteId, startDate, endDate, tz, unit, '*'),- getPageviews(websiteId, startDate, endDate, tz, unit, 'distinct session_id'),- ]);+ const [pageviews, uniques] = await Promise.all([+ getPageviews(websiteId, startDate, endDate, tz, unit, '*'),+ getPageviews(websiteId, startDate, endDate, tz, unit, 'distinct session_id'),+ ]);++ return ok(res, { pageviews, uniques });+ }- return ok(res, { pageviews, uniques });+ return methodNotAllowed(res);};diff --git a/pages/api/website/[id]/rankings.js b/pages/api/website/[id]/rankings.jsindex 4e613d0d9..158a4bc28 100644--- a/pages/api/website/[id]/rankings.js+++ b/pages/api/website/[id]/rankings.js@@ -1,6 +1,7 @@import { getRankings } from 'lib/queries';-import { ok, badRequest } from 'lib/response';-import { DOMAIN_REGEX } from '../../../../lib/constants';+import { ok, badRequest, methodNotAllowed } from 'lib/response';+import { DOMAIN_REGEX } from 'lib/constants';+import { useAuth } from 'lib/middleware';const sessionColumns = ['browser', 'os', 'device', 'country'];const pageviewColumns = ['url', 'referrer'];@@ -25,29 +26,35 @@ function getColumn(type) {}export default async (req, res) => {- const { id, type, start_at, end_at, domain } = req.query;- const websiteId = +id;- const startDate = new Date(+start_at);- const endDate = new Date(+end_at);-- if (- type !== 'event' &&- !sessionColumns.includes(type) &&- !pageviewColumns.includes(type) &&- domain &&- DOMAIN_REGEX.test(domain)- ) {- return badRequest(res);+ await useAuth(req, res);++ if (req.method === 'GET') {+ const { id, type, start_at, end_at, domain } = req.query;+ const websiteId = +id;+ const startDate = new Date(+start_at);+ const endDate = new Date(+end_at);++ if (+ type !== 'event' &&+ !sessionColumns.includes(type) &&+ !pageviewColumns.includes(type) &&+ domain &&+ DOMAIN_REGEX.test(domain)+ ) {+ return badRequest(res);+ }++ const rankings = await getRankings(+ websiteId,+ startDate,+ endDate,+ getColumn(type),+ getTable(type),+ domain,+ );++ return ok(res, rankings);}- const rankings = await getRankings(- websiteId,- startDate,- endDate,- getColumn(type),- getTable(type),- domain,- );-- return ok(res, rankings);+ return methodNotAllowed(res);};