API 就像是呼叫者與被呼叫者之間簽訂的規定。最常見的 API 濫用形式是由呼叫者這一當事方未能遵守此規定所造成的。例如,如果程式在呼叫 chroot() 後無法呼叫 chdir(),即違反規範如何以安全方式變更使用中根目錄的規定。程式庫濫用的另一個好例子是期待被呼叫者向呼叫者傳回值得信賴的 DNS 資訊。在這種情況下,呼叫者是透過對其行為做出某些假設 (傳回值可用於驗證目的) 來濫用被呼叫者 API。另一方也可能違反呼叫者與被呼叫者間的規定。例如,如果編碼器衍生出子類別 SecureRandom 並傳回一個非隨機值,則違反了規定。
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()
經常被誤用,因為一般認為它會傳回由指定的 String 引數代表的布林值。但是,如 Javadoc Boolean.getBoolean(String)
方法中所述,「只有當引數命名的系統屬性存在且等於 String 'true'時傳回 true」("Returns true if and only if the system property named by the argument exists and is equal to the string 'true'.")。Boolean.valueOf(String)
或 Boolean.parseBoolean(String)
方法。Boolean.getBoolean(String)
不會轉譯 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)
...
_alloca()
函數會拋出堆疊溢位例外,可能導致程式當機。_alloca()
函數會分配堆疊上的記憶體。如果要求分配空間對於可用堆疊來說太大了,_alloca()
將會拋出一個異常。如果沒有捉到異常,程式就會當機,有可能會引起 Denial of Service 攻擊。_alloca()
,並使用較安全的 _alloca_s()
取代。MAX_PATH
位元組長度的緩衝區,但是你應該檢查每一個函數的文件資料。如果緩衝區不夠大,無法儲存操作的結果,就會發生 Buffer overflow。
char *createOutputDirectory(char *name) {
char outputDirectoryName[128];
if (getCurrentDirectory(128, outputDirectoryName) == 0) {
return null;
}
if (!PathAppend(outputDirectoryName, "output")) {
return null;
}
if (!PathAppend(outputDirectoryName, name)) {
return null;
}
if (SHCreateDirectoryEx(NULL, outputDirectoryName, NULL)
!= ERROR_SUCCESS) {
return null;
}
return StrDup(outputDirectoryName);
}
output\<name>
」的目錄,並且回傳該名稱的堆積分配副本。對於大多數的目前目錄值和名稱參數,此函數都能夠正常執行。但是如果 name
參數特別長,第二個 PathAppend()
呼叫就可能會溢出 outputDirectoryName
緩衝區,因為這個緩衝區比 MAX_PATH
位元組小。umask()
引數指定的 mask 總是容易與 chmod()
引數混淆。umask()
線上手冊以錯誤陳述式開頭:chmod()
的用法,即使用者提供的引數指定某些位元在指定的檔案上啟用,但事實上 umask()
的行為恰恰相反: umask()
將 umask 設為 ~mask & 0777
。umask()
線上手冊接下來描述了對於 umask()
的正確使用方式:open()
使用 umask 來為新建立的檔案設定初始化檔案權限。 特別是,umask 的權限關閉了傳送給 open(2)
的 mode 引數 (例如,一般的 umask 預設值 022 會讓新建立的檔案擁有 0666 & ~022 = 0644 = rw-r--r-- 權限,而在一般情況下,應將 mode 指定為 0666)。」
...
struct stat output;
int ret = stat(aFilePath, &output);
// error handling omitted for this example
struct timespec accessTime = output.st_atime;
...
umask()
引數指定的 mask 總是容易與 chmod()
引數混淆。umask()
線上手冊以錯誤指令開頭:chmod()
的用法,即使用者指定引數中某些位元值來控制檔案存取權限,但事實上 umask()
做的恰恰相反:umask()
將 umask 設為 ~mask & 0777
。umask()
線上手冊接下來描述了對於 umask()
的正確使用方式:transactionId
寫入至應用程式文件目錄中的暫存檔案:
...
//get the documents directory:
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
//make a file name to write the data to using the documents directory:
let fileName = NSString(format:"%@/tmp_activeTrans.txt", documentsPath)
// write data to the file
let transactionId = "TransactionId=12341234"
transactionId.writeToFile(fileName, atomically:true)
...
posted
物件。FileUpload
屬於 System.Web.UI.HtmlControls.HtmlInputFile
類型。
HttpPostedFile posted = FileUpload.PostedFile;
@Controller
public class MyFormController {
...
@RequestMapping("/test")
public String uploadFile (org.springframework.web.multipart.MultipartFile file) {
...
} ...
}
<?php
$udir = 'upload/'; // Relative path under Web root
$ufile = $udir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $ufile)) {
echo "Valid upload received\n";
} else {
echo "Invalid upload rejected\n";
} ?>
from django.core.files.storage import default_storage
from django.core.files.base import File
...
def handle_upload(request):
files = request.FILES
for f in files.values():
path = default_storage.save('upload/', File(f))
...
file
類型的 <input>
標籤表示程式接受檔案上傳。
<input type="file">
myModule.config(function($interpolateProvider){
$interpolateProvider.startSymbol("[[");
$interpolateProvider.endSymbol("]]");
});