破坏 API 服务器的 45 种方法 (带有示例的负面测试)

2024-03-24   出处: dev.to  作/译者:zvone187/Mint

作为开发人员,我们努力编写准确无误的代码,但实际上没有人这样做,因为……bug。为了在这些讨厌的bug对我们的应用程序造成严重破坏之前将其捕获,我们依赖于自动化测试。正向测试可以确保我们的代码按照预期运行,而负向测试则在验证我们的应用程序是否足够强大,在处理意外输入和边缘情况方面发挥着至关重要的作用。

我正在开发 Pythagora,这是一款开源工具,它可以自己编写自动化集成测试(当然,GPT-4 也会提供一些帮助),开发人员无需编写任何代码。基本上,你可以在 30 分钟内实现从 0 到 80% 的代码覆盖率。我们刚刚创建了一项功能,只需一条命令就能从整个测试套件中自动生成负向测试。在创建该功能时,我研究了破坏API服务器的不同方法,以测试它是否能正常处理错误。这里有一个全面的列表,如果被测代码不能正确处理错误,则有可能破坏服务器。

1. 必填字段为空或缺失

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "",
  5. "email": ""
  6. },
  7. "method": "POST"
  8. }

2. 无效字段值 - 超过字符限制

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "ThisIsAnIncrediblyLongUsernameThatExceedsTheCharacterLimit"
  5. },
  6. "method": "POST"

3. 无效字段值-数据格式错误

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "email": "invalid-email@"
  5. },
  6. "method": "POST"
  7. }

4. 有效负载中多余的或不相关的键

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "validuser",
  5. "extra_key": "irrelevant_value"
  6. },
  7. "method": "POST"
  8. }

5. 不正确或无效的HTTP方法

  1. {
  2. "endpoint": "/api/users/123",
  3. "body": {},
  4. "method": "POST"
  5. }

6. 无效终端路径

  1. {
  2. "endpoint": "/api/nonexistent_endpoint",
  3. "body": {},
  4. "method": "GET"
  5. }

7. 在 POST 请求中使用查询参数而不是请求正文

  1. {
  2. "endpoint": "/api/users?username=testuser",
  3. "body": {},
  4. "method": "POST"
  5. }

8. 缺失或无效的身份验证标头(如 API 密钥)

  1. {
  2. "endpoint": "/api/users",
  3. "body": {},
  4. "method": "GET",
  5. "headers": {
  6. "Authorization": "Invalid API_KEY"
  7. }
  8. }

9. 不正确的数据结构 - 数组而非对象

  1. {
  2. "endpoint": "/api/users",
  3. "body": [
  4. "username": "testuser",
  5. "email": "test@example.com"
  6. ],
  7. "method": "POST"
  8. }

10. 不正确的数据结构 - 对象而非数组

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "users": {
  5. "username": "testuser",
  6. "email": "test@example.com"
  7. }
  8. },
  9. "method": "POST"
  10. }

11. JSON格式问题-无效的Unicode字符

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "test\uFFFFuser"
  5. },
  6. "method": "POST"
  7. }

12. 有效负载中的重复键

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "testuser",
  5. "username": "duplicate"
  6. },
  7. "method": "POST"
  8. }

13. 无效或不支持的内容类型(例如,发送XML而不是JSON)

  1. {
  2. "endpoint": "/api/users",
  3. "body": "<user><username>testuser</username><email>test@example.com</email></user>",
  4. "method": "POST",
  5. "headers": {
  6. "Content-Type": "application/xml"
  7. }
  8. }

14. 超过有效载荷大小限制

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "large_data": "A very large data string that exceeds the server's payload size limit..."
  5. },
  6. "method": "POST"
  7. }

15. 无效或过期的身份验证令牌

  1. {
  2. "endpoint": "/api/users",
  3. "body": {},
  4. "method": "GET",
  5. "headers": {
  6. "Authorization": "Bearer expired_token"
  7. }
  8. }

16. 在字段值中使用特殊字符

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "test!@#$%^&*()-user"
  5. },
  6. "method": "POST"
  7. }

17. 发送嵌套对象而不是简单的键值对

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "user": {
  5. "username": "testuser",
  6. "email": "test@example.com"
  7. }
  8. },
  9. "method": "POST"
  10. }

18. 以错误的数据类型(如字符串而非整数)发送数据

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "age": "25"
  5. },
  6. "method": "POST"
  7. }

19. 为必需的字段发送空值

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": null
  5. },
  6. "method": "POST"
  7. }

20. 在字段名中使用保留关键字

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "class": "user"
  5. },
  6. "method": "POST"
  7. }

21. 发送不完整或格式错误的多部分上传文件

  1. {
  2. "endpoint": "/api/upload",
  3. "body": {
  4. "file": "incomplete_file_data"
  5. },
  6. "method": "POST",
  7. "headers": {
  8. "Content-Type": "multipart/form-data"
  9. }
  10. }

22. 特殊字符的URL编码不正确或缺失

  1. {
  2. "endpoint": "/api/users?username=test user",
  3. "body": {},
  4. "method": "GET"
  5. }

23. 在 GET 请求中发送请求内容

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "testuser"
  5. },
  6. "method": "GET"
  7. }

24. 无效的日期或时间格式

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "birthdate": "01-25-1990"
  5. },
  6. "method": "POST"
  7. }

25. 在字段名中使用非 ASCII 字符

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "üsername": "testuser"
  5. },
  6. "method": "POST"
  7. }

26. 发送深度嵌套对象

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "user": {
  5. "profile": {
  6. "details": {
  7. "nested": "too_deep"
  8. }
  9. }
  10. }
  11. },
  12. "method": "POST"
  13. }

27. 在字段值中使用不可打印字符或控制字符

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "test\u0008user"
  5. },
  6. "method": "POST"
  7. }

28. 用不同的值多次发送相同的字段

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "testuser",
  5. "username": "different"
  6. },
  7. "method": "POST"
  8. }

29. 请求正文的内容长度标头缺失或无效

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "testuser"
  5. },
  6. "method": "POST",
  7. "headers": {
  8. "Content-Length": "invalid"
  9. }
  10. }

30. 在字段名称中使用空格或特殊字符

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "user name": "testuser"
  5. },
  6. "method": "POST"
  7. }

31. 发送无效或格式错误的JSONP回调

  1. {
  2. "endpoint": "/api/users?callback=invalid(callback)",
  3. "body": {},
  4. "method": "GET"
  5. }

32. 以单个字符串而不是键值对的形式发送有效载荷

  1. {
  2. "endpoint": "/api/users",
  3. "body": "username=testuser&email=test@example.com",
  4. "method": "POST"
  5. }

33. 以字符串形式发送布尔值(例如,以 “true “代替 true)

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "active": "true"
  5. },
  6. "method": "POST"
  7. }

34. 使用非标准 HTTP 方法(例如 PATCH、CONNECT)

  1. {
  2. "endpoint": "/api/users/123",
  3. "body": {
  4. "username": "updateduser"
  5. },
  6. "method": "PATCH"
  7. }

35. 发送不受支持的 HTTP 版本编号

  1. {
  2. "endpoint": "/api/users",
  3. "body": {},
  4. "method": "GET",
  5. "httpVersion": "HTTP/3.0"
  6. }

36. 发送多个验证标头(如 API 密钥和令牌)

  1. {
  2. "endpoint": "/api/users",
  3. "body": {},
  4. "method": "GET",
  5. "headers": {
  6. "Authorization": "Bearer token_value",
  7. "API-Key": "api_key_value"
  8. }
  9. }

37. 发送不必要或无效的CORS标头

  1. {
  2. "endpoint": "/api/users",
  3. "body": {},
  4. "method": "GET",
  5. "headers": {
  6. "Access-Control-Allow-Origin": "*"
  7. }
  8. }

38. 发送相互矛盾的查询参数和请求正文数据

  1. {
  2. "endpoint": "/api/users?username=testuser",
  3. "body": {
  4. "username": "different_user"
  5. },
  6. "method": "POST"
  7. }

39. 在身份验证头值中使用非标准字符

  1. {
  2. "endpoint": "/api/users",
  3. "body": {},
  4. "method": "GET",
  5. "headers": {
  6. "Authorization": "Bearer t@ken_value"
  7. }
  8. }

40. 为只能接受正值的字段发送负数

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "age": -25
  5. },
  6. "method": "POST"
  7. }

41. 发送超出预期范围的未来或过去的时间戳

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "birthdate": "01-25-1800"
  5. },
  6. "method": "POST"
  7. }

42. 在字段值中使用HTML、JavaScript或SQL代码来尝试代码注入

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "<script>alert('test')</script>"
  5. },
  6. "method": "POST"
  7. }

43. 在有效载荷中使用不同的字符编码(例如,UTF-8, UTF-16)

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": "téstuser"
  5. },
  6. "method": "POST",
  7. "headers": {
  8. "Content-Type": "application/json; charset=UTF-16"
  9. }
  10. }

44. 发送混合数据类型的数组

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "values": [1, "string", true]
  5. },
  6. "method": "POST"
  7. }

45. 以数组或对象的形式发送字段值,而不是简单的数据类型(例如,字符串,数字)

  1. {
  2. "endpoint": "/api/users",
  3. "body": {
  4. "username": ["testuser"]
  5. },
  6. "method": "POST"
  7. }

希望这份清单能为你提供测试和保护服务器的新思路。如果您觉得这篇文章很有价值,请在 Pythagora Github repo 上支持我们。如果你试用了它,请告诉我们你的反馈,我们很高兴听到你的反馈。


声明:本文为本站编辑转载,文章版权归原作者所有。文章内容为作者个人观点,本站只提供转载参考(依行业惯例严格标明出处和作译者),目的在于传递更多专业信息,普惠测试相关从业者,开源分享,推动行业交流和进步。 如涉及作品内容、版权和其它问题,请原作者及时与本站联系(QQ:1017718740),我们将第一时间进行处理。本站拥有对此声明的最终解释权!欢迎大家通过新浪微博(@测试窝)或微信公众号(测试窝)关注我们,与我们的编辑和其他窝友交流。
200° /2005 人阅读/0 条评论 发表评论

登录 后发表评论
最新文章