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

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

作为开发人员,我们努力编写准确无误的代码,但实际上没有人这样做,因为……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. 请求正文的内容长度标头缺失或无效

{
    "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 上支持我们。如果你试用了它,请告诉我们你的反馈,我们很高兴听到你的反馈。


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

登录 后发表评论