Skip to content

Commit

Permalink
Fix session cache poisoning
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGinnivan committed Aug 20, 2024
1 parent bf35e36 commit a2331be
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions website/app/lib/sessionize.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import { z } from 'zod'

const NO_CACHE = process.env.NO_CACHE != null ? process.env.NO_CACHE === 'true' : undefined
// const SPEAKERS_CACHE_KEY = 'speakers'
const SESSIONS_CACHE_KEY = 'sessions'
const SCHEDULE_CACHE_KEY = 'schedule'

const scheduleCache = new LRUCache<typeof SCHEDULE_CACHE_KEY, z.infer<typeof gridSmartSchema>>({
const scheduleCache = new LRUCache<string, z.infer<typeof gridSmartSchema>>({
max: 250,
maxSize: 1024 * 1024 * 12, // 12 mb
ttl: 1000 * 60 * 60 * 24, // 24 hours
sizeCalculation(value, key) {
return JSON.stringify(value).length + (key ? key.length : 0)
},
})
const sessionsCache = new LRUCache<typeof SESSIONS_CACHE_KEY, z.infer<typeof sessionsSchema>>({
const sessionsCache = new LRUCache<string, z.infer<typeof sessionsSchema>>({
max: 250,
maxSize: 1024 * 1024 * 12, // 12 mb
ttl: 1000 * 60 * 60 * 24, // 24 hours
Expand Down Expand Up @@ -93,7 +91,7 @@ export const sessionsSchema = z.array(
export async function getScheduleGrid(opts: Options): Promise<z.infer<typeof gridSmartSchema>> {
const { noCache = NO_CACHE ?? false } = opts
if (!noCache) {
const cached = scheduleCache.get(SCHEDULE_CACHE_KEY)
const cached = scheduleCache.get(opts.sessionizeEndpoint)
if (cached) {
return cached as z.infer<typeof gridSmartSchema>
}
Expand All @@ -117,7 +115,7 @@ export async function getScheduleGrid(opts: Options): Promise<z.infer<typeof gri
const schedule = gridSmartSchema.parse(json)

if (!noCache) {
scheduleCache.set(SCHEDULE_CACHE_KEY, schedule)
scheduleCache.set(opts.sessionizeEndpoint, schedule)
}

return schedule
Expand All @@ -126,7 +124,7 @@ export async function getScheduleGrid(opts: Options): Promise<z.infer<typeof gri
export async function getConfSessions(opts: Options): Promise<z.infer<typeof sessionsSchema>> {
const { noCache = NO_CACHE ?? false } = opts
if (!noCache) {
const cached = sessionsCache.get(SESSIONS_CACHE_KEY)
const cached = sessionsCache.get(opts.sessionizeEndpoint)
if (cached) {
return cached as z.infer<typeof sessionsSchema>
}
Expand All @@ -151,7 +149,7 @@ export async function getConfSessions(opts: Options): Promise<z.infer<typeof ses
const sessions = sessionsSchema.parse(json)

if (!noCache) {
sessionsCache.set(SESSIONS_CACHE_KEY, sessions)
sessionsCache.set(opts.sessionizeEndpoint, sessions)
}

return sessions
Expand Down

0 comments on commit a2331be

Please sign in to comment.