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.
普遍性
中
覆盖 1 种语言
影响
高
3 条严重级别为高的规则
预防
已记录
5 个修复示例
2 预防
2 预防
如何修复此漏洞
基于 5 条 Shoulder 检测规则的 Incorrect Type Conversion 预防策略。
JavaScript
查看全部 JavaScript 详情 →
tRPC Type Safety Bypass with Any
MEDIUM
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 }, }); }), });
TypeScript Unconstrained Generic Type Parameters
MEDIUM
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); }); }
TypeScript Strict Mode Disabled
HIGH
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 } }
3 检测
3 检测
查找代码中的漏洞
使用Shoulder扫描代码中的Incorrect Type Conversion or Cast模式。 5 规则.
# Scan with Shoulder CLI npx @shoulderdev/cli trust --cwe=704 # Or scan entire project npx @shoulderdev/cli trust .
检测规则 (5)
🔷
Typescript
5 rules
tRPC Type Safety Bypass with Any
MEDIUM
Using 'any' type in tRPC procedures defeats type safety and allows unvalidated
data to pass through, enabling injection and runtime errors.
TypeScript Unconstrained Generic Type Parameters
MEDIUM
Unconstrained generics (<T> or <T extends any>) allow any type to pass through,
causing runtime errors and type confusion when accessing properties that do not exist.
TypeScript Strict Mode Disabled
HIGH
Disabled TypeScript strict mode flags weaken type safety and allow null/undefined errors,
implicit any types, and unsafe function parameters that lead to runtime vulnerabilities.
Unsafe 'any' Type in Security-Sensitive Context
HIGH
Using 'any' type with untrusted input bypasses TypeScript's type safety, allowing
unvalidated data to flow into security-sensitive operations.
TypeScript Unsafe Type Guard
HIGH
Type guards that always return true or use assertions without validation create
type confusion, allowing untrusted data to bypass security checks.
4 警告信号
4 警告信号
代码审查中需要关注的内容
这些模式表明潜在的Incorrect Type Conversion or Cast漏洞。在代码审查和安全审计中注意查找。
tsconfig.json has '...' disabled. Enable strict mode for better type safety and security.
typescript-strict-mode-violations
Variable declared with 'any' type receives untrusted input from .... This bypasses TypeScript's type safety and may lead
typescript-unsafe-any-usage
Type guard '...' uses 'is' predicate but lacks proper runtime validation. This creates type confusion vulnerabilities.
typescript-unsafe-type-guard
tRPC code uses 'any' type which defeats type safety. Use proper TypeScript types or Zod inference.
trpc-type-inference-bypass
Generic type parameter '...' lacks constraints. Add 'extends' constraint to ensure type safety.
typescript-generic-constraint-bypass