[IMPROVE] Add proxy for data export (#20998)
Co-authored-by: Diego Sampaio <chinello@gmail.com>pull/21586/head
parent
f05b07c720
commit
4b3d58fd36
@ -0,0 +1,54 @@ |
||||
import { Cookies } from 'meteor/ostrio:cookies'; |
||||
|
||||
import Users from '../../models/server/models/Users'; |
||||
import { FileUpload } from '../../file-upload/server'; |
||||
import { getURL } from '../../utils/lib/getURL'; |
||||
|
||||
const cookie = new Cookies(); |
||||
const userDataStore = FileUpload.getStore('UserDataFiles'); |
||||
|
||||
export const DataExport = { |
||||
handlers: {}, |
||||
|
||||
getPath(path = '') { |
||||
return `/data-export/${ path }`; |
||||
}, |
||||
|
||||
requestCanAccessFiles({ headers = {}, query = {} }, userId) { |
||||
let { rc_uid, rc_token } = query; |
||||
|
||||
if (!rc_uid && headers.cookie) { |
||||
rc_uid = cookie.get('rc_uid', headers.cookie); |
||||
rc_token = cookie.get('rc_token', headers.cookie); |
||||
} |
||||
|
||||
const options = { fields: { _id: 1 } }; |
||||
|
||||
if (rc_uid && rc_token && rc_uid === userId) { |
||||
return !!Users.findOneByIdAndLoginToken(rc_uid, rc_token, options); |
||||
} |
||||
|
||||
if (headers['x-user-id'] && headers['x-auth-token'] && headers['x-user-id'] === userId) { |
||||
return !!Users.findOneByIdAndLoginToken(headers['x-user-id'], headers['x-auth-token'], options); |
||||
} |
||||
|
||||
return false; |
||||
}, |
||||
|
||||
get(file, req, res, next) { |
||||
if (userDataStore && userDataStore.get) { |
||||
return userDataStore.get(file, req, res, next); |
||||
} |
||||
res.writeHead(404); |
||||
res.end(); |
||||
}, |
||||
|
||||
getErrorPage(errorType, errorDescription) { |
||||
let errorHtml = Assets.getText('errors/error_template.html'); |
||||
errorHtml = errorHtml.replace('$ERROR_TYPE$', errorType); |
||||
errorHtml = errorHtml.replace('$ERROR_DESCRIPTION$', errorDescription); |
||||
errorHtml = errorHtml.replace('$SERVER_URL$', getURL('/', { full: true, cdn: false })); |
||||
return errorHtml; |
||||
}, |
||||
|
||||
}; |
@ -0,0 +1,34 @@ |
||||
import { WebApp } from 'meteor/webapp'; |
||||
import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; |
||||
|
||||
import { UserDataFiles } from '../../models'; |
||||
import { DataExport } from './DataExport'; |
||||
import { settings } from '../../settings/server'; |
||||
|
||||
|
||||
WebApp.connectHandlers.use(DataExport.getPath(), function(req, res, next) { |
||||
const match = /^\/([^\/]+)/.exec(req.url); |
||||
|
||||
if (!settings.get('UserData_EnableDownload')) { |
||||
res.writeHead(403); |
||||
res.setHeader('Content-Type', 'text/html; charset=UTF-8'); |
||||
return res.end(DataExport.getErrorPage(TAPi18n.__('Feature_Disabled'), TAPi18n.__('UserDataDownload_FeatureDisabled'))); |
||||
} |
||||
|
||||
if (match && match[1]) { |
||||
const file = UserDataFiles.findOneById(match[1]); |
||||
if (file) { |
||||
if (!DataExport.requestCanAccessFiles(req, file.userId)) { |
||||
res.setHeader('Content-Type', 'text/html; charset=UTF-8'); |
||||
res.writeHead(403); |
||||
return res.end(DataExport.getErrorPage(TAPi18n.__('403'), TAPi18n.__('UserDataDownload_LoginNeeded'))); |
||||
} |
||||
|
||||
res.setHeader('Content-Security-Policy', 'default-src \'none\''); |
||||
res.setHeader('Cache-Control', 'max-age=31536000'); |
||||
return DataExport.get(file, req, res, next); |
||||
} |
||||
} |
||||
res.writeHead(404); |
||||
res.end(); |
||||
}); |
@ -1,2 +1,3 @@ |
||||
import './startup/settings'; |
||||
import './cronProcessDownloads'; |
||||
import './exportDownload'; |
||||
|
@ -0,0 +1,31 @@ |
||||
<html> |
||||
<style type="text/css"> |
||||
body { |
||||
margin: 40px auto; |
||||
max-width: 650px; |
||||
line-height: 1.4; |
||||
font-size: 18px; |
||||
color: #444; |
||||
padding: 0 10px; |
||||
font-family: sans-serif; |
||||
} |
||||
|
||||
h1, h2, h3 { |
||||
line-height: 1.2 |
||||
} |
||||
|
||||
div { |
||||
display: flex; |
||||
justify-content: center; |
||||
align-items: center; |
||||
flex-direction: column; |
||||
} |
||||
</style> |
||||
<body> |
||||
<div> |
||||
<h1>$ERROR_TYPE$</h1> |
||||
<p>$ERROR_DESCRIPTION$</p> |
||||
<p><a href='$SERVER_URL$'>Go home</a></p> |
||||
</div> |
||||
</body> |
||||
</html> |
Loading…
Reference in new issue