Skip to content

Commit 272adef

Browse files
committed
Do not expose rootPath.
1 parent b74e92a commit 272adef

File tree

4 files changed

+33
-37
lines changed

4 files changed

+33
-37
lines changed

lib/handlers/patch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async function patchHandler (req, res, next) {
6767
const result = await withLock(path, { mustExist: false }, async () => {
6868
const graph = await readGraph(resource)
6969
await applyPatch(patchObject, graph, url)
70-
return writeGraph(graph, resource, ldp.resourceMapper.rootPath, ldp.serverUri)
70+
return writeGraph(graph, resource, ldp.resourceMapper.getBaseFilePath(req.hostname), ldp.serverUri)
7171
})
7272

7373
// Send the result to the client

lib/ldp.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const ldpContainer = require('./ldp-container')
1717
const parse = require('./utils').parse
1818
const fetch = require('node-fetch')
1919
const { promisify } = require('util')
20+
const URL = require('url')
2021
const URI = require('urijs')
2122
const withLock = require('./lock')
2223

@@ -153,8 +154,7 @@ class LDP {
153154
originalPath += '/'
154155
}
155156
}
156-
const { url: putUrl, contentType } = await this.resourceMapper.mapFileToUrl(
157-
{ path: this.resourceMapper.rootPath + resourcePath, hostname: host })
157+
const { url: putUrl, contentType } = await this.resourceMapper.mapFileToUrl({ path: resourcePath, hostname: host })
158158

159159
// HACK: the middleware in webid-oidc.js uses body-parser, thus ending the stream of data
160160
// for JSON bodies. So, the stream needs to be reset
@@ -218,7 +218,8 @@ class LDP {
218218
// First check if we are above quota
219219
let isOverQuota
220220
try {
221-
isOverQuota = await overQuota(this.resourceMapper.rootPath, this.serverUri)
221+
const { hostname } = URL.parse(url.url || url)
222+
isOverQuota = await overQuota(this.resourceMapper.getBaseFilePath(hostname), this.serverUri)
222223
} catch (err) {
223224
throw error(500, 'Error finding user quota')
224225
}

lib/resource-mapper.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,34 @@ class ResourceMapper {
3737
}
3838
}
3939

40-
get rootPath () {
41-
return this._rootPath
40+
// Returns the URL of the given HTTP request
41+
getRequestUrl (req) {
42+
const { hostname, pathname } = this._parseUrl(req)
43+
return this.resolveUrl(hostname, pathname)
44+
}
45+
46+
// Returns the URL for the relative path on the pod
47+
resolveUrl (hostname, pathname = '') {
48+
return !this._includeHost ? `${this._rootUrl}${pathname}`
49+
: `${this._protocol}//${hostname}${this._port}${this._rootUrl}${pathname}`
50+
}
51+
52+
// Gets the base file path for the given hostname
53+
getBaseFilePath (hostname) {
54+
return !this._includeHost ? this._rootPath : `${this._rootPath}/${hostname}`
55+
}
56+
57+
// Maps a given server file to a URL
58+
async mapFileToUrl ({ path, hostname }) {
59+
// Remove the root path if specified
60+
if (path.startsWith(this._rootPath)) {
61+
path = path.substring(this._rootPath.length)
62+
}
63+
64+
// Determine the URL by chopping off everything after the dollar sign
65+
const pathname = this._removeDollarExtension(path)
66+
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname.replace(/\\/g, '/'))}`
67+
return { url, contentType: this._getContentTypeByExtension(path) }
4268
}
4369

4470
// Maps the request for a given resource and representation format to a server file
@@ -100,32 +126,6 @@ class ResourceMapper {
100126
}
101127
}
102128

103-
// Maps a given server file to a URL
104-
async mapFileToUrl ({ path, hostname }) {
105-
// Determine the URL by chopping off everything after the dollar sign
106-
let pathname = this._removeDollarExtension(path.substring(this._rootPath.length))
107-
pathname = this._replaceBackslashes(pathname)
108-
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname)}`
109-
return { url, contentType: this._getContentTypeByExtension(path) }
110-
}
111-
112-
// Gets the base file path for the given hostname
113-
getBaseFilePath (hostname) {
114-
return !this._includeHost ? this._rootPath : `${this._rootPath}/${hostname}`
115-
}
116-
117-
// Returns the URL for the given HTTP request
118-
getRequestUrl (req) {
119-
const { hostname, pathname } = this._parseUrl(req)
120-
return this.resolveUrl(hostname, pathname)
121-
}
122-
123-
// Returns the URL for the relative path on the pod
124-
resolveUrl (hostname, pathname = '') {
125-
return !this._includeHost ? `${this._rootUrl}${pathname}`
126-
: `${this._protocol}//${hostname}${this._port}${this._rootUrl}${pathname}`
127-
}
128-
129129
// Determine the full file path corresponding to a URL
130130
_getFilePath (url) {
131131
const { pathname, hostname } = this._parseUrl(url)
@@ -168,10 +168,6 @@ class ResourceMapper {
168168
const dollarPos = path.lastIndexOf('$')
169169
return dollarPos < 0 ? path : path.substr(0, dollarPos)
170170
}
171-
172-
_replaceBackslashes (path) {
173-
return path.replace(/\\/g, '/')
174-
}
175171
}
176172

177173
module.exports = ResourceMapper

lib/server-config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ function printDebugInfo (options) {
2020
debug.settings('Config path: ' + options.configPath)
2121
debug.settings('Suffix Acl: ' + options.suffixAcl)
2222
debug.settings('Suffix Meta: ' + options.suffixMeta)
23-
debug.settings('Filesystem Root: ' + (this.resourceMapper ? this.resourceMapper.rootPath : 'none'))
2423
debug.settings('Allow WebID authentication: ' + !!options.webid)
2524
debug.settings('Live-updates: ' + !!options.live)
2625
debug.settings('Multi-user: ' + !!options.multiuser)

0 commit comments

Comments
 (0)