API 是调用方和被调用方之间的约定。最常见的 API 滥用是由于调用方未能遵守此约定的终止导致的。例如,如果某个程序在调用 chroot() 后未能调用 chdir(),则违反了用于指定如何安全地更改活动根目录的约定。库滥用的另一个典型示例是期望被调用方向调用方返回可信的 DNS 信息。在这种情况下,调用方通过对被调用方行为做出某种假设(返回值可用于身份验证目的)滥用其 API。另一方也可能违反调用方-被调用方约定。例如,如果编码器子类化 SecureRandom 并返回一个非随机值,则将违反此约定。
private
和 final
,然后错误地创建了改变集的方法。
@Immutable
public final class ThreeStooges {
private final Set stooges = new HashSet>();
...
public void addStooge(String name) {
stooges.add(name);
}
...
}
final
。Immutable
注释。非最终字段允许更改值,从而违反了类的不可变性。public
和非 final
。
@Immutable
public class ImmutableInteger {
public int value;
}
public
和 final
。
@Immutable
public final class ThreeStooges {
public final Set stooges = new HashSet();
...
}
ctx = new InitialContext();
datasource = (DataSource)ctx.lookup(DB_DATASRC_REF);
conn = datasource.getConnection();
conn = DriverManager.getConnection(CONNECT_STRING);
--auto-tls
标记设置为 true
。因此,etcd 实例使用自签名证书与客户端进行 TLS 连接。
...
spec:
containers:
- command:
...
- etcd
...
- --auto-tls=true
...
RegisterModel
或 Details
类中的任何属性:
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
try
{
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", "");
}
}
return View(model);
}
RegisterModel
类定义为:
public class RegisterModel
{
[BindRequired]
[Display(Name = "User name")]
public string UserName { get; set; }
[BindRequired]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
public string ConfirmPassword { get; set; }
public Details Details { get; set; }
public RegisterModel()
{
Details = new Details();
}
}
Details
类定义为:示例 2:在 ASP.NET MVC、Web API 应用程序中使用
public class Details
{
public bool IsAdmin { get; set; }
...
}
TryUpdateModel()
或 UpdateModel()
时,默认情况下,模型绑定器将自动尝试绑定所有 HTTP 请求参数:示例 3:在 ASP.NET Web Form 应用程序中,将
public ViewResult Register()
{
var model = new RegisterModel();
TryUpdateModel<RegisterModel>(model);
return View("detail", model);
}
TryUpdateModel()
或 UpdateModel()
与 IValueProvider 接口结合使用时,模型绑定器将自动尝试绑定所有 HTTP 请求参数。
Employee emp = new Employee();
TryUpdateModel(emp, new System.Web.ModelBinding.FormValueProvider(ModelBindingExecutionContext));
if (ModelState.IsValid)
{
db.SaveChanges();
}
Employee
类定义为:
public class Employee
{
public Employee()
{
IsAdmin = false;
IsManager = false;
}
public string Name { get; set; }
public string Email { get; set; }
public bool IsManager { get; set; }
public bool IsAdmin { get; set; }
}
Booking
类的任何属性:
<view-state id="enterBookingDetails" model="booking">
<on-render>
<render fragments="body" />
</on-render>
<transition on="proceed" to="reviewBooking">
</transition>
<transition on="cancel" to="cancel" bind="false" />
</view-state>
Booking
类定义如下:
public class Booking implements Serializable {
private Long id;
private User user;
private Hotel hotel;
private Date checkinDate;
private Date checkoutDate;
private String creditCard;
private String creditCardName;
private int creditCardExpiryMonth;
private int creditCardExpiryYear;
private boolean smoking;
private int beds;
private Set<Amenity> amenities;
// Public Getters and Setters
...
}
Order
、Customer
和 Profile
都是 Microsoft .NET 实体持久类。
public class Order {
public string ordered { get; set; }
public List<LineItem> LineItems { get; set; }
pubilc virtual Customer Customer { get; set; }
...
}
public class Customer {
public int CustomerId { get; set; }
...
public virtual Profile Profile { get; set; }
...
}
public class Profile {
public int profileId { get; set; }
public string username { get; set; }
public string password { get; set; }
...
}
OrderController
是处理该请求的 ASP.NET MVC 控制器类:
public class OrderController : Controller{
StoreEntities db = new StoreEntities();
...
public String updateOrder(Order order) {
...
db.Orders.Add(order);
db.SaveChanges();
}
}
Order
、Customer
和 Profile
都是 Hibernate 持久类。
public class Order {
String ordered;
List lineItems;
Customer cust;
...
}
public class Customer {
String customerId;
...
Profile p;
...
}
public class Profile {
String profileId;
String username;
String password;
...
}
OrderController
是处理该请求的 Spring 控制器类:
@Controller
public class OrderController {
...
@RequestMapping("/updateOrder")
public String updateOrder(Order order) {
...
session.save(order);
}
}
[FromBody]
注释时,用于将 HTTP 请求参数绑定到模型类的框架绑定器依赖于输入格式程序。[FromBody]
注释应用于操作的复杂参数,然后任何其他应用于参数类型或其任何字段的绑定属性(例如 [Bind]
或 [BindNever]
)都会被有效忽略,这意味着使用绑定注释进行规避是不可能的。[FromBody]
注释应用于操作的参数时,模型绑定器会自动尝试使用输入格式程序绑定请求正文中指定的所有参数。默认情况下,绑定器使用 JSON 输入格式程序来尝试绑定来自请求正文的所有可能的参数:
[HttpPost]
public ActionResult Create([FromBody] Product p)
{
return View(p.Name);
}
[FromBody]
注释时,任何应用于后接的 Product
类型的绑定注释(例如 [Bind]
或 [BindNever]
)都会因为所使用的输入格式程序而被忽略。
public class Product
{
...
public string Name { get; set; }
public bool IsAdmin { get; set; }
...
}
null
。Equals()
之前检查 Item
属性返回的字符串是否为 null
,从而可能会导致 null
dereference。
string itemName = request.Item(ITEM_NAME);
if (itemName.Equals(IMPORTANT_ITEM)) {
...
}
...
null
也就无关紧要了。”null
。malloc()
返回的指针之前,并没有检查内存是否分配成功。
buf = (char*) malloc(req_size);
strncpy(buf, xfer, req_size);
malloc()
的调用失败是不是因为 req_size
太大,还是因为在同一时刻处理的请求太多。或者是由于已累计超时的 memory leak 引起的。如果不对错误进行处理,就不会知道是什么原因。null
。compareTo()
之前,不会检查 getParameter()
返回的字符串是否为 null
,从而可能会造成 null
dereference。例 2:。以下代码显示了这样一个例子,一个系统属性被设置为了
String itemName = request.getParameter(ITEM_NAME);
if (itemName.compareTo(IMPORTANT_ITEM)) {
...
}
...
null
,随后间接引用它的程序员错误地认为该属性值是已定义的。
System.clearProperty("os.name");
...
String os = System.getProperty("os.name");
if (os.equalsIgnoreCase("Windows 95") )
System.out.println("Not supported");
null
也就无关紧要了。”null
进行比较的约定。Object.equals()
、Comparable.compareTo()
和 Comparator.compare()
时,如果其参数为 null
,则必须返回一个指定值。不遵守该约定可能会导致发生意外的行为。equals()
方法,但不会将其参数与 null
进行比较。
public boolean equals(Object object)
{
return (toString().equals(object.toString()));
}
clone()
应调用 super.clone()
获取新的对象。clone()
的方法中,应通过调用 super.clone()
来获取新对象。如果类没有遵守该约定,那么子类的 clone()
方法将会返回一个错误的对象类型。super.clone()
而产生的 bug。由于 Kibitzer
实现 clone()
的方法的缘故,FancyKibitzer
的克隆方法将会返回类型为 Kibitzer
而非 FancyKibitzer
的对象。
public class Kibitzer implements Cloneable {
public Object clone() throws CloneNotSupportedException {
Object returnMe = new Kibitzer();
...
}
}
public class FancyKibitzer extends Kibitzer
implements Cloneable {
public Object clone() throws CloneNotSupportedException {
Object returnMe = super.clone();
...
}
}
Equals()
和 GetHashCode()
中的一个。a.Equals(b) == true
,那么 a.GetHashCode() == b.GetHashCode()
。 Equals()
,但没有重写 GetHashCode()
。
public class Halfway() {
public override boolean Equals(object obj) {
...
}
}
equals()
和 hashCode()
中的一个。a.equals(b) == true
,那么 a.hashCode() == b.hashCode()
。 equals()
,但没有重写 hashCode()
。
public class halfway() {
public boolean equals(Object obj) {
...
}
}
saveState()
和 restoreState()
中的一个。saveState(javax.faces.context.FacesContext)
和 restoreState(javax.faces.context.FacesContext, java.lang.Object)
,或者同时都不实施。由于这两种方法关系密切,因此,saveState(javax.faces.context.FacesContext)
和 restoreState(javax.faces.context.FacesContext, java.lang.Object)
方法不得驻留在继承层次结构的不同级别中。saveState()
,但未定义 restoreState()
,因此无论扩展它的任何类做什么,它都会出错。
public class KibitzState implements StateHolder {
public Object saveState(FacesContext fc) {
...
}
}
checkCallingOrSelfPermission()
或 checkCallingOrSelfUriPermission()
用来判定调用程序是否具备访问某个服务或给定 URI 所需的权限。但是,由于此类函数可允许缺乏相应权限的恶意应用程序利用您应用程序的权限进行访问,因而应慎重使用。Assert()
与特定权限一起使用时,意味着当前控制流具有指定的权限。这又会导致 .NET Framework 只要满足所需权限就会停止任何进一步的权限检查,意味着调用对 Assert()
发出调用的代码的代码可能没有所需权限。使用 Assert()
在一些情况下是有帮助的,但如果允许恶意用户获取对他们本无权限的资源的控制则会导致漏洞。
IPAddress hostIPAddress = IPAddress.Parse(RemoteIpAddress);
IPHostEntry hostInfo = Dns.GetHostByAddress(hostIPAddress);
if (hostInfo.HostName.EndsWith("trustme.com")) {
trusted = true;
}
getlogin()
函数很容易让您上当。请不要依靠其返回的名称。getlogin()
函数应该返回一个包含当前在终端登陆的用户名的字符串,但是攻击者可使 getlogin()
返回一个任意在本机登录的用户名。不要依赖 getlogin()
返回的名称来确定是否安全。getlogin()
来确定用户是否可以信赖。但它很容易被人暗中破坏。
pwd = getpwnam(getlogin());
if (isTrustedGroup(pwd->pw_gid)) {
allow();
} else {
deny();
}
String ip = request.getRemoteAddr();
InetAddress addr = InetAddress.getByName(ip);
if (addr.getCanonicalHostName().endsWith("trustme.com")) {
trusted = true;
}
Boolean.getBoolean()
常常与 Boolean.valueOf()
或 Boolean.parseBoolean()
方法调用混淆。Boolean.getBoolean()
来返回指定字符串变量表示的布尔值,因而导致此方法的调用使用不当。但是,正如 Javadoc Boolean.getBoolean(String)
方法所说,“当且仅当该参数表示的系统属性存在且等于字符串 'true' 时,才会返回 true。”Boolean.valueOf(String)
或 Boolean.parseBoolean(String)
方法。Boolean.getBoolean(String)
不会对基元型字符串进行转换。它只能对系统属性进行转换。
...
String isValid = "true";
if ( Boolean.getBoolean(isValid) ) {
System.out.println("TRUE");
}
else {
System.out.println("FALSE");
}
...
Decoder
和 Encoding
类中的 GetChars
方法以及 Encoder
和 Encoding
类中的 GetBytes
方法在内部对字符数组和字节数组执行指针运算,以将字符范围转换为字节数范围,反之亦然。
out.println("x = " + encoder.encodeForJavaScript(input) + ";");
...
unichar ellipsis = 0x2026;
NSString *myString = [NSString stringWithFormat:@"My Test String%C", ellipsis];
NSData *asciiData = [myString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *asciiString = [[NSString alloc] initWithData:asciiData encoding:NSASCIIStringEncoding];
NSLog(@"Original: %@ (length %d)", myString, [myString length]);
NSLog(@"Best-fit-mapped: %@ (length %d)", asciiString, [asciiString length]);
// output:
// Original: My Test String... (length 15)
// Best-fit-mapped: My Test String... (length 17)
...
...
let ellipsis = 0x2026;
let myString = NSString(format:"My Test String %C", ellipsis)
let asciiData = myString.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion:true)
let asciiString = NSString(data:asciiData!, encoding:NSASCIIStringEncoding)
NSLog("Original: %@ (length %d)", myString, myString.length)
NSLog("Best-fit-mapped: %@ (length %d)", asciiString!, asciiString!.length)
// output:
// Original: My Test String ... (length 16)
// Best-fit-mapped: My Test String ... (length 18)
...