Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/cozy-pouch-link/src/mango.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,25 @@ export const getIndexNameFromFields = (fields, partialFilter) => {
const indexName = `by_${fields.join('_and_')}`

if (partialFilter) {
return `${indexName}_filter_(${makeKeyFromPartialFilter(partialFilter)})`
const filterKey = makeKeyFromPartialFilter(partialFilter)
return sanitizeIndexName(`${indexName}_filter_(${filterKey})`)
}

return indexName
}

/**
* Sanitize an index name to remove characters that are not compatible with
* CouchDB design document naming conventions (e.g. "/" breaks CouchDB > 3.3.3)
* https://github.com/linagora/cozy-client/issues/1667
*
* @param {string} name - The index name to sanitize
* @returns {string} The sanitized index name
*/
const sanitizeIndexName = name => {
return name.replace(/%/g, '%25').replace(/\//g, '%2F')
}

/**
* @function
* @description Compute fields that should be indexed for a mango
Expand Down
15 changes: 14 additions & 1 deletion packages/cozy-stack-client/src/mangoIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,25 @@ export const getIndexNameFromFields = (fields, partialFilter) => {
const indexName = `by_${fields.join('_and_')}`

if (partialFilter) {
return `${indexName}_filter_(${makeKeyFromPartialFilter(partialFilter)})`
const filterKey = makeKeyFromPartialFilter(partialFilter)
return sanitizeIndexName(`${indexName}_filter_(${filterKey})`)
}

return indexName
}

/**
* Sanitize an index name to remove characters that are not compatible with
* CouchDB design document naming conventions (e.g. "/" breaks CouchDB > 3.3.3)
* https://github.com/linagora/cozy-client/issues/1667
*
* @param {string} name - The index name to sanitize
* @returns {string} The sanitized index name
*/
const sanitizeIndexName = name => {
return name.replace(/%/g, '%25').replace(/\//g, '%2F')
}

/**
* Transform sort into Array
*
Expand Down
28 changes: 28 additions & 0 deletions packages/cozy-stack-client/src/mangoIndex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,34 @@ describe('getIndexNameFromFields', () => {
)
})

it('should sanitize slash characters from index name', () => {
const partialFilter = {
_id: {
$nin: ['io.cozy.files.trash-dir', 'io.cozy.files.shared-drives-dir']
},
path: {
$or: [{ $exists: false }, { $nin: ['/Settings'] }]
}
}

const indexName = getIndexNameFromFields(fields, partialFilter)
expect(indexName).not.toContain('/')
expect(indexName).toContain('%2FSettings')
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it('should produce different index names for values with and without slash', () => {
const withSlash = {
path: { $nin: ['/Settings'] }
}
const withoutSlash = {
path: { $nin: ['Settings'] }
}

const nameWithSlash = getIndexNameFromFields(fields, withSlash)
const nameWithoutSlash = getIndexNameFromFields(fields, withoutSlash)
expect(nameWithSlash).not.toEqual(nameWithoutSlash)
})

it('should return index fields with partial filter with $nor inside sub condition', () => {
const partialFilter = {
$nor: [
Expand Down
Loading