fix: whitelister les champs autorisés sur create/update (mass assignment)
This commit is contained in:
@@ -70,4 +70,22 @@ describe('artiste.create', () => {
|
||||
expect(ctx.badRequest).toHaveBeenCalled()
|
||||
expect(strapi.documents).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('ignore les champs non autorisés du payload (mass assignment)', async () => {
|
||||
const {strapi, artisteDocuments} = buildStrapi({dbUser})
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx(buildData({
|
||||
isExclusiveArtist: true,
|
||||
userAdmin: {id: 999}
|
||||
}))
|
||||
|
||||
await controller.create(ctx)
|
||||
|
||||
expect(artisteDocuments.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
alias: 'Test Artist',
|
||||
user: dbUser.id
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -37,7 +37,8 @@ module.exports = createCoreController('api::artiste.artiste', ({strapi}) => ({
|
||||
} else {
|
||||
const newArtiste = await strapi.documents('api::artiste.artiste').create({
|
||||
data: {
|
||||
...data
|
||||
alias: data.alias,
|
||||
user: user.id
|
||||
}
|
||||
})
|
||||
return newArtiste
|
||||
|
||||
@@ -99,4 +99,21 @@ describe('commentaire.create', () => {
|
||||
await expect(controller.create(ctx)).rejects.toThrow('Informations manquantes.')
|
||||
expect(paroleDbQuery.findOne).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('ignore les champs non autorisés du payload (mass assignment)', async () => {
|
||||
const {strapi, commentaireDocuments} = buildStrapi()
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx(buildData({publishedAt: '2020-01-01'}))
|
||||
|
||||
await controller.create(ctx)
|
||||
|
||||
expect(commentaireDocuments.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
contenu: 'Un commentaire',
|
||||
datePublication: '2026-07-04',
|
||||
user: dbUser.id,
|
||||
parole: dbParole.id
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -24,8 +24,6 @@ module.exports = createCoreController('api::commentaire.commentaire', ({strapi})
|
||||
throw new ApplicationError('Informations non valides.')
|
||||
}
|
||||
|
||||
data.user = user.id
|
||||
|
||||
const parole = await strapi.db.query('api::parole.parole').findOne({
|
||||
where: {id: data.parole}
|
||||
})
|
||||
@@ -36,7 +34,10 @@ module.exports = createCoreController('api::commentaire.commentaire', ({strapi})
|
||||
|
||||
const newCommentaire = await strapi.documents('api::commentaire.commentaire').create({
|
||||
data: {
|
||||
...data
|
||||
contenu: data.contenu,
|
||||
datePublication: data.datePublication,
|
||||
user: user.id,
|
||||
parole: parole.id
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -5,7 +5,8 @@ const {default: createController} = await import('../parole.js')
|
||||
function buildStrapi({dbUser, artiste}) {
|
||||
const paroleDocuments = {
|
||||
findMany: vi.fn(async () => []),
|
||||
create: vi.fn(async ({data}) => ({id: 42, ...data}))
|
||||
create: vi.fn(async ({data}) => ({id: 42, ...data})),
|
||||
update: vi.fn(async ({data}) => ({id: 42, ...data}))
|
||||
}
|
||||
const userDocuments = {
|
||||
findOne: vi.fn(async () => dbUser),
|
||||
@@ -44,7 +45,7 @@ function buildCtx(data) {
|
||||
}
|
||||
|
||||
const dbUser = {id: 1, documentId: 'user-doc-1', username: 'foo', email: 'foo@bar.com'}
|
||||
const artiste = {documentId: 'artiste-doc-1'}
|
||||
const artiste = {id: 9, documentId: 'artiste-doc-1'}
|
||||
|
||||
function buildData(overrides = {}) {
|
||||
return {
|
||||
@@ -113,4 +114,57 @@ describe('parole.create', () => {
|
||||
expect(ctx.badRequest).toHaveBeenCalled()
|
||||
expect(artisteDocuments.findOne).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('ignore les champs non autorisés du payload (mass assignment)', async () => {
|
||||
const {strapi, paroleDocuments} = buildStrapi({dbUser, artiste})
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx(buildData({
|
||||
userAdmin: {id: 999},
|
||||
isNewRelease: true,
|
||||
difference: [{fake: true}]
|
||||
}))
|
||||
|
||||
await controller.create(ctx)
|
||||
|
||||
expect(paroleDocuments.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
titre: 'Test',
|
||||
transcription: 'Paroles...',
|
||||
traductions: undefined,
|
||||
traductionAuto: undefined,
|
||||
artistes: [artiste.id],
|
||||
user: dbUser.id
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('parole.update', () => {
|
||||
it('ignore les champs non autorisés du payload (mass assignment)', async () => {
|
||||
const {strapi, paroleDocuments} = buildStrapi({dbUser, artiste})
|
||||
const controller = createController({strapi})
|
||||
const ctx = buildCtx({
|
||||
documentId: 'doc-1',
|
||||
titre: 'Nouveau titre',
|
||||
transcription: 'Nouveau texte',
|
||||
traductions: {francais: 'salut'},
|
||||
traductionAuto: true,
|
||||
artistes: [9],
|
||||
userAdmin: {id: 999},
|
||||
user: {id: 999}
|
||||
})
|
||||
|
||||
await controller.update(ctx)
|
||||
|
||||
expect(paroleDocuments.update).toHaveBeenCalledWith({
|
||||
documentId: 'doc-1',
|
||||
data: {
|
||||
titre: 'Nouveau titre',
|
||||
transcription: 'Nouveau texte',
|
||||
traductions: {francais: 'salut'},
|
||||
traductionAuto: true,
|
||||
artistes: [9]
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -61,7 +61,11 @@ module.exports = createCoreController('api::parole.parole', ({strapi}) => ({
|
||||
documentId: data.documentId,
|
||||
|
||||
data: {
|
||||
...data
|
||||
titre: data.titre,
|
||||
transcription: data.transcription,
|
||||
traductions: data.traductions,
|
||||
traductionAuto: data.traductionAuto,
|
||||
artistes: data.artistes
|
||||
}
|
||||
})
|
||||
|
||||
@@ -119,7 +123,12 @@ module.exports = createCoreController('api::parole.parole', ({strapi}) => ({
|
||||
|
||||
const newParole = await strapi.documents('api::parole.parole').create({
|
||||
data: {
|
||||
...data
|
||||
titre: data.titre,
|
||||
transcription: data.transcription,
|
||||
traductions: data.traductions,
|
||||
traductionAuto: data.traductionAuto,
|
||||
artistes: [artiste.id],
|
||||
user: user.id
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user