@@ -145,18 +145,26 @@ class LDP {
145145
146146 const ldp = this
147147 debug . handlers ( 'POST -- On parent: ' + containerPath )
148- // prepare slug
148+ if ( container ) {
149+ // Containers should not receive an extension
150+ extension = ''
151+ }
152+ // pepare slug
149153 if ( slug ) {
150- if ( this . isAuxResource ( slug , extension ) ) throw error ( 403 , 'POST is not allowed for auxiliary resources' )
151154 slug = decodeURIComponent ( slug )
155+
156+ if ( container ) {
157+ // the name of a container cannot be a valid auxiliary resource document
158+ while ( this . _containsInvalidSuffixes ( slug + '/' ) ) {
159+ const idx = slug . lastIndexOf ( '.' )
160+ slug = slug . substr ( 0 , idx )
161+ }
162+ } else if ( this . isAuxResource ( slug , extension ) ) throw error ( 403 , 'POST to auxiliary resources is not allowed' )
163+
152164 if ( slug . match ( / \/ | \| | : / ) ) {
153- throw error ( 400 , 'The name of new file POSTed may not contain : | or / ' )
165+ throw error ( 400 , 'The name of a POSTed new file may not contain ":" (colon), "|" (pipe), or "/" (slash) ' )
154166 }
155167 }
156- // Containers should not receive an extension
157- if ( container ) {
158- extension = ''
159- }
160168
161169 // always return a valid URL.
162170 const resourceUrl = await ldp . getAvailableUrl ( hostname , containerPath , { slug, extension, container } )
@@ -327,11 +335,25 @@ class LDP {
327335 } catch ( err ) { }
328336 }
329337
338+ /**
339+ * This function is used to make sure a resource or container which contains
340+ * reserved suffixes for auxiliary documents cannot be created.
341+ * @param {string } path - the uri to check for invalid suffixes
342+ * @returns {boolean } true is fail - if the path contains reserved suffixes
343+ */
344+ _containsInvalidSuffixes ( path ) {
345+ return AUXILIARY_RESOURCES . some ( suffix => path . endsWith ( suffix + '/' ) )
346+ }
347+
330348 // check whether a document (or container) has the same name as another document (or container)
331349 async checkItemName ( url ) {
332350 let testName , testPath
333351 const { hostname, pathname } = this . resourceMapper . _parseUrl ( url ) // (url.url || url)
334352 let itemUrl = this . resourceMapper . resolveUrl ( hostname , pathname )
353+ // make sure the resource being created does not attempt invalid resource creation
354+ if ( this . _containsInvalidSuffixes ( itemUrl ) ) {
355+ throw error ( 400 , `${ itemUrl } contained reserved suffixes in path` )
356+ }
335357 const container = itemUrl . endsWith ( '/' )
336358 try {
337359 const testUrl = container ? itemUrl . slice ( 0 , - 1 ) : itemUrl + '/'
0 commit comments