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

Prevent circular collections #1608

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 21 additions & 1 deletion lib/views/addToCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const pug = require('pug')
const config = require('../config')
const sparql = require('../sparql/sparql')
const getOwnedBy = require('../query/ownedBy')
const getUrisFromReq = require('../getUrisFromReq')

module.exports = function (req, res) {
req.setTimeout(0) // no timeout
Expand All @@ -18,11 +19,16 @@ module.exports = function (req, res) {
}
}

const { uri } = getUrisFromReq(req, res)

return sparql.queryJson(collectionQuery, req.user.graphUri).then((collections) => {
collections.map((result) => {
collections = collections.filter((result) => {
return result.subject !== uri
}).map((result) => {
result.uri = result.subject
result.name = result.name ? result.name : result.uri.toString()
delete result.subject
return result
})
collections.sort(sortByNames)

Expand Down Expand Up @@ -64,6 +70,20 @@ module.exports = function (req, res) {
memberUri = memberUri.replace('/user/', config.get('databasePrefix') + 'user/')
}

if (memberUri === uri) {
if (!req.accepts('text/html')) {
return res.status(400).type('text/plain').send('Cannot make a collection a member of itself')
} else {
const locals = {
config: config.get(),
section: 'errors',
user: req.user,
errors: [ 'Cannot make a collection a member of itself' ]
}
return res.status(400).send(pug.renderFile('templates/views/errors/errors.jade', locals))
}
}

var templateParams = {
uri: uri,
memberUri: memberUri
Expand Down
Loading