Often when people think of security testing, they think of complicated software scans, request intercepts, and IP address spoofing. But some of the most crucial application security testing can be done simply through making API requests. In this week’s post, I’m taking a look at examples of authentication testing, authorization testing, and field validation testing.
As I have in every post in this Easy Free Automation series, I’ve created an example that you can download here. This is a simple json file that can be run with Newman. As you recall from Easy Free Automation Part III: Services Tests, Newman is the command-line runner for Postman, my favorite API testing tool. If you need to install Postman or Newman, take a look at that post.
For my test application, I’m using the awesome Restful-Booker API. It’s worth noting that this API does come with some intentional bugs, two of which I’ll mention below.
The json file I’ve made available on Github is for the test collection. I didn’t include an environment file this week, because I didn’t need to store any variables. Once you have downloaded the json file, open a command window, change directories to get to the directory where the file is stored, and type newman run easyFreeSecurityTests.json. You should see sixteen tests run and pass.
Let’s take a look at the kinds of tests we’re running. The tests will be easier to interpret if you upload them into Postman; take a look at this post if you need help doing that.
The first six tests in the collection are authentication tests. I am verifying that I can’t log in with invalid credentials. But I’m verifying six different invalid username and password combinations:
- Create token with empty username
- Create token with invalid username
- Create token with empty password
- Create token with invalid password
- Create token with empty username and password
- Create token with invalid username and password
This may seem like overkill, but I have actually encountered bugs where a user can log in if the password field is blank, and where a user can log in if both the username and password are incorrect.
The assertion I am using for each of the six authentication tests is the following:
pm.test(“Bad credential message returned”, function () {
pm.expect(pm.response.text()).to.include(“Bad credentials”);
});
Ordinarily I would assert that the response code I was getting was a 401, but since this request is (incorrectly) returning a 200, I’m instead verifying the text of the response: “Bad credentials”. (For more information on HTTP error codes, see this post.)
The next six tests in my collection are authorization tests. There are three actions in the Restful-Booker that require a valid token: Put, Patch, and Delete. So for each of these requests, I’m testing that I cannot run the request with a missing token, and I cannot run the request with an invalid token:
- Update booking with no token
- Update booking with invalid token
- Partial update booking with no token
- Partial update booking with invalid token
- Delete booking with no token
- Delete booking with invalid token
- Create booking invalid checkin month
- Create booking invalid checkin day
- Create booking invalid checkout month
- Create booking invalid checkout day
In each of these tests, I am validating that I receive an invalid date message:
Hi Kristin, great post!
I would just add to this post a disclaimer, that this example does not represents all security tests that could be done. For example, SQL injections could also be used to override authentication feature.
Regards, Karlo.
Hi Karlo- I'm glad you enjoyed this post! I totally agree that these API tests do NOT represent all the security testing that should be done on an application. The point of my "Easy Free Automation" series is to give people simple ways to get started with the eight types of testing on the Automation Test Wheel. From those starting points, testers can then expand their skills. I've written a number of posts about XSS, SQLi, and other security concerns; you can find those posts in May-July 2018.
Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing. Need to learn Security Testing Services
Amazing article…
Thanks for this article on Security Testing