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 sanitize media #12781

Merged
merged 1 commit into from Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -2,9 +2,9 @@

const { propEq, omit } = require('lodash/fp');

const { createTestBuilder } = require('../../../../../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../../../../../test/helpers/strapi');
const { createContentAPIRequest } = require('../../../../../../../../test/helpers/request');
const { createTestBuilder } = require('../../../../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../../../../test/helpers/strapi');
const { createContentAPIRequest } = require('../../../../../../../test/helpers/request');

const builder = createTestBuilder();

Expand Down
Binary file added packages/core/strapi/tests/api/populate/rec.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions packages/core/strapi/tests/api/populate/sanitize.test.e2e.js
@@ -0,0 +1,93 @@
'use strict';

const fs = require('fs');
const path = require('path');

const { createTestBuilder } = require('../../../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../../../test/helpers/strapi');
const {
createContentAPIRequest,
createAuthRequest,
} = require('../../../../../../test/helpers/request');

const builder = createTestBuilder();

let strapi;
let rq;

const schemas = {
contentTypes: {
a: {
kind: 'collectionType',
displayName: 'a',
singularName: 'a',
pluralName: 'as',
attributes: {
cover: { type: 'media' },
},
},
},
};

const getFixtures = file => {
return [
{
cover: file.id,
},
];
};

const uploadFile = async () => {
const strapi = await createStrapiInstance();
const rq = await createAuthRequest({ strapi });

const res = await rq({
method: 'POST',
url: '/upload',
formData: {
files: fs.createReadStream(path.join(__dirname, 'rec.jpg')),
},
});

await strapi.destroy();

return res.body[0];
};

describe('Sanitize populated entries', () => {
beforeAll(async () => {
const file = await uploadFile();

await builder
.addContentTypes(Object.values(schemas.contentTypes))
.addFixtures(schemas.contentTypes.a.singularName, getFixtures(file))
.build();

strapi = await createStrapiInstance();
rq = createContentAPIRequest({ strapi });
});

afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});

describe('Populate simple media', () => {
test('Media can be populated without restricted attributes', async () => {
const { status, body } = await rq.get(`/${schemas.contentTypes.a.pluralName}`, {
qs: {
populate: {
cover: {
populate: '*',
},
},
},
});

expect(status).toBe(200);
expect(body.data[0].attributes.cover).toBeDefined();
expect(body.data[0].attributes.cover.data.attributes.createdBy).toBeUndefined();
expect(body.data[0].attributes.cover.data.attributes.updatedBy).toBeUndefined();
});
});
});
Expand Up @@ -2,6 +2,7 @@

const ACTIONS_TO_VERIFY = ['find'];

// FIXME: Support populating creator fields
module.exports = auth => async ({ data, key, attribute }, { remove, set }) => {
const isRelation = attribute.type === 'relation';

Expand Down
17 changes: 17 additions & 0 deletions packages/core/utils/lib/traverse-entity.js
Expand Up @@ -41,6 +41,7 @@ const traverseEntity = async (visitor, options, entity) => {
const isRelation = attribute.type === 'relation';
const isComponent = attribute.type === 'component';
const isDynamicZone = attribute.type === 'dynamiczone';
const isMedia = attribute.type === 'media';

if (isRelation) {
const isMorphRelation = attribute.relation.toLowerCase().startsWith('morph');
Expand All @@ -61,6 +62,22 @@ const traverseEntity = async (visitor, options, entity) => {
: await traverseTarget(value);
}

if (isMedia) {
const traverseTarget = entry => {
const targetSchemaUID = 'plugin::upload.file';
const targetSchema = strapi.getModel(targetSchemaUID);

const traverseOptions = { schema: targetSchema, path: newPath };

return traverseEntity(visitor, traverseOptions, entry);
};

// need to update copy
copy[key] = isArray(value)
? await Promise.all(value.map(traverseTarget))
: await traverseTarget(value);
}

if (isComponent) {
const targetSchema = strapi.getModel(attribute.component);
const traverseOptions = { schema: targetSchema, path: newPath };
Expand Down