Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid website in cache #3128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/lib/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ export async function getSession(req: NextApiRequestCollect): Promise<SessionDat
const cacheToken = req.headers['x-umami-cache'];

if (cacheToken) {
const result = await parseToken(cacheToken, secret());
const result: SessionData | undefined = await parseToken(cacheToken, secret());

// Token is valid
if (result) {
// website may differ in payload
if (result.websiteId !== payload.website) {
await checkWebsite(payload.website);
result.websiteId = payload.website;
}

return result;
}
}

// Verify payload
const { website: websiteId, hostname, screen, language } = payload;

// Find website
const website = await fetchWebsite(websiteId);

if (!website) {
throw new Error(`Website not found: ${websiteId}.`);
}
// check website
await checkWebsite(websiteId);

const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
await getClientInfo(req);
Expand Down Expand Up @@ -90,3 +92,12 @@ export async function getSession(req: NextApiRequestCollect): Promise<SessionDat

return { ...session, visitId };
}

const checkWebsite = async (websiteId: string) => {
// Find website
const website = await fetchWebsite(websiteId);

if (!website) {
throw new Error(`Website not found: ${websiteId}.`);
}
};