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/issue#1692 #1778

Merged
merged 21 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 12 additions & 21 deletions lib/ldp.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,24 @@ class LDP {

const ldp = this
debug.handlers('POST -- On parent: ' + containerPath)
// prepare slug
if (container) {
// Containers should not receive an extension
extension = ''
}
// pepare slug
if (slug) {
if (this.isAuxResource(slug, extension)) throw error(403, 'POST is not allowed for auxiliary resources')
if (this._containsInvalidSuffixes(slug)) {
throw error(400, `${slug} is an invalid file path`)
}
slug = decodeURIComponent(slug)

if (container) {
// the name of a container cannot be a valid auxiliary resource document
if (this._containsInvalidSuffixes(slug + '/')) slug = slug.split('.')[0]
zg009 marked this conversation as resolved.
Show resolved Hide resolved
} else if (this.isAuxResource(slug, extension)) throw error(403, 'POST is not allowed for auxiliary resources')
zg009 marked this conversation as resolved.
Show resolved Hide resolved

if (slug.match(/\/|\||:/)) {
throw error(400, 'The name of new file POSTed may not contain : | or /')
zg009 marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Containers should not receive an extension
if (container) {
extension = ''
}

// always return a valid URL.
const resourceUrl = await ldp.getAvailableUrl(hostname, containerPath, { slug, extension, container })
debug.handlers('POST -- Will create at: ' + resourceUrl)
Expand Down Expand Up @@ -338,17 +339,7 @@ class LDP {
* @returns {boolean} true is fail - if the path contains reserved suffixes
*/
_containsInvalidSuffixes (path) {
// if it is a container, no suffix so remove last slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
} else {
// this is a resource, so it either ends with an extension, or just text
const lastFullStop = path.lastIndexOf('.')
if (lastFullStop !== -1) { // contains at least one full stop
path = path.slice(0, lastFullStop)
}
}
return AUXILIARY_RESOURCES.some(suffix => path.includes(suffix))
return AUXILIARY_RESOURCES.some(suffix => path.endsWith(suffix + '/'))
}

// check whether a document (or container) has the same name as another document (or container)
Expand Down
Loading
Loading