安全编码规范

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
public static void main(String[] args) {
System.out.println("The account balance is: " + c.balance);
}
}
类加载时初始化指向Cycle类的静态变量c,而类Cycle的无参构造方法又依赖静态变量deposit,导致无法预期的结果。
if (matcher.find()) {
// Found black listed tag
throw new IllegalStateException();
} else {
// ...
}
1.2
1.2.1
例:
错误的写法:
public class Cycle {
private final int balance;
正确的写法:
public class Cycle {
private final int balance;
private static final int deposit = (int) (Math.random() * 100); // Random deposit
private static final Cycle c = new Cycle(); // Inserted after initialization of required fields
安全编码规范
版本号:V1.0
修订页
编号
章节
名称
修订内容简述
修订
日期
修订前
版本号
修订后
版本号
修订人
批准人
1
1.1
程序接受数据可能来源于未经验证的用户,网络连接和其他不受信任的来源,如果未对程序接受数据进行校验,则可能会引发安全问题。
1.1.1
使用PreparedStatement预编译SQL,解决SQL注入问题,传递给PreparedStatement对象的参数可以被强制进行类型转换,确保在插入或查询数据时与底层的数据库格式匹配。
// Format violation
}
String xmlString = "<item>\n<description>Widget</description>\n" +
"<price>500</price>\n" +
"<quantity>" + quantity + "</quantity></item>";
String sqlString = "select * from db_user where username=? and password=?";
PreparedStatement stmt = connection.prepareStatement(sqlString);
stmt.setString(1, username);
int[] arr1 = new int[20]; // initialized to 0
int[] arr2 = new int[20]; // initialized to 0
//过滤字符串标准化
s = Normalizer.normalize(s, Form.NFKC);
//使用正则表达式匹配inputStr是否存在<script>
Pattern pattern = Pattern.compile(inputStr);
Matcher matcher = pattern.matcher(s);
在使用变量前一定要做是否为NULL值的校验。
1.3.3
数组没有覆盖的Object.equals()方法,调用Object.equals()方法实际上是比较数组的引用,而不是他们的内容。程序必须使用两个参数Arrays.equals()方法来比较两个数组的内容
public void arrayEqualsExample() {
outStream.write(xmlString.getBytes());
outStream.flush();
1.1.3
对产生跨站的参数进行严格过滤,禁止传入<SCRIPT>标签
//定义需过滤的字段串<script>
String s = "\uFE64" + "script" + "\uFE65";
private static final Cycle c = new Cycle();
private static final int deposit = (int) (Math.random() * 100); // Random deposit
public Cycle() {
balance = deposit - 10; // Subtract processing fee
stmt.setString(2, pwd);
ResultSet rs = stmt.executeQuery();
1.1.2
通过StringBulider或StringBuffer拼接XML文件时,需对输入数据进行合法性校验。
对数量quantity进行合法性校验,控制只能传入0-9的数字:
if (!Pattern.matches("[0-9]+", quantity)) {
File someFile = new File("someFileName.txt");
if (!someFile.delete()) {
// handle failure to delete the file
}
}
1.3.2
当一个变量指向一个NULL值,使用这个变量的时候又没有检查,这时会导致。NullPointerException。
}
}
1.3
1.3.1
忽略方法的放回值可能会导致无法预料的结果。
错误的写法:
public void deleteFile(){
File someFile = new File("someFileName.txt");
someFile.delete();
}来自百度文库
正确的写法:
public void deleteFile(){
public Cycle() {
balance = deposit - 10; // Subtract processing fee
}
public static void main(String[] args) {
System.out.println("The account balance is: " + c.balance);
相关文档
最新文档