Java编码指南 编写安全可靠程序的75条建议(英文版)

目录
Chapter 1 Security / 安全 1
1. Limit the lifetime of sensitive data 2
限制敏感数据的生命周期
2. Do not store unencrypted sensitive information on the client side 5
不要在客户端存储未经加密的敏感数据
3. Provide sensitive mutable classes with unmodifiable wrappers 9
为敏感可变类提供不可修改的包装器
4. Ensure that security-sensitive methods are called with validated arguments 11
确保安全敏感方法被调用时参数经过验证
5. Prevent arbitrary file upload 13
防止任意文件上传
6. Properly encode or escape output 16
正确地编码或转义输出
7. Prevent code injection 20
防止代码注入
8. Prevent XPath injection 23
防止XPath注入
9. Prevent LDAP injection 27
防止LDAP注入
10. Do not use the clone() method to copy untrusted method parameters 31
不要使用clone()方法来复制不可信的方法参数
11. Do not use Object.equals() to compare cryptographic keys 34
不要使用Object.equals()来比较密钥
12. Do not use insecure or weak cryptographic algorithms 36
不要使用不安全的弱加密算法
13. Store passwords using a hash function 37
使用散列函数存储密码
14. Ensure that SecureRandom is properly seeded 42
确保SecureRandom正确地选择随机数种子
15. Do not rely on methods that can be overridden by untrusted code 44
不要依赖可以被不可信代码覆写的方法
16. Avoid granting excess privileges 50
避免授予过多特权
17. Minimize privileged code 54
最小化特权代码
18. Do not expose methods that use reduced-security checks to untrusted code 56
不要将使用降低安全性检查的方法暴露给不可信代码
19. Define custom security permissions for fine-grained security 64
对细粒度的安全定义自定义安全权限
20. Create a secure sandbox using a security manager 67
使用安全管理器创建一个安全的沙盒
21. Do not let untrusted code misuse privileges of callback methods 72
不要让不可信代码误用回调方法的特权
Chapter 2 Defensive Programming / 防御式编程 79
22. Minimize the scope of variables 80
最小化变量的作用域
23. Minimize the scope of the @SuppressWarnings annotation 82
最小化@SuppressWarnings注解的作用域
24. Minimize the accessibility of classes and their members 84
最小化类及其成员的可访问性
25. Document thread-safety and use annotations where applicable 89
文档化代码的线程安全性
26. Always provide feedback about the resulting value of a method 96
为方法的结果值提供反馈
27. Identify files using multiple file attributes 99
使用多个文件属性识别文件
28. Do not attach significance to the ordinal associated with an enum 106
不要赋予枚举常量的序号任何特殊意义
29. Be aware of numeric promotion behavior 108
注意数字提升行为
30. Enable compile-time type checking of variable arity parameter types 112
对可变参数的类型做编译时类型检查
31. Do not apply public final to constants whose value might change in later releases 115
不要把其值在以后版本里可能会发生变化的常量设置为public final
32. Avoid cyclic dependencies between packages 118
避免包之间的循环依赖
33. Prefer user-defined exceptions over more general exception types 121
使用用户自定义的异常而非宽泛的异常类型
34. Try to gracefully recover from system errors 123
尽量从系统错误中优雅恢复
35. Carefully design interfaces before releasing them 125
布接口前请谨慎设计
36. Write garbage collection–friendly code 128
编写对垃圾回收机制友好的代码
Chapter 3 Reliability / 可靠性 131
37. Do not shadow or obscure identifiers in subscopes 132
不要在子作用域里遮蔽或者掩盖标识符
38. Do not declare more than one variable per declaration 134
不要在一个声明里声明多个变量
39. Use meaningful symbolic constantsto represent literal values in program logic 138
在程序逻辑中用有意义的符号常量代表文字值
40. Properly encode relationships in constant definitions 142
在常量定义中恰当地表示相互之间的关系
41. Return an empty array or collection instead of a null value for methods that return an array or collection 143
对于返回数组或者集合的方法,用返回一个空数组或者集合来替代返回一个空值
42. Use exceptions only for exceptional conditions 146
只在异常的情况下使用异常
43. Use a try-with-resources statement to safely handle closeable resources 148
使用try-with-resources语句安全处理可关闭的资源
44. Do not use assertions to verify the absence of runtime errors 151
不要使用断言来验证不存在的运行时错误
45. Use the same type for the second and third operands in conditional expressions 153
在条件表达式中,第二和第三个操作数应使用相同类型
46. Do not serialize direct handles to system resources 157
不要序列化直接指向系统资源的句柄
47. Prefer using iterators over enumerations 159
更倾向于使用迭代器而不是枚举
48. Do not use direct buffers for short-lived, infrequently used objects 162
对于短生存周期、不常用的对象不要使用直接缓冲区
49. Remove short-lived objects from long-lived containe
Chapter 1 Security / 安全 1
1. Limit the lifetime of sensitive data 2
限制敏感数据的生命周期
2. Do not store unencrypted sensitive information on the client side 5
不要在客户端存储未经加密的敏感数据
3. Provide sensitive mutable classes with unmodifiable wrappers 9
为敏感可变类提供不可修改的包装器
4. Ensure that security-sensitive methods are called with validated arguments 11
确保安全敏感方法被调用时参数经过验证
5. Prevent arbitrary file upload 13
防止任意文件上传
6. Properly encode or escape output 16
正确地编码或转义输出
7. Prevent code injection 20
防止代码注入
8. Prevent XPath injection 23
防止XPath注入
9. Prevent LDAP injection 27
防止LDAP注入
10. Do not use the clone() method to copy untrusted method parameters 31
不要使用clone()方法来复制不可信的方法参数
11. Do not use Object.equals() to compare cryptographic keys 34
不要使用Object.equals()来比较密钥
12. Do not use insecure or weak cryptographic algorithms 36
不要使用不安全的弱加密算法
13. Store passwords using a hash function 37
使用散列函数存储密码
14. Ensure that SecureRandom is properly seeded 42
确保SecureRandom正确地选择随机数种子
15. Do not rely on methods that can be overridden by untrusted code 44
不要依赖可以被不可信代码覆写的方法
16. Avoid granting excess privileges 50
避免授予过多特权
17. Minimize privileged code 54
最小化特权代码
18. Do not expose methods that use reduced-security checks to untrusted code 56
不要将使用降低安全性检查的方法暴露给不可信代码
19. Define custom security permissions for fine-grained security 64
对细粒度的安全定义自定义安全权限
20. Create a secure sandbox using a security manager 67
使用安全管理器创建一个安全的沙盒
21. Do not let untrusted code misuse privileges of callback methods 72
不要让不可信代码误用回调方法的特权
Chapter 2 Defensive Programming / 防御式编程 79
22. Minimize the scope of variables 80
最小化变量的作用域
23. Minimize the scope of the @SuppressWarnings annotation 82
最小化@SuppressWarnings注解的作用域
24. Minimize the accessibility of classes and their members 84
最小化类及其成员的可访问性
25. Document thread-safety and use annotations where applicable 89
文档化代码的线程安全性
26. Always provide feedback about the resulting value of a method 96
为方法的结果值提供反馈
27. Identify files using multiple file attributes 99
使用多个文件属性识别文件
28. Do not attach significance to the ordinal associated with an enum 106
不要赋予枚举常量的序号任何特殊意义
29. Be aware of numeric promotion behavior 108
注意数字提升行为
30. Enable compile-time type checking of variable arity parameter types 112
对可变参数的类型做编译时类型检查
31. Do not apply public final to constants whose value might change in later releases 115
不要把其值在以后版本里可能会发生变化的常量设置为public final
32. Avoid cyclic dependencies between packages 118
避免包之间的循环依赖
33. Prefer user-defined exceptions over more general exception types 121
使用用户自定义的异常而非宽泛的异常类型
34. Try to gracefully recover from system errors 123
尽量从系统错误中优雅恢复
35. Carefully design interfaces before releasing them 125
布接口前请谨慎设计
36. Write garbage collection–friendly code 128
编写对垃圾回收机制友好的代码
Chapter 3 Reliability / 可靠性 131
37. Do not shadow or obscure identifiers in subscopes 132
不要在子作用域里遮蔽或者掩盖标识符
38. Do not declare more than one variable per declaration 134
不要在一个声明里声明多个变量
39. Use meaningful symbolic constantsto represent literal values in program logic 138
在程序逻辑中用有意义的符号常量代表文字值
40. Properly encode relationships in constant definitions 142
在常量定义中恰当地表示相互之间的关系
41. Return an empty array or collection instead of a null value for methods that return an array or collection 143
对于返回数组或者集合的方法,用返回一个空数组或者集合来替代返回一个空值
42. Use exceptions only for exceptional conditions 146
只在异常的情况下使用异常
43. Use a try-with-resources statement to safely handle closeable resources 148
使用try-with-resources语句安全处理可关闭的资源
44. Do not use assertions to verify the absence of runtime errors 151
不要使用断言来验证不存在的运行时错误
45. Use the same type for the second and third operands in conditional expressions 153
在条件表达式中,第二和第三个操作数应使用相同类型
46. Do not serialize direct handles to system resources 157
不要序列化直接指向系统资源的句柄
47. Prefer using iterators over enumerations 159
更倾向于使用迭代器而不是枚举
48. Do not use direct buffers for short-lived, infrequently used objects 162
对于短生存周期、不常用的对象不要使用直接缓冲区
49. Remove short-lived objects from long-lived containe
Fred Long 英国Aberystwyth大学计算机科学系的高级讲师,自1992起一直担任软件工程研究所(Software Engineering Institute)的客座科学家。
Dhruv Mohindra 印度Persistent Systems有限公司安全实践小组的技术领导,为金融、电信、健康领域的很多公司提供咨询服务。
Robert C. Seacord 出版过多本软件安全与软件工程方面的书籍,是CERT安全编码倡议小组的技术经理,另外他还在卡内基-梅隆大学教计算机科学。
Dean F. Sutherland CERT高级软件安全工程师,之前是Tartan公司技术组的高级成员,他在Tartan公司主要负责开发优化编译器。
David Svoboda CERT软件安全工程师,是多个CMU项目的主要开发者。
Dhruv Mohindra 印度Persistent Systems有限公司安全实践小组的技术领导,为金融、电信、健康领域的很多公司提供咨询服务。
Robert C. Seacord 出版过多本软件安全与软件工程方面的书籍,是CERT安全编码倡议小组的技术经理,另外他还在卡内基-梅隆大学教计算机科学。
Dean F. Sutherland CERT高级软件安全工程师,之前是Tartan公司技术组的高级成员,他在Tartan公司主要负责开发优化编译器。
David Svoboda CERT软件安全工程师,是多个CMU项目的主要开发者。
全世界的各种组织都依赖Java代码执行关键任务,因此代码必须是可靠的、健壮的、快速的、可维护的、安全的。本书的主题就是如何编写安全可靠的Java代码。书中提供了更新的技术来防止蓄意攻击和其他意想不到的事件。书中给出了编写安全、可靠的Java代码的75条建议,每一条都非常一致和直观。对于每条编码指南都指定了一致性要求,对大多数编码指南还提供了违反这些原则的代码示例和合规的解决方案。读者不但可以在本书中找到改进代码可靠性和清晰性的实践,还能全面了解导致不佳代码的常见误解。
比价列表