Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 4x 4x 9x 24x 24x 23x 5x 18x 18x 18x 8x 7x 7x 7x 7x 7x 8x 8x 4x 3x 4x | const { BatchGetCommand } = require('@aws-sdk/lib-dynamodb')
function fieldLabel(field) {
Eif (typeof field.name === 'string') return field.name
if (field.name && typeof field.name === 'object') {
return field.name.en || field.name.fr || Object.values(field.name)[0] || 'unknown'
}
return 'unknown'
}
function errorResponse(message) {
return {
statusCode: 400,
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({ error: message })
}
}
/**
* Validates that a public entity's fields don't reference user data or
* non-public entities, which would leak flow-private data to other flows.
*
* @param {Array} fields - The entity fields array
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {string} entitiesTableName
* @returns {Promise<{ valid: boolean, errorResponse?: object }>}
*/
async function validatePublicEntityFields(fields, docClient, entitiesTableName) {
if (!Array.isArray(fields)) return { valid: true }
const userField = fields.find((field) => field.type === 'user')
if (userField) {
return {
valid: false,
errorResponse: errorResponse('Public entities cannot have a field of type "user"')
}
}
const entityRefFields = fields.filter(
(field) => field.type === 'entity_ref' && field.reference?.entityId
)
if (entityRefFields.length === 0) return { valid: true }
const uniqueEntityIds = [...new Set(entityRefFields.map((field) => field.reference.entityId))]
const batchResult = await docClient.send(
new BatchGetCommand({
RequestItems: { [entitiesTableName]: { Keys: uniqueEntityIds.map((id) => ({ id })) } }
})
)
const referencedEntities = batchResult.Responses?.[entitiesTableName] || []
const entityById = new Map(referencedEntities.map((entity) => [entity.id, entity]))
for (const field of entityRefFields) {
const target = entityById.get(field.reference.entityId)
if (!target || target.visibility !== 'public') {
return {
valid: false,
errorResponse: errorResponse(
`Field "${fieldLabel(field)}" links to a non-public entity, which is not allowed in a public entity`
)
}
}
}
return { valid: true }
}
module.exports = { validatePublicEntityFields }
|