C# 如何验证/清理整型,以便我验证/清理整型
我有一些代码可以从一个不可信的源代码(我控制的一个文件)中检索数字数据,在访问两者之间的“不安全”缓冲区之前,我将其转换为一对整数,并使用类似的函数对它们进行范围检查C# 如何验证/清理整型,以便我验证/清理整型,c#,checkmarx,secure-coding,insecuredeserialization,C#,Checkmarx,Secure Coding,Insecuredeserialization,我有一些代码可以从一个不可信的源代码(我控制的一个文件)中检索数字数据,在访问两者之间的“不安全”缓冲区之前,我将其转换为一对整数,并使用类似的函数对它们进行范围检查 int ValidateInt(int value, int min, int max) { if(value < min) { throw new ArgumentOutOfRangeException("value", value, "Too small"); }
int ValidateInt(int value, int min, int max)
{
if(value < min) { throw new ArgumentOutOfRangeException("value", value, "Too small"); }
if(value > max) { throw new ArgumentOutOfRangeException("value", value, "Too big"); }
return value;
}
void AccessBuffer(IntPtr buffer, int bufferLengthInInt32s, int lowerBound, int upperBound)
{
lowerBound = ValidateInt(lowerBound, 0, bufferLengthInInt32s-1);
upperBound = ValidateInt(upperBound, 0, bufferLengthInInt32s-1);
for(int index=lowerBound ; index<upperBound ; index++)
DoSomething(Marshal.ReadInt32(buffer, index*4));
}
int ValidateInt(int值、int最小值、int最大值)
{
如果(valuemax){抛出新的ArgumentOutOfRangeException(“value”,value,“太大”);}
返回值;
}
void AccessBuffer(IntPtr buffer,int bufferlengthint32s,int lowerBound,int upperBound)
{
lowerBound=ValidateInt(lowerBound,0,BufferLengthint32s-1);
上限=ValidateInt(上限,0,BufferLengthint32s-1);
对于(int index=lowerBound;index整数验证应该已经被识别并且已经足够了,但我认为Checkmarx不会将其识别为一种净化剂。您可以通过使用重写反序列化不受信任数据Checkmarx查询来更改此行为
Checkmarx能够识别的是对方法的调用。我建议从不同的哈希算法(建议使用强哈希算法)调用任何可用的ComputeHash方法,以替换当前的整数检查
SHA256 hashAlgorithm = SHA256.Create();
var lowerBoundHash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(lowerBound));
var upperBoundHash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(upperBound));
//if (upperBoundHash != expectedUpperBoundHash) {}
//if (lowerBoundHash != expectedLowerBoundHash) {}
或者,您可以简单地向您的安全团队指出,由于存在整数验证,应该将其标记为不可利用