破坏 API 服务器的 45 种方法(带有示例的负面测试)
作为开发者,我们力求编写出完美无误的代码,但实际上却无人能做到,原因在于……bug。为了在那些恼人的 bug 对我们的应用程序造成严重破坏之前将其捕获,我们依赖于自动化测试。积极的测试可以确保我们的代码按预期运行,而消极的测试则在验证我们的应用程序是否足够健壮,能够处理意外输入和极端情况方面发挥着至关重要的作用。
我正在研究Pythagora,这是一款开源工具,它可以自行编写自动化集成测试(当然,GPT-4 也提供了一些帮助),开发者无需编写任何代码。基本上,你可以在 30 分钟内获得从 0% 到 80% 的代码覆盖率(视频)。
我们刚刚创建了一个功能,只需一条命令即可自动从整个测试套件中生成负面测试。在构建该功能的过程中,我研究了导致 API 服务器崩溃的各种方法,以测试它是否能够优雅地处理错误。因此,我整理了一份详尽的列表,列出了如果服务器无法正常处理错误,可能导致服务器崩溃的各种方法。
1. 必填字段为空或缺失
{
"endpoint": "/api/users",
"body": {
"username": "",
"email": ""
},
"method": "POST"
}
2. 无效字段值 - 超出字符限制
{
"endpoint": "/api/users",
"body": {
"username": "ThisIsAnIncrediblyLongUsernameThatExceedsTheCharacterLimit"
},
"method": "POST"
}
3. 无效的字段值 - 数据格式错误
{
"endpoint": "/api/users",
"body": {
"email": "invalid-email@"
},
"method": "POST"
}
4. 有效载荷中存在多余或不相关的密钥
{
"endpoint": "/api/users",
"body": {
"username": "validuser",
"extra_key": "irrelevant_value"
},
"method": "POST"
}
5. 不正确或无效的 HTTP 方法
{
"endpoint": "/api/users/123",
"body": {},
"method": "POST"
}
6. 无效的端点路径
{
"endpoint": "/api/nonexistent_endpoint",
"body": {},
"method": "GET"
}
7. 在 POST 请求中使用查询参数代替请求体
{
"endpoint": "/api/users?username=testuser",
"body": {},
"method": "POST"
}
8. 缺少或无效的身份验证标头(例如 API 密钥)
{
"endpoint": "/api/users",
"body": {},
"method": "GET",
"headers": {
"Authorization": "Invalid API_KEY"
}
}
9. 不正确的数据结构——数组而不是对象
{
"endpoint": "/api/users",
"body": [
"username": "testuser",
"email": "test@example.com"
],
"method": "POST"
}
10. 不正确的数据结构——对象而不是数组
{
"endpoint": "/api/users",
"body": {
"users": {
"username": "testuser",
"email": "test@example.com"
}
},
"method": "POST"
}
11. JSON 格式问题 - 无效的 Unicode 字符
{
"endpoint": "/api/users",
"body": {
"username": "test\uFFFFuser"
},
"method": "POST"
}
12. 有效载荷中的重复密钥
{
"endpoint": "/api/users",
"body": {
"username": "testuser",
"username": "duplicate"
},
"method": "POST"
}
13. 无效或不受支持的内容类型(例如,发送 XML 而不是 JSON)
{
"endpoint": "/api/users",
"body": "<user><username>testuser</username><email>test@example.com</email></user>",
"method": "POST",
"headers": {
"Content-Type": "application/xml"
}
}
14. 超出有效载荷大小限制
{
"endpoint": "/api/users",
"body": {
"large_data": "A very large data string that exceeds the server's payload size limit..."
},
"method": "POST"
}
15. 身份验证令牌无效或过期
{
"endpoint": "/api/users",
"body": {},
"method": "GET",
"headers": {
"Authorization": "Bearer expired_token"
}
}
16. 在字段值中使用特殊字符
{
"endpoint": "/api/users",
"body": {
"username": "test!@#$%^&*()-user"
},
"method": "POST"
}
17. 发送嵌套对象而不是简单的键值对
{
"endpoint": "/api/users",
"body": {
"user": {
"username": "testuser",
"email": "test@example.com"
}
},
"method": "POST"
}
18. 发送错误数据类型的数据(例如,发送字符串而不是整数)
{
"endpoint": "/api/users",
"body": {
"age": "25"
},
"method": "POST"
}
19. 为必填字段发送空值
{
"endpoint": "/api/users",
"body": {
"username": null
},
"method": "POST"
}
20. 在字段名称中使用保留关键字
{
"endpoint": "/api/users",
"body": {
"class": "user"
},
"method": "POST"
}
21. 发送不完整或格式错误的分段文件上传
{
"endpoint": "/api/upload",
"body": {
"file": "incomplete_file_data"
},
"method": "POST",
"headers": {
"Content-Type": "multipart/form-data"
}
}
22. 特殊字符的 URL 编码不正确或缺失
{
"endpoint": "/api/users?username=test user",
"body": {},
"method": "GET"
}
23. 在 GET 请求中发送请求主体
{
"endpoint": "/api/users",
"body": {
"username": "testuser"
},
"method": "GET"
}
24. 无效的日期或时间格式
{
"endpoint": "/api/users",
"body": {
"birthdate": "01-25-1990"
},
"method": "POST"
}
25. 在字段名称中使用非 ASCII 字符
{
"endpoint": "/api/users",
"body": {
"üsername": "testuser"
},
"method": "POST"
}
26. 发送深层嵌套的对象
{
"endpoint": "/api/users",
"body": {
"user": {
"profile": {
"details": {
"nested": "too_deep"
}
}
}
},
"method": "POST"
}
27. 在字段值中使用不可打印字符或控制字符
{
"endpoint": "/api/users",
"body": {
"username": "test\u0008user"
},
"method": "POST"
}
28. 多次发送相同字段,但值不同
{
"endpoint": "/api/users",
"body": {
"username": "testuser",
"username": "different"
},
"method": "POST"
}
29. 请求主体的 Content-Length 标头缺失或无效
{
"endpoint": "/api/users",
"body": {
"username": "testuser"
},
"method": "POST",
"headers": {
"Content-Length": "invalid"
}
}
30. 在字段名称中使用空格或特殊字符
{
"endpoint": "/api/users",
"body": {
"user name": "testuser"
},
"method": "POST"
}
31. 发送无效或格式错误的 JSONP 回调
{
"endpoint": "/api/users?callback=invalid(callback)",
"body": {},
"method": "GET"
}
32. 将有效负载作为单个字符串而不是键值对发送
{
"endpoint": "/api/users",
"body": "username=testuser&email=test@example.com",
"method": "POST"
}
33. 将布尔值作为字符串发送(例如,“true”而不是true)
{
"endpoint": "/api/users",
"body": {
"active": "true"
},
"method": "POST"
}
34. 使用非标准 HTTP 方法(例如 PATCH、CONNECT)
{
"endpoint": "/api/users/123",
"body": {
"username": "updateduser"
},
"method": "PATCH"
}
35. 发送不受支持的 HTTP 版本号
{
"endpoint": "/api/users",
"body": {},
"method": "GET",
"httpVersion": "HTTP/3.0"
}
36. 发送多个身份验证标头(例如,API 密钥和令牌)
{
"endpoint": "/api/users",
"body": {},
"method": "GET",
"headers": {
"Authorization": "Bearer token_value",
"API-Key": "api_key_value"
}
}
37. 发送不必要或无效的 CORS 标头
{
"endpoint": "/api/users",
"body": {},
"method": "GET",
"headers": {
"Access-Control-Allow-Origin": "*"
}
}
38. 发送冲突的查询参数和请求正文数据
{
"endpoint": "/api/users?username=testuser",
"body": {
"username": "different_user"
},
"method": "POST"
}
39. 在身份验证标头值中使用非标准字符
{
"endpoint": "/api/users",
"body": {},
"method": "GET",
"headers": {
"Authorization": "Bearer t@ken_value"
}
}
40. 向仅接受正值的字段发送负数
{
"endpoint": "/api/users",
"body": {
"age": -25
},
"method": "POST"
}
41. 发送超出预期范围的未来或过去的时间戳
{
"endpoint": "/api/users",
"body": {
"birthdate": "01-25-1800"
},
"method": "POST"
}
42. 在字段值中使用 HTML、JavaScript 或 SQL 代码尝试代码注入
{
"endpoint": "/api/users",
"body": {
"username": "<script>alert('test')</script>"
},
"method": "POST"
}
43. 在有效载荷中使用不同的字符编码(例如,UTF-8,UTF-16)
{
"endpoint": "/api/users",
"body": {
"username": "téstuser"
},
"method": "POST",
"headers": {
"Content-Type": "application/json; charset=UTF-16"
}
}
44. 发送混合数据类型的数组
{
"endpoint": "/api/users",
"body": {
"values": [1, "string", true]
},
"method": "POST"
}
45. 将字段值作为数组或对象发送,而不是简单数据类型(例如字符串、数字)
{
"endpoint": "/api/users",
"body": {
"username": ["testuser"]
},
"method": "POST"
}
就是这样。我希望这份清单能给你提供一些新的想法来测试和保护你的服务器。
如果您发现这篇文章很有价值,那么如果您能通过关注Pythagora Github repo来支持我们,那对我来说意义重大。
而且,如果您尝试了,请告诉我们您的反馈,我们很高兴听到。
鏂囩珷鏉ユ簮锛�https://dev.to/zvone187/45-ways-to-break-an-api-server-male-tests-with-examples-4ok3