Incorrect Type Conversion or Cast
The product does not correctly convert an object, resource, or structure from one type to a different type.
Type conversions often have implications for resource and bounds checking. When types are not converted properly, this can lead to access of out-of-bounds memory or misinterpretation of data.
Comment corriger cette vulnérabilité
Stratégies de prévention pour Incorrect Type Conversion basées sur 5 règles de détection Shoulder.
Use Zod schemas with type inference instead of 'any' to maintain end-to-end type safety in tRPC
import { router, publicProcedure } from './trpc'; - - export const postRouter = router({ - createPost: publicProcedure - .mutation(async ({ input }: { input: any }) => { - return await db.post.create({ data: input }); - }), - - getPost: publicProcedure - .query(async ({ input }: any) => { + import { z } from 'zod'; + + const createPostInput = z.object({ + title: z.string().min(1).max(200), + content: z.string().min(1), + published: z.boolean().default(false), + }); + + export const postRouter = router({ + createPost: publicProcedure + .input(createPostInput) + .mutation(async ({ input }) => { + // input is typed as { title: string; content: string; published: boolean } + return await db.post.create({ data: input }); + }), + + getPost: publicProcedure + .input(z.object({ postId: z.number().int().positive() })) + .query(async ({ input }) => { return await db.post.findUnique({ where: { id: input.postId }, }); }), });
Add type constraints using 'extends' to ensure generic parameters have required properties
- function getIdentifier<T>(entity: T): string { - return entity.id.toString(); // T has no guaranteed 'id' property - } - - function processEntities<T>(items: T[]): void { - items.forEach(item => { - console.log(item.name); // Runtime error if 'name' missing + interface Identifiable { + id: number | string; + } + + interface Named { + name: string; + } + + function getIdentifier<T extends Identifiable>(entity: T): string { + return entity.id.toString(); + } + + function processEntities<T extends Named>(items: T[]): void { + items.forEach(item => { + console.log(item.name); }); }
Enable strict mode in tsconfig.json to activate all strict type-checking options
{ "compilerOptions": { "target": "ES2020", "module": "commonjs", - "strict": false, - "strictNullChecks": false, - "noImplicitAny": false + "strict": true, + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true } }
Trouvez les vulnérabilités dans votre code
Utilisez Shoulder pour scanner votre code à la recherche de patterns Incorrect Type Conversion or Cast. 5 règles.
# Scan with Shoulder CLI npx @shoulderdev/cli trust --cwe=704 # Or scan entire project npx @shoulderdev/cli trust .
Règles de Détection (5)
Ce qu'il faut surveiller lors des revues de code
Ces patterns indiquent des vulnérabilités potentielles Incorrect Type Conversion or Cast. Recherchez-les lors des revues de code et des audits de sécurité.
Scannez votre base de code pour Incorrect Type Conversion or Cast
Shoulder CLI trouve les motifs vulnérables dans toute votre base de code.