{"record":{"id":"d9ff9fb63ef90389","repo":"mastra-ai/mastra","slug":"page-value-too-large","errorCode":null,"errorMessage":"page value too large","messagePattern":"page value too large","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/agents/inmemory.ts","lineNumber":145,"sourceCode":"      metadata,\n      status,\n      entityIds,\n      pinFavoritedFor,\n      favoritedOnly,\n    } = args || {};\n    const { field, direction } = this.parseOrderBy(orderBy);\n\n    // Normalize perPage for query (false → MAX_SAFE_INTEGER, 0 → 0, undefined → 100)\n    const perPage = normalizePerPage(perPageInput, 100);\n\n    if (page < 0) {\n      throw new Error('page must be >= 0');\n    }\n\n    // Prevent unreasonably large page values\n    const maxOffset = Number.MAX_SAFE_INTEGER / 2;\n    if (page * perPage > maxOffset) {\n      throw new Error('page value too large');\n    }\n\n    // Get all agents and apply filters\n    let agents = Array.from(this.db.agents.values());\n\n    // Restrict to a set of IDs (used by ?favoritedOnly=true).\n    // An empty array means \"no candidates\" -> empty result.\n    if (entityIds !== undefined) {\n      if (entityIds.length === 0) {\n        return {\n          agents: [],\n          total: 0,\n          page,\n          perPage: perPageInput === false ? false : perPage,\n          hasMore: false,\n        };\n      }\n      const idSet = new Set(entityIds);","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/agents/inmemory.ts#L127-L163","documentation":"`list` guards against unreasonably large pagination by rejecting `page * perPage` offsets above `Number.MAX_SAFE_INTEGER / 2`, since such offsets cannot be represented safely and would never return meaningful data.","triggerScenarios":"Calling `list({ page: <huge number> })`, e.g. `perPage: false` (normalized to MAX_SAFE_INTEGER) combined with any page > 0, or passing attacker-controlled / erroneous page numbers.","commonSituations":"Using `perPage: false` (fetch-all) with a nonzero page; unbounded loop incrementing `page` on empty results; malicious query params requesting page=1e15.","solutions":["Keep page numbers small and paginate normally; use `perPage: false` only with page 0 for fetch-all","Cap page at a sane maximum before calling `list`","Fix pagination loops to stop when results are empty instead of incrementing indefinitely"],"exampleFix":"// before\nawait agents.list({ page: 2, perPage: false });\n// after\nawait agents.list({ page: 0, perPage: false }); // fetch-all must use page 0","handlingStrategy":"validation","validationCode":"const MAX_OFFSET = Number.MAX_SAFE_INTEGER / 2;\nif (page * perPage > MAX_OFFSET) {\n  throw new Error('page/perPage combination too large');\n}\nawait agentsDomain.list({ page, perPage });","typeGuard":"null","tryCatchPattern":"try {\n  return await agentsDomain.list({ page, perPage });\n} catch (e) {\n  if (e instanceof Error && e.message === 'page value too large') {\n    return agentsDomain.list({ page: 0, perPage: 100 });\n  }\n  throw e;\n}","preventionTips":["Only use perPage: false (fetch-all) with page 0","Cap page numbers before calling list","Terminate pagination loops when a page comes back empty","Never forward unbounded client page params straight to storage"],"tags":["storage","pagination","validation"],"backgroundTag":"invalid-pagination-parameters","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}