|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @fileoverview Rule to enforce wrapping random/time APIs with withRandomSafeContext |
| 5 | + * |
| 6 | + * This rule detects uses of APIs that generate random values or time-based values |
| 7 | + * and ensures they are wrapped with `withRandomSafeContext()` to ensure safe |
| 8 | + * random number generation in certain contexts (e.g., React Server Components with caching). |
| 9 | + */ |
| 10 | + |
| 11 | +// APIs that should be wrapped with withRandomSafeContext, with their specific messages |
| 12 | +const UNSAFE_MEMBER_CALLS = [ |
| 13 | + { |
| 14 | + object: 'Date', |
| 15 | + property: 'now', |
| 16 | + messageId: 'unsafeDateNow', |
| 17 | + }, |
| 18 | + { |
| 19 | + object: 'Math', |
| 20 | + property: 'random', |
| 21 | + messageId: 'unsafeMathRandom', |
| 22 | + }, |
| 23 | + { |
| 24 | + object: 'performance', |
| 25 | + property: 'now', |
| 26 | + messageId: 'unsafePerformanceNow', |
| 27 | + }, |
| 28 | + { |
| 29 | + object: 'crypto', |
| 30 | + property: 'randomUUID', |
| 31 | + messageId: 'unsafeCryptoRandomUUID', |
| 32 | + }, |
| 33 | + { |
| 34 | + object: 'crypto', |
| 35 | + property: 'getRandomValues', |
| 36 | + messageId: 'unsafeCryptoGetRandomValues', |
| 37 | + }, |
| 38 | +]; |
| 39 | + |
| 40 | +module.exports = { |
| 41 | + meta: { |
| 42 | + type: 'problem', |
| 43 | + docs: { |
| 44 | + description: |
| 45 | + 'Enforce wrapping random/time APIs (Date.now, Math.random, performance.now, crypto.randomUUID) with withRandomSafeContext', |
| 46 | + category: 'Best Practices', |
| 47 | + recommended: true, |
| 48 | + }, |
| 49 | + fixable: null, |
| 50 | + schema: [], |
| 51 | + messages: { |
| 52 | + unsafeDateNow: |
| 53 | + '`Date.now()` should be replaced with `safeDateNow()` from `@sentry/core` to ensure safe time value generation. You can disable this rule with an eslint-disable comment if this usage is intentional.', |
| 54 | + unsafeMathRandom: |
| 55 | + '`Math.random()` should be replaced with `safeMathRandom()` from `@sentry/core` to ensure safe random value generation. You can disable this rule with an eslint-disable comment if this usage is intentional.', |
| 56 | + unsafePerformanceNow: |
| 57 | + '`performance.now()` should be wrapped with `withRandomSafeContext()` to ensure safe time value generation. Use: `withRandomSafeContext(() => performance.now())`. You can disable this rule with an eslint-disable comment if this usage is intentional.', |
| 58 | + unsafeCryptoRandomUUID: |
| 59 | + '`crypto.randomUUID()` should be wrapped with `withRandomSafeContext()` to ensure safe random value generation. Use: `withRandomSafeContext(() => crypto.randomUUID())`. You can disable this rule with an eslint-disable comment if this usage is intentional.', |
| 60 | + unsafeCryptoGetRandomValues: |
| 61 | + '`crypto.getRandomValues()` should be wrapped with `withRandomSafeContext()` to ensure safe random value generation. Use: `withRandomSafeContext(() => crypto.getRandomValues(...))`. You can disable this rule with an eslint-disable comment if this usage is intentional.', |
| 62 | + }, |
| 63 | + }, |
| 64 | + create: function (context) { |
| 65 | + /** |
| 66 | + * Check if a node is inside a withRandomSafeContext call |
| 67 | + */ |
| 68 | + function isInsidewithRandomSafeContext(node) { |
| 69 | + let current = node.parent; |
| 70 | + |
| 71 | + while (current) { |
| 72 | + // Check if we're inside a callback passed to withRandomSafeContext |
| 73 | + if ( |
| 74 | + current.type === 'CallExpression' && |
| 75 | + current.callee.type === 'Identifier' && |
| 76 | + current.callee.name === 'withRandomSafeContext' |
| 77 | + ) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + // Also check for arrow functions or regular functions passed to withRandomSafeContext |
| 82 | + if ( |
| 83 | + (current.type === 'ArrowFunctionExpression' || current.type === 'FunctionExpression') && |
| 84 | + current.parent?.type === 'CallExpression' && |
| 85 | + current.parent.callee.type === 'Identifier' && |
| 86 | + current.parent.callee.name === 'withRandomSafeContext' |
| 87 | + ) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + current = current.parent; |
| 92 | + } |
| 93 | + |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Check if a node is inside the safeRandomGeneratorRunner.ts file (the definition file) |
| 99 | + */ |
| 100 | + function isInSafeRandomGeneratorRunner(_node) { |
| 101 | + const filename = context.getFilename(); |
| 102 | + return filename.includes('safeRandomGeneratorRunner'); |
| 103 | + } |
| 104 | + |
| 105 | + return { |
| 106 | + CallExpression(node) { |
| 107 | + // Skip if we're in the safeRandomGeneratorRunner.ts file itself |
| 108 | + if (isInSafeRandomGeneratorRunner(node)) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + // Check for member expression calls like Date.now(), Math.random(), etc. |
| 113 | + if (node.callee.type === 'MemberExpression') { |
| 114 | + const callee = node.callee; |
| 115 | + |
| 116 | + // Get the object name (e.g., 'Date', 'Math', 'performance', 'crypto') |
| 117 | + let objectName = null; |
| 118 | + if (callee.object.type === 'Identifier') { |
| 119 | + objectName = callee.object.name; |
| 120 | + } |
| 121 | + |
| 122 | + // Get the property name (e.g., 'now', 'random', 'randomUUID') |
| 123 | + let propertyName = null; |
| 124 | + if (callee.property.type === 'Identifier') { |
| 125 | + propertyName = callee.property.name; |
| 126 | + } else if (callee.computed && callee.property.type === 'Literal') { |
| 127 | + propertyName = callee.property.value; |
| 128 | + } |
| 129 | + |
| 130 | + if (!objectName || !propertyName) { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + // Check if this is one of the unsafe APIs |
| 135 | + const unsafeApi = UNSAFE_MEMBER_CALLS.find(api => api.object === objectName && api.property === propertyName); |
| 136 | + |
| 137 | + if (unsafeApi && !isInsidewithRandomSafeContext(node)) { |
| 138 | + context.report({ |
| 139 | + node, |
| 140 | + messageId: unsafeApi.messageId, |
| 141 | + }); |
| 142 | + } |
| 143 | + } |
| 144 | + }, |
| 145 | + }; |
| 146 | + }, |
| 147 | +}; |
0 commit comments