Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hide sensitive data #404

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
fix: filter email from user endopints
  • Loading branch information
sidemt committed Jan 30, 2024
commit c4d11bd970eddf75877239bc0d3f7ff0e353e62a
134 changes: 130 additions & 4 deletions apps/backend/src/protected-populate/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"populate": {
"feature_image": {
"fields": ["url", "id"],
"fields": ["url", "id", "formats"],
"populate": {}
},
"tags": {
Expand Down Expand Up @@ -110,7 +110,7 @@
],
"populate": {
"feature_image": {
"fields": ["url", "id"]
"fields": ["url", "id", "formats"]
},
"tags": {
"fields": [
Expand Down Expand Up @@ -171,7 +171,7 @@
],
"populate": {
"feature_image": {
"fields": ["url", "id"],
"fields": ["url", "id", "formats"],
"populate": {}
},
"tags": {
Expand Down Expand Up @@ -260,7 +260,7 @@
],
"populate": {
"feature_image": {
"fields": ["url", "id"]
"fields": ["url", "id", "formats"]
},
"tags": {
"fields": [
Expand Down Expand Up @@ -684,5 +684,131 @@
]
}
}
},
"GET /api/users": {
"content-type": "plugin::users-permissions.user",
"roles": {
"contributor": {
"fields": [
"provider",
"confirmed",
"blocked",
"slug",
"name",
"bio",
"website",
"location",
"facebook",
"twitter",
"last_seen",
"ghost_id",
"status",
"createdAt",
"updatedAt",
"id"
],
"populate": {
"role": {
"fields": ["name", "type", "id", "description"]
},
"profile_image": {
"fields": ["url"]
}
}
},
"authenticated": {
"fields": [
"email",
"provider",
"confirmed",
"blocked",
"slug",
"name",
"bio",
"website",
"location",
"facebook",
"twitter",
"last_seen",
"ghost_id",
"status",
"createdAt",
"updatedAt",
"id"
],
"populate": {
"profile_image": {
"fields": ["url"]
},
"role": {
"fields": ["name", "description", "type", "id"]
}
}
},
"public": {}
}
},
"GET /api/users/:id": {
"content-type": "plugin::users-permissions.user",
"roles": {
"contributor": {
"fields": [
"provider",
"confirmed",
"blocked",
"slug",
"name",
"bio",
"website",
"location",
"facebook",
"twitter",
"last_seen",
"ghost_id",
"status",
"createdAt",
"updatedAt",
"id"
],
"populate": {
"role": {
"fields": ["name", "type", "id", "description"]
},
"profile_image": {
"fields": ["url"]
}
}
},
"authenticated": {
"fields": [
"email",
"provider",
"confirmed",
"blocked",
"slug",
"name",
"bio",
"website",
"location",
"facebook",
"twitter",
"last_seen",
"ghost_id",
"status",
"createdAt",
"updatedAt",
"id"
],
"populate": {
"profile_image": {
"fields": ["url"]
},
"role": {
"fields": ["name", "description", "type", "id"]
}
}
},
"public": {}
}
}
}
79 changes: 79 additions & 0 deletions apps/backend/tests/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const {
deleteUser,
getUserByRole,
getAllRoles,
getUser,
getUserJWT,
} = require("../helpers/helpers");

// user mock data
Expand All @@ -15,6 +17,15 @@ const mockUserData = {
blocked: null,
};

let contributorJWT = "";
let editorJWT = "";

beforeAll(async () => {
// Prepare user token
contributorJWT = await getUserJWT("contributor-user");
editorJWT = await getUserJWT("editor-user");
});

describe("user", () => {
// Example test taken from https://docs.strapi.io/dev-docs/testing
// This test should pass if the test environment is set up properly
Expand Down Expand Up @@ -55,4 +66,72 @@ describe("user", () => {
});
}
});

describe("Contributors getting user data", () => {
// Due to Strapi's permission system, if we desable the /users or /users/:id endpoint,
// it will also disable population of the user data in other endpoints.
// (e.g. /posts?populate[0]=author)
// Therefore, we are filtering out the email field in the response
// instead of disabling the entire endpoint.

it("GET /users should not return email to contributors", async () => {
const response = await request(strapi.server.httpServer)
.get(`/api/users`)
.set("Content-Type", "application/json")
.set("Authorization", `Bearer ${contributorJWT}`)
.send();

expect(response.status).toBe(200);
// check that email and username are not present in the response
expect(response.body.every((user) => !user.email && !user.username)).toBe(
true,
);
});

it("GET /users/:id should not return email to contributors", async () => {
const editorUser = await getUser("editor-user");

const response = await request(strapi.server.httpServer)
.get(`/api/users/${editorUser.id}`)
.set("Content-Type", "application/json")
.set("Authorization", `Bearer ${contributorJWT}`)
.send();

expect(response.status).toBe(200);

// check that email and username are not present in the response
expect(response.body).not.toHaveProperty("email");
expect(response.body).not.toHaveProperty("username");
});
});

describe("Editors getting user data", () => {
it("GET /users should return email to editors", async () => {
const response = await request(strapi.server.httpServer)
.get(`/api/users`)
.set("Content-Type", "application/json")
.set("Authorization", `Bearer ${editorJWT}`)
.send();

expect(response.status).toBe(200);
expect(response.body.every((user) => user.email && !user.username)).toBe(
true,
);
});

it("GET /users/:id should return email to editors", async () => {
const contributorUser = await getUser("contributor-user");

const response = await request(strapi.server.httpServer)
.get(`/api/users/${contributorUser.id}`)
.set("Content-Type", "application/json")
.set("Authorization", `Bearer ${editorJWT}`)
.send();

expect(response.status).toBe(200);

expect(response.body).toHaveProperty("email");
expect(response.body).not.toHaveProperty("username");
});
});
});