router.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. const AdminController = require('./Features/ServerAdmin/AdminController')
  2. const ErrorController = require('./Features/Errors/ErrorController')
  3. const ProjectController = require('./Features/Project/ProjectController')
  4. const ProjectApiController = require('./Features/Project/ProjectApiController')
  5. const SpellingController = require('./Features/Spelling/SpellingController')
  6. const EditorRouter = require('./Features/Editor/EditorRouter')
  7. const Settings = require('settings-sharelatex')
  8. const TpdsController = require('./Features/ThirdPartyDataStore/TpdsController')
  9. const SubscriptionRouter = require('./Features/Subscription/SubscriptionRouter')
  10. const UploadsRouter = require('./Features/Uploads/UploadsRouter')
  11. const metrics = require('@overleaf/metrics')
  12. const ReferalController = require('./Features/Referal/ReferalController')
  13. const AuthenticationController = require('./Features/Authentication/AuthenticationController')
  14. const TagsController = require('./Features/Tags/TagsController')
  15. const NotificationsController = require('./Features/Notifications/NotificationsController')
  16. const CollaboratorsRouter = require('./Features/Collaborators/CollaboratorsRouter')
  17. const UserInfoController = require('./Features/User/UserInfoController')
  18. const UserController = require('./Features/User/UserController')
  19. const UserEmailsController = require('./Features/User/UserEmailsController')
  20. const UserPagesController = require('./Features/User/UserPagesController')
  21. const DocumentController = require('./Features/Documents/DocumentController')
  22. const CompileManager = require('./Features/Compile/CompileManager')
  23. const CompileController = require('./Features/Compile/CompileController')
  24. const ClsiCookieManager = require('./Features/Compile/ClsiCookieManager')(
  25. Settings.apis.clsi != null ? Settings.apis.clsi.backendGroupName : undefined
  26. )
  27. const HealthCheckController = require('./Features/HealthCheck/HealthCheckController')
  28. const ProjectDownloadsController = require('./Features/Downloads/ProjectDownloadsController')
  29. const FileStoreController = require('./Features/FileStore/FileStoreController')
  30. const HistoryController = require('./Features/History/HistoryController')
  31. const ExportsController = require('./Features/Exports/ExportsController')
  32. const PasswordResetRouter = require('./Features/PasswordReset/PasswordResetRouter')
  33. const StaticPagesRouter = require('./Features/StaticPages/StaticPagesRouter')
  34. const ChatController = require('./Features/Chat/ChatController')
  35. const Modules = require('./infrastructure/Modules')
  36. const RateLimiterMiddleware = require('./Features/Security/RateLimiterMiddleware')
  37. const InactiveProjectController = require('./Features/InactiveData/InactiveProjectController')
  38. const ContactRouter = require('./Features/Contacts/ContactRouter')
  39. const ReferencesController = require('./Features/References/ReferencesController')
  40. const AuthorizationMiddleware = require('./Features/Authorization/AuthorizationMiddleware')
  41. const BetaProgramController = require('./Features/BetaProgram/BetaProgramController')
  42. const AnalyticsRouter = require('./Features/Analytics/AnalyticsRouter')
  43. const MetaController = require('./Features/Metadata/MetaController')
  44. const TokenAccessController = require('./Features/TokenAccess/TokenAccessController')
  45. const Features = require('./infrastructure/Features')
  46. const LinkedFilesRouter = require('./Features/LinkedFiles/LinkedFilesRouter')
  47. const TemplatesRouter = require('./Features/Templates/TemplatesRouter')
  48. const InstitutionsController = require('./Features/Institutions/InstitutionsController')
  49. const UserMembershipRouter = require('./Features/UserMembership/UserMembershipRouter')
  50. const SystemMessageController = require('./Features/SystemMessages/SystemMessageController')
  51. const { Joi, validate } = require('./infrastructure/Validation')
  52. const {
  53. renderUnsupportedBrowserPage,
  54. unsupportedBrowserMiddleware,
  55. } = require('./infrastructure/UnsupportedBrowserMiddleware')
  56. const logger = require('logger-sharelatex')
  57. const _ = require('underscore')
  58. module.exports = { initialize }
  59. function initialize(webRouter, privateApiRouter, publicApiRouter) {
  60. if (!Settings.allowPublicAccess) {
  61. webRouter.all('*', AuthenticationController.requireGlobalLogin)
  62. }
  63. webRouter.get('/login', UserPagesController.loginPage)
  64. AuthenticationController.addEndpointToLoginWhitelist('/login')
  65. webRouter.post('/login', AuthenticationController.passportLogin)
  66. webRouter.get(
  67. '/read-only/one-time-login',
  68. UserPagesController.oneTimeLoginPage
  69. )
  70. AuthenticationController.addEndpointToLoginWhitelist(
  71. '/read-only/one-time-login'
  72. )
  73. webRouter.get('/logout', UserPagesController.logoutPage)
  74. webRouter.post('/logout', UserController.logout)
  75. webRouter.get('/restricted', AuthorizationMiddleware.restricted)
  76. if (Features.hasFeature('registration')) {
  77. webRouter.get('/register', UserPagesController.registerPage)
  78. AuthenticationController.addEndpointToLoginWhitelist('/register')
  79. }
  80. EditorRouter.apply(webRouter, privateApiRouter)
  81. CollaboratorsRouter.apply(webRouter, privateApiRouter)
  82. SubscriptionRouter.apply(webRouter, privateApiRouter, publicApiRouter)
  83. UploadsRouter.apply(webRouter, privateApiRouter)
  84. PasswordResetRouter.apply(webRouter, privateApiRouter)
  85. StaticPagesRouter.apply(webRouter, privateApiRouter)
  86. ContactRouter.apply(webRouter, privateApiRouter)
  87. AnalyticsRouter.apply(webRouter, privateApiRouter, publicApiRouter)
  88. LinkedFilesRouter.apply(webRouter, privateApiRouter, publicApiRouter)
  89. TemplatesRouter.apply(webRouter)
  90. UserMembershipRouter.apply(webRouter)
  91. Modules.applyRouter(webRouter, privateApiRouter, publicApiRouter)
  92. if (Settings.enableSubscriptions) {
  93. webRouter.get(
  94. '/user/bonus',
  95. AuthenticationController.requireLogin(),
  96. ReferalController.bonus
  97. )
  98. }
  99. // .getMessages will generate an empty response for anonymous users.
  100. webRouter.get('/system/messages', SystemMessageController.getMessages)
  101. webRouter.get(
  102. '/user/settings',
  103. AuthenticationController.requireLogin(),
  104. UserPagesController.settingsPage
  105. )
  106. webRouter.post(
  107. '/user/settings',
  108. AuthenticationController.requireLogin(),
  109. UserController.updateUserSettings
  110. )
  111. webRouter.post(
  112. '/user/password/update',
  113. AuthenticationController.requireLogin(),
  114. RateLimiterMiddleware.rateLimit({
  115. endpointName: 'change-password',
  116. maxRequests: 10,
  117. timeInterval: 60,
  118. }),
  119. UserController.changePassword
  120. )
  121. webRouter.get(
  122. '/user/emails',
  123. AuthenticationController.requireLogin(),
  124. UserController.promises.ensureAffiliationMiddleware,
  125. UserEmailsController.list
  126. )
  127. webRouter.get('/user/emails/confirm', UserEmailsController.showConfirm)
  128. webRouter.post(
  129. '/user/emails/confirm',
  130. RateLimiterMiddleware.rateLimit({
  131. endpointName: 'confirm-email',
  132. maxRequests: 10,
  133. timeInterval: 60,
  134. }),
  135. UserEmailsController.confirm
  136. )
  137. webRouter.post(
  138. '/user/emails/resend_confirmation',
  139. AuthenticationController.requireLogin(),
  140. RateLimiterMiddleware.rateLimit({
  141. endpointName: 'resend-confirmation',
  142. maxRequests: 10,
  143. timeInterval: 60,
  144. }),
  145. UserEmailsController.resendConfirmation
  146. )
  147. if (Features.hasFeature('affiliations')) {
  148. webRouter.post(
  149. '/user/emails',
  150. AuthenticationController.requireLogin(),
  151. RateLimiterMiddleware.rateLimit({
  152. endpointName: 'add-email',
  153. maxRequests: 10,
  154. timeInterval: 60,
  155. }),
  156. UserEmailsController.add
  157. )
  158. webRouter.post(
  159. '/user/emails/delete',
  160. AuthenticationController.requireLogin(),
  161. RateLimiterMiddleware.rateLimit({
  162. endpointName: 'delete-email',
  163. maxRequests: 10,
  164. timeInterval: 60,
  165. }),
  166. UserEmailsController.remove
  167. )
  168. webRouter.post(
  169. '/user/emails/default',
  170. AuthenticationController.requireLogin(),
  171. UserEmailsController.setDefault
  172. )
  173. webRouter.post(
  174. '/user/emails/endorse',
  175. AuthenticationController.requireLogin(),
  176. RateLimiterMiddleware.rateLimit({
  177. endpointName: 'endorse-email',
  178. maxRequests: 30,
  179. timeInterval: 60,
  180. }),
  181. UserEmailsController.endorse
  182. )
  183. }
  184. webRouter.get(
  185. '/user/sessions',
  186. AuthenticationController.requireLogin(),
  187. UserPagesController.sessionsPage
  188. )
  189. webRouter.post(
  190. '/user/sessions/clear',
  191. AuthenticationController.requireLogin(),
  192. UserController.clearSessions
  193. )
  194. webRouter.delete(
  195. '/user/newsletter/unsubscribe',
  196. AuthenticationController.requireLogin(),
  197. UserController.unsubscribe
  198. )
  199. webRouter.post(
  200. '/user/delete',
  201. RateLimiterMiddleware.rateLimit({
  202. endpointName: 'delete-user',
  203. maxRequests: 10,
  204. timeInterval: 60,
  205. }),
  206. AuthenticationController.requireLogin(),
  207. UserController.tryDeleteUser
  208. )
  209. webRouter.get(
  210. '/user/personal_info',
  211. AuthenticationController.requireLogin(),
  212. UserInfoController.getLoggedInUsersPersonalInfo
  213. )
  214. privateApiRouter.get(
  215. '/user/:user_id/personal_info',
  216. AuthenticationController.httpAuth,
  217. UserInfoController.getPersonalInfo
  218. )
  219. webRouter.get(
  220. '/user/reconfirm',
  221. UserPagesController.renderReconfirmAccountPage
  222. )
  223. // for /user/reconfirm POST, see password router
  224. webRouter.get(
  225. '/user/tpds/queues',
  226. AuthenticationController.requireLogin(),
  227. TpdsController.getQueues
  228. )
  229. webRouter.get(
  230. '/user/projects',
  231. AuthenticationController.requireLogin(),
  232. ProjectController.userProjectsJson
  233. )
  234. webRouter.get(
  235. '/project/:Project_id/entities',
  236. AuthenticationController.requireLogin(),
  237. AuthorizationMiddleware.ensureUserCanReadProject,
  238. ProjectController.projectEntitiesJson
  239. )
  240. webRouter.get(
  241. '/project',
  242. AuthenticationController.requireLogin(),
  243. RateLimiterMiddleware.rateLimit({
  244. endpointName: 'open-dashboard',
  245. maxRequests: 30,
  246. timeInterval: 60,
  247. }),
  248. unsupportedBrowserMiddleware,
  249. ProjectController.projectListPage
  250. )
  251. webRouter.post(
  252. '/project/new',
  253. AuthenticationController.requireLogin(),
  254. RateLimiterMiddleware.rateLimit({
  255. endpointName: 'create-project',
  256. maxRequests: 20,
  257. timeInterval: 60,
  258. }),
  259. ProjectController.newProject
  260. )
  261. webRouter.get(
  262. '/Project/:Project_id',
  263. RateLimiterMiddleware.rateLimit({
  264. endpointName: 'open-project',
  265. params: ['Project_id'],
  266. maxRequests: 15,
  267. timeInterval: 60,
  268. }),
  269. unsupportedBrowserMiddleware,
  270. AuthenticationController.validateUserSession(),
  271. AuthorizationMiddleware.ensureUserCanReadProject,
  272. ProjectController.loadEditor
  273. )
  274. webRouter.head(
  275. '/Project/:Project_id/file/:File_id',
  276. AuthorizationMiddleware.ensureUserCanReadProject,
  277. FileStoreController.getFileHead
  278. )
  279. webRouter.get(
  280. '/Project/:Project_id/file/:File_id',
  281. AuthorizationMiddleware.ensureUserCanReadProject,
  282. FileStoreController.getFile
  283. )
  284. webRouter.post(
  285. '/project/:Project_id/settings',
  286. AuthorizationMiddleware.ensureUserCanWriteProjectSettings,
  287. ProjectController.updateProjectSettings
  288. )
  289. webRouter.post(
  290. '/project/:Project_id/settings/admin',
  291. AuthenticationController.requireLogin(),
  292. AuthorizationMiddleware.ensureUserCanAdminProject,
  293. ProjectController.updateProjectAdminSettings
  294. )
  295. webRouter.post(
  296. '/project/:Project_id/compile',
  297. RateLimiterMiddleware.rateLimit({
  298. endpointName: 'compile-project-http',
  299. params: ['Project_id'],
  300. maxRequests: 800,
  301. timeInterval: 60 * 60,
  302. }),
  303. AuthorizationMiddleware.ensureUserCanReadProject,
  304. CompileController.compile
  305. )
  306. webRouter.post(
  307. '/project/:Project_id/compile/stop',
  308. AuthorizationMiddleware.ensureUserCanReadProject,
  309. CompileController.stopCompile
  310. )
  311. // LEGACY: Used by the web download buttons, adds filename header, TODO: remove at some future date
  312. webRouter.get(
  313. '/project/:Project_id/output/output.pdf',
  314. AuthorizationMiddleware.ensureUserCanReadProject,
  315. CompileController.downloadPdf
  316. )
  317. // PDF Download button
  318. webRouter.get(
  319. /^\/download\/project\/([^/]*)\/output\/output\.pdf$/,
  320. function (req, res, next) {
  321. const params = { Project_id: req.params[0] }
  322. req.params = params
  323. next()
  324. },
  325. AuthorizationMiddleware.ensureUserCanReadProject,
  326. CompileController.downloadPdf
  327. )
  328. // PDF Download button for specific build
  329. webRouter.get(
  330. /^\/download\/project\/([^/]*)\/build\/([0-9a-f-]+)\/output\/output\.pdf$/,
  331. function (req, res, next) {
  332. const params = {
  333. Project_id: req.params[0],
  334. build_id: req.params[1],
  335. }
  336. req.params = params
  337. next()
  338. },
  339. AuthorizationMiddleware.ensureUserCanReadProject,
  340. CompileController.downloadPdf
  341. )
  342. // Align with limits defined in CompileController.downloadPdf
  343. const rateLimiterMiddlewareOutputFiles = RateLimiterMiddleware.rateLimit({
  344. endpointName: 'misc-output-download',
  345. params: ['Project_id'],
  346. maxRequests: 1000,
  347. timeInterval: 60 * 60,
  348. })
  349. // Used by the pdf viewers
  350. webRouter.get(
  351. /^\/project\/([^/]*)\/output\/(.*)$/,
  352. function (req, res, next) {
  353. const params = {
  354. Project_id: req.params[0],
  355. file: req.params[1],
  356. }
  357. req.params = params
  358. next()
  359. },
  360. rateLimiterMiddlewareOutputFiles,
  361. AuthorizationMiddleware.ensureUserCanReadProject,
  362. CompileController.getFileFromClsi
  363. )
  364. // direct url access to output files for a specific build (query string not required)
  365. webRouter.get(
  366. /^\/project\/([^/]*)\/build\/([0-9a-f-]+)\/output\/(.*)$/,
  367. function (req, res, next) {
  368. const params = {
  369. Project_id: req.params[0],
  370. build_id: req.params[1],
  371. file: req.params[2],
  372. }
  373. req.params = params
  374. next()
  375. },
  376. rateLimiterMiddlewareOutputFiles,
  377. AuthorizationMiddleware.ensureUserCanReadProject,
  378. CompileController.getFileFromClsi
  379. )
  380. // direct url access to output files for user but no build, to retrieve files when build fails
  381. webRouter.get(
  382. /^\/project\/([^/]*)\/user\/([0-9a-f-]+)\/output\/(.*)$/,
  383. function (req, res, next) {
  384. const params = {
  385. Project_id: req.params[0],
  386. user_id: req.params[1],
  387. file: req.params[2],
  388. }
  389. req.params = params
  390. next()
  391. },
  392. rateLimiterMiddlewareOutputFiles,
  393. AuthorizationMiddleware.ensureUserCanReadProject,
  394. CompileController.getFileFromClsi
  395. )
  396. // direct url access to output files for a specific user and build (query string not required)
  397. webRouter.get(
  398. /^\/project\/([^/]*)\/user\/([0-9a-f]+)\/build\/([0-9a-f-]+)\/output\/(.*)$/,
  399. function (req, res, next) {
  400. const params = {
  401. Project_id: req.params[0],
  402. user_id: req.params[1],
  403. build_id: req.params[2],
  404. file: req.params[3],
  405. }
  406. req.params = params
  407. next()
  408. },
  409. rateLimiterMiddlewareOutputFiles,
  410. AuthorizationMiddleware.ensureUserCanReadProject,
  411. CompileController.getFileFromClsi
  412. )
  413. webRouter.delete(
  414. '/project/:Project_id/output',
  415. validate({ query: { clsiserverid: Joi.string() } }),
  416. AuthorizationMiddleware.ensureUserCanReadProject,
  417. CompileController.deleteAuxFiles
  418. )
  419. webRouter.get(
  420. '/project/:Project_id/sync/code',
  421. AuthorizationMiddleware.ensureUserCanReadProject,
  422. CompileController.proxySyncCode
  423. )
  424. webRouter.get(
  425. '/project/:Project_id/sync/pdf',
  426. AuthorizationMiddleware.ensureUserCanReadProject,
  427. CompileController.proxySyncPdf
  428. )
  429. webRouter.get(
  430. '/project/:Project_id/wordcount',
  431. validate({ query: { clsiserverid: Joi.string() } }),
  432. AuthorizationMiddleware.ensureUserCanReadProject,
  433. CompileController.wordCount
  434. )
  435. webRouter.post(
  436. '/Project/:Project_id/archive',
  437. AuthorizationMiddleware.ensureUserCanReadProject,
  438. ProjectController.archiveProject
  439. )
  440. webRouter.delete(
  441. '/Project/:Project_id/archive',
  442. AuthorizationMiddleware.ensureUserCanReadProject,
  443. ProjectController.unarchiveProject
  444. )
  445. webRouter.post(
  446. '/project/:project_id/trash',
  447. AuthorizationMiddleware.ensureUserCanReadProject,
  448. ProjectController.trashProject
  449. )
  450. webRouter.delete(
  451. '/project/:project_id/trash',
  452. AuthorizationMiddleware.ensureUserCanReadProject,
  453. ProjectController.untrashProject
  454. )
  455. webRouter.delete(
  456. '/Project/:Project_id',
  457. AuthenticationController.requireLogin(),
  458. AuthorizationMiddleware.ensureUserCanAdminProject,
  459. ProjectController.deleteProject
  460. )
  461. webRouter.post(
  462. '/Project/:Project_id/restore',
  463. AuthenticationController.requireLogin(),
  464. AuthorizationMiddleware.ensureUserCanAdminProject,
  465. ProjectController.restoreProject
  466. )
  467. webRouter.post(
  468. '/Project/:Project_id/clone',
  469. AuthorizationMiddleware.ensureUserCanReadProject,
  470. ProjectController.cloneProject
  471. )
  472. webRouter.post(
  473. '/project/:Project_id/rename',
  474. AuthenticationController.requireLogin(),
  475. AuthorizationMiddleware.ensureUserCanAdminProject,
  476. ProjectController.renameProject
  477. )
  478. webRouter.get(
  479. '/project/:Project_id/updates',
  480. AuthorizationMiddleware.ensureUserCanReadProject,
  481. HistoryController.selectHistoryApi,
  482. HistoryController.proxyToHistoryApiAndInjectUserDetails
  483. )
  484. webRouter.get(
  485. '/project/:Project_id/doc/:doc_id/diff',
  486. AuthorizationMiddleware.ensureUserCanReadProject,
  487. HistoryController.selectHistoryApi,
  488. HistoryController.proxyToHistoryApi
  489. )
  490. webRouter.get(
  491. '/project/:Project_id/diff',
  492. AuthorizationMiddleware.ensureUserCanReadProject,
  493. HistoryController.selectHistoryApi,
  494. HistoryController.proxyToHistoryApiAndInjectUserDetails
  495. )
  496. webRouter.get(
  497. '/project/:Project_id/filetree/diff',
  498. AuthorizationMiddleware.ensureUserCanReadProject,
  499. HistoryController.selectHistoryApi,
  500. HistoryController.proxyToHistoryApi
  501. )
  502. webRouter.post(
  503. '/project/:Project_id/doc/:doc_id/version/:version_id/restore',
  504. AuthorizationMiddleware.ensureUserCanWriteProjectContent,
  505. HistoryController.selectHistoryApi,
  506. HistoryController.proxyToHistoryApi
  507. )
  508. webRouter.post(
  509. '/project/:project_id/doc/:doc_id/restore',
  510. AuthorizationMiddleware.ensureUserCanWriteProjectContent,
  511. HistoryController.restoreDocFromDeletedDoc
  512. )
  513. webRouter.post(
  514. '/project/:project_id/restore_file',
  515. AuthorizationMiddleware.ensureUserCanWriteProjectContent,
  516. HistoryController.restoreFileFromV2
  517. )
  518. webRouter.get(
  519. '/project/:project_id/version/:version/zip',
  520. RateLimiterMiddleware.rateLimit({
  521. endpointName: 'download-project-revision',
  522. maxRequests: 30,
  523. timeInterval: 60 * 60,
  524. }),
  525. AuthorizationMiddleware.ensureUserCanReadProject,
  526. HistoryController.downloadZipOfVersion
  527. )
  528. privateApiRouter.post(
  529. '/project/:Project_id/history/resync',
  530. AuthenticationController.httpAuth,
  531. HistoryController.resyncProjectHistory
  532. )
  533. webRouter.get(
  534. '/project/:Project_id/labels',
  535. AuthorizationMiddleware.ensureUserCanReadProject,
  536. HistoryController.selectHistoryApi,
  537. HistoryController.ensureProjectHistoryEnabled,
  538. HistoryController.getLabels
  539. )
  540. webRouter.post(
  541. '/project/:Project_id/labels',
  542. AuthorizationMiddleware.ensureUserCanWriteProjectContent,
  543. HistoryController.selectHistoryApi,
  544. HistoryController.ensureProjectHistoryEnabled,
  545. HistoryController.createLabel
  546. )
  547. webRouter.delete(
  548. '/project/:Project_id/labels/:label_id',
  549. AuthorizationMiddleware.ensureUserCanWriteProjectContent,
  550. HistoryController.selectHistoryApi,
  551. HistoryController.ensureProjectHistoryEnabled,
  552. HistoryController.deleteLabel
  553. )
  554. webRouter.post(
  555. '/project/:project_id/export/:brand_variation_id',
  556. AuthorizationMiddleware.ensureUserCanWriteProjectContent,
  557. ExportsController.exportProject
  558. )
  559. webRouter.get(
  560. '/project/:project_id/export/:export_id',
  561. AuthorizationMiddleware.ensureUserCanWriteProjectContent,
  562. ExportsController.exportStatus
  563. )
  564. webRouter.get(
  565. '/project/:project_id/export/:export_id/:type',
  566. AuthorizationMiddleware.ensureUserCanWriteProjectContent,
  567. ExportsController.exportDownload
  568. )
  569. webRouter.get(
  570. '/Project/:Project_id/download/zip',
  571. AuthorizationMiddleware.ensureUserCanReadProject,
  572. ProjectDownloadsController.downloadProject
  573. )
  574. webRouter.get(
  575. '/project/download/zip',
  576. AuthorizationMiddleware.ensureUserCanReadMultipleProjects,
  577. ProjectDownloadsController.downloadMultipleProjects
  578. )
  579. webRouter.get(
  580. '/project/:project_id/metadata',
  581. AuthorizationMiddleware.ensureUserCanReadProject,
  582. Settings.allowAnonymousReadAndWriteSharing
  583. ? (req, res, next) => {
  584. next()
  585. }
  586. : AuthenticationController.requireLogin(),
  587. MetaController.getMetadata
  588. )
  589. webRouter.post(
  590. '/project/:project_id/doc/:doc_id/metadata',
  591. AuthorizationMiddleware.ensureUserCanReadProject,
  592. Settings.allowAnonymousReadAndWriteSharing
  593. ? (req, res, next) => {
  594. next()
  595. }
  596. : AuthenticationController.requireLogin(),
  597. MetaController.broadcastMetadataForDoc
  598. )
  599. privateApiRouter.post(
  600. '/internal/expire-deleted-projects-after-duration',
  601. AuthenticationController.httpAuth,
  602. ProjectController.expireDeletedProjectsAfterDuration
  603. )
  604. privateApiRouter.post(
  605. '/internal/expire-deleted-users-after-duration',
  606. AuthenticationController.httpAuth,
  607. UserController.expireDeletedUsersAfterDuration
  608. )
  609. privateApiRouter.post(
  610. '/internal/project/:projectId/expire-deleted-project',
  611. AuthenticationController.httpAuth,
  612. ProjectController.expireDeletedProject
  613. )
  614. privateApiRouter.post(
  615. '/internal/users/:userId/expire',
  616. AuthenticationController.httpAuth,
  617. UserController.expireDeletedUser
  618. )
  619. privateApiRouter.get(
  620. '/user/:userId/tag',
  621. AuthenticationController.httpAuth,
  622. TagsController.apiGetAllTags
  623. )
  624. webRouter.get(
  625. '/tag',
  626. AuthenticationController.requireLogin(),
  627. TagsController.getAllTags
  628. )
  629. webRouter.post(
  630. '/tag',
  631. AuthenticationController.requireLogin(),
  632. RateLimiterMiddleware.rateLimit({
  633. endpointName: 'create-tag',
  634. maxRequests: 30,
  635. timeInterval: 60,
  636. }),
  637. TagsController.createTag
  638. )
  639. webRouter.post(
  640. '/tag/:tagId/rename',
  641. AuthenticationController.requireLogin(),
  642. RateLimiterMiddleware.rateLimit({
  643. endpointName: 'rename-tag',
  644. maxRequests: 30,
  645. timeInterval: 60,
  646. }),
  647. TagsController.renameTag
  648. )
  649. webRouter.delete(
  650. '/tag/:tagId',
  651. AuthenticationController.requireLogin(),
  652. RateLimiterMiddleware.rateLimit({
  653. endpointName: 'delete-tag',
  654. maxRequests: 30,
  655. timeInterval: 60,
  656. }),
  657. TagsController.deleteTag
  658. )
  659. webRouter.post(
  660. '/tag/:tagId/project/:projectId',
  661. AuthenticationController.requireLogin(),
  662. RateLimiterMiddleware.rateLimit({
  663. endpointName: 'add-project-to-tag',
  664. maxRequests: 30,
  665. timeInterval: 60,
  666. }),
  667. TagsController.addProjectToTag
  668. )
  669. webRouter.delete(
  670. '/tag/:tagId/project/:projectId',
  671. AuthenticationController.requireLogin(),
  672. RateLimiterMiddleware.rateLimit({
  673. endpointName: 'remove-project-from-tag',
  674. maxRequests: 30,
  675. timeInterval: 60,
  676. }),
  677. TagsController.removeProjectFromTag
  678. )
  679. webRouter.get(
  680. '/notifications',
  681. AuthenticationController.requireLogin(),
  682. NotificationsController.getAllUnreadNotifications
  683. )
  684. webRouter.delete(
  685. '/notifications/:notification_id',
  686. AuthenticationController.requireLogin(),
  687. NotificationsController.markNotificationAsRead
  688. )
  689. // Deprecated in favour of /internal/project/:project_id but still used by versioning
  690. privateApiRouter.get(
  691. '/project/:project_id/details',
  692. AuthenticationController.httpAuth,
  693. ProjectApiController.getProjectDetails
  694. )
  695. // New 'stable' /internal API end points
  696. privateApiRouter.get(
  697. '/internal/project/:project_id',
  698. AuthenticationController.httpAuth,
  699. ProjectApiController.getProjectDetails
  700. )
  701. privateApiRouter.get(
  702. '/internal/project/:Project_id/zip',
  703. AuthenticationController.httpAuth,
  704. ProjectDownloadsController.downloadProject
  705. )
  706. privateApiRouter.get(
  707. '/internal/project/:project_id/compile/pdf',
  708. AuthenticationController.httpAuth,
  709. CompileController.compileAndDownloadPdf
  710. )
  711. privateApiRouter.post(
  712. '/internal/deactivateOldProjects',
  713. AuthenticationController.httpAuth,
  714. InactiveProjectController.deactivateOldProjects
  715. )
  716. privateApiRouter.post(
  717. '/internal/project/:project_id/deactivate',
  718. AuthenticationController.httpAuth,
  719. InactiveProjectController.deactivateProject
  720. )
  721. privateApiRouter.get(
  722. /^\/internal\/project\/([^/]*)\/output\/(.*)$/,
  723. function (req, res, next) {
  724. const params = {
  725. Project_id: req.params[0],
  726. file: req.params[1],
  727. }
  728. req.params = params
  729. next()
  730. },
  731. AuthenticationController.httpAuth,
  732. CompileController.getFileFromClsi
  733. )
  734. privateApiRouter.get(
  735. '/project/:Project_id/doc/:doc_id',
  736. AuthenticationController.httpAuth,
  737. DocumentController.getDocument
  738. )
  739. privateApiRouter.post(
  740. '/project/:Project_id/doc/:doc_id',
  741. AuthenticationController.httpAuth,
  742. DocumentController.setDocument
  743. )
  744. privateApiRouter.post(
  745. '/user/:user_id/update/*',
  746. AuthenticationController.httpAuth,
  747. TpdsController.mergeUpdate
  748. )
  749. privateApiRouter.delete(
  750. '/user/:user_id/update/*',
  751. AuthenticationController.httpAuth,
  752. TpdsController.deleteUpdate
  753. )
  754. privateApiRouter.post(
  755. '/project/:project_id/contents/*',
  756. AuthenticationController.httpAuth,
  757. TpdsController.updateProjectContents
  758. )
  759. privateApiRouter.delete(
  760. '/project/:project_id/contents/*',
  761. AuthenticationController.httpAuth,
  762. TpdsController.deleteProjectContents
  763. )
  764. webRouter.post(
  765. '/spelling/check',
  766. AuthenticationController.requireLogin(),
  767. SpellingController.proxyRequestToSpellingApi
  768. )
  769. webRouter.post(
  770. '/spelling/learn',
  771. AuthenticationController.requireLogin(),
  772. SpellingController.proxyRequestToSpellingApi
  773. )
  774. webRouter.get(
  775. '/project/:project_id/messages',
  776. AuthorizationMiddleware.blockRestrictedUserFromProject,
  777. AuthorizationMiddleware.ensureUserCanReadProject,
  778. ChatController.getMessages
  779. )
  780. webRouter.post(
  781. '/project/:project_id/messages',
  782. AuthorizationMiddleware.blockRestrictedUserFromProject,
  783. AuthorizationMiddleware.ensureUserCanReadProject,
  784. RateLimiterMiddleware.rateLimit({
  785. endpointName: 'send-chat-message',
  786. maxRequests: 100,
  787. timeInterval: 60,
  788. }),
  789. ChatController.sendMessage
  790. )
  791. webRouter.post(
  792. '/project/:Project_id/references/index',
  793. AuthorizationMiddleware.ensureUserCanReadProject,
  794. RateLimiterMiddleware.rateLimit({
  795. endpointName: 'index-project-references',
  796. maxRequests: 30,
  797. timeInterval: 60,
  798. }),
  799. ReferencesController.index
  800. )
  801. webRouter.post(
  802. '/project/:Project_id/references/indexAll',
  803. AuthorizationMiddleware.ensureUserCanReadProject,
  804. RateLimiterMiddleware.rateLimit({
  805. endpointName: 'index-all-project-references',
  806. maxRequests: 30,
  807. timeInterval: 60,
  808. }),
  809. ReferencesController.indexAll
  810. )
  811. // disable beta program while v2 is in beta
  812. webRouter.get(
  813. '/beta/participate',
  814. AuthenticationController.requireLogin(),
  815. BetaProgramController.optInPage
  816. )
  817. webRouter.post(
  818. '/beta/opt-in',
  819. AuthenticationController.requireLogin(),
  820. BetaProgramController.optIn
  821. )
  822. webRouter.post(
  823. '/beta/opt-out',
  824. AuthenticationController.requireLogin(),
  825. BetaProgramController.optOut
  826. )
  827. // New "api" endpoints. Started as a way for v1 to call over to v2 (for
  828. // long-term features, as opposed to the nominally temporary ones in the
  829. // overleaf-integration module), but may expand beyond that role.
  830. publicApiRouter.post(
  831. '/api/clsi/compile/:submission_id',
  832. AuthenticationController.httpAuth,
  833. CompileController.compileSubmission
  834. )
  835. publicApiRouter.get(
  836. /^\/api\/clsi\/compile\/([^/]*)\/build\/([0-9a-f-]+)\/output\/(.*)$/,
  837. function (req, res, next) {
  838. const params = {
  839. submission_id: req.params[0],
  840. build_id: req.params[1],
  841. file: req.params[2],
  842. }
  843. req.params = params
  844. next()
  845. },
  846. AuthenticationController.httpAuth,
  847. CompileController.getFileFromClsiWithoutUser
  848. )
  849. publicApiRouter.post(
  850. '/api/institutions/confirm_university_domain',
  851. RateLimiterMiddleware.rateLimit({
  852. endpointName: 'confirm-university-domain',
  853. maxRequests: 1,
  854. timeInterval: 60,
  855. }),
  856. AuthenticationController.httpAuth,
  857. InstitutionsController.confirmDomain
  858. )
  859. webRouter.get('/chrome', function (req, res, next) {
  860. // Match v1 behaviour - this is used for a Chrome web app
  861. if (AuthenticationController.isUserLoggedIn(req)) {
  862. res.redirect('/project')
  863. } else {
  864. res.redirect('/register')
  865. }
  866. })
  867. // Admin Stuff
  868. webRouter.get(
  869. '/admin',
  870. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  871. AdminController.index
  872. )
  873. webRouter.get(
  874. '/admin/user',
  875. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  876. (req, res) => res.redirect('/admin/register')
  877. ) // this gets removed by admin-panel addon
  878. webRouter.get(
  879. '/admin/register',
  880. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  881. AdminController.registerNewUser
  882. )
  883. webRouter.post(
  884. '/admin/register',
  885. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  886. UserController.register
  887. )
  888. if (!Features.hasFeature('saas')) {
  889. webRouter.post(
  890. '/admin/openEditor',
  891. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  892. AdminController.openEditor
  893. )
  894. webRouter.post(
  895. '/admin/closeEditor',
  896. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  897. AdminController.closeEditor
  898. )
  899. webRouter.post(
  900. '/admin/disconnectAllUsers',
  901. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  902. AdminController.disconnectAllUsers
  903. )
  904. }
  905. webRouter.post(
  906. '/admin/flushProjectToTpds',
  907. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  908. AdminController.flushProjectToTpds
  909. )
  910. webRouter.post(
  911. '/admin/pollDropboxForUser',
  912. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  913. AdminController.pollDropboxForUser
  914. )
  915. webRouter.post(
  916. '/admin/messages',
  917. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  918. AdminController.createMessage
  919. )
  920. webRouter.post(
  921. '/admin/messages/clear',
  922. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  923. AdminController.clearMessages
  924. )
  925. privateApiRouter.post(
  926. '/disconnectAllUsers',
  927. AdminController.disconnectAllUsers
  928. )
  929. privateApiRouter.get('/perfTest', (req, res) => res.send('hello'))
  930. publicApiRouter.get('/status', (req, res) => {
  931. if (!Settings.siteIsOpen) {
  932. res.send('web site is closed (web)')
  933. } else if (!Settings.editorIsOpen) {
  934. res.send('web editor is closed (web)')
  935. } else {
  936. res.send('web sharelatex is alive (web)')
  937. }
  938. })
  939. privateApiRouter.get('/status', (req, res) =>
  940. res.send('web sharelatex is alive (api)')
  941. )
  942. // used by kubernetes health-check and acceptance tests
  943. webRouter.get('/dev/csrf', (req, res) => res.send(res.locals.csrfToken))
  944. publicApiRouter.get(
  945. '/health_check',
  946. HealthCheckController.checkActiveHandles,
  947. HealthCheckController.check
  948. )
  949. privateApiRouter.get(
  950. '/health_check',
  951. HealthCheckController.checkActiveHandles,
  952. HealthCheckController.checkApi
  953. )
  954. publicApiRouter.get(
  955. '/health_check/full',
  956. HealthCheckController.checkActiveHandles,
  957. HealthCheckController.check
  958. )
  959. privateApiRouter.get(
  960. '/health_check/full',
  961. HealthCheckController.checkActiveHandles,
  962. HealthCheckController.check
  963. )
  964. publicApiRouter.get('/health_check/redis', HealthCheckController.checkRedis)
  965. privateApiRouter.get('/health_check/redis', HealthCheckController.checkRedis)
  966. publicApiRouter.get('/health_check/mongo', HealthCheckController.checkMongo)
  967. privateApiRouter.get('/health_check/mongo', HealthCheckController.checkMongo)
  968. webRouter.get(
  969. '/status/compiler/:Project_id',
  970. RateLimiterMiddleware.rateLimit({
  971. endpointName: 'status-compiler',
  972. maxRequests: 10,
  973. timeInterval: 60,
  974. }),
  975. AuthorizationMiddleware.ensureUserCanReadProject,
  976. function (req, res) {
  977. const projectId = req.params.Project_id
  978. const sendRes = _.once(function (statusCode, message) {
  979. res.status(statusCode)
  980. res.send(message)
  981. ClsiCookieManager.clearServerId(projectId)
  982. }) // force every compile to a new server
  983. // set a timeout
  984. var handler = setTimeout(function () {
  985. sendRes(500, 'Compiler timed out')
  986. handler = null
  987. }, 10000)
  988. // use a valid user id for testing
  989. const testUserId = '123456789012345678901234'
  990. // run the compile
  991. CompileManager.compile(
  992. projectId,
  993. testUserId,
  994. {},
  995. function (error, status) {
  996. if (handler) {
  997. clearTimeout(handler)
  998. }
  999. if (error) {
  1000. sendRes(500, `Compiler returned error ${error.message}`)
  1001. } else if (status === 'success') {
  1002. sendRes(200, 'Compiler returned in less than 10 seconds')
  1003. } else {
  1004. sendRes(500, `Compiler returned failure ${status}`)
  1005. }
  1006. }
  1007. )
  1008. }
  1009. )
  1010. webRouter.get('/no-cache', function (req, res, next) {
  1011. res.header('Cache-Control', 'max-age=0')
  1012. res.sendStatus(404)
  1013. })
  1014. webRouter.get('/oops-express', (req, res, next) =>
  1015. next(new Error('Test error'))
  1016. )
  1017. webRouter.get('/oops-internal', function (req, res, next) {
  1018. throw new Error('Test error')
  1019. })
  1020. webRouter.get('/oops-mongo', (req, res, next) =>
  1021. require('./models/Project').Project.findOne({}, function () {
  1022. throw new Error('Test error')
  1023. })
  1024. )
  1025. privateApiRouter.get('/opps-small', function (req, res, next) {
  1026. logger.err('test error occured')
  1027. res.sendStatus(200)
  1028. })
  1029. webRouter.post('/error/client', function (req, res, next) {
  1030. logger.warn(
  1031. { err: req.body.error, meta: req.body.meta },
  1032. 'client side error'
  1033. )
  1034. metrics.inc('client-side-error')
  1035. res.sendStatus(204)
  1036. })
  1037. webRouter.get(
  1038. `/read/:token(${TokenAccessController.READ_ONLY_TOKEN_PATTERN})`,
  1039. RateLimiterMiddleware.rateLimit({
  1040. endpointName: 'read-only-token',
  1041. maxRequests: 15,
  1042. timeInterval: 60,
  1043. }),
  1044. TokenAccessController.tokenAccessPage
  1045. )
  1046. webRouter.get(
  1047. `/:token(${TokenAccessController.READ_AND_WRITE_TOKEN_PATTERN})`,
  1048. RateLimiterMiddleware.rateLimit({
  1049. endpointName: 'read-and-write-token',
  1050. maxRequests: 15,
  1051. timeInterval: 60,
  1052. }),
  1053. TokenAccessController.tokenAccessPage
  1054. )
  1055. webRouter.post(
  1056. `/:token(${TokenAccessController.READ_AND_WRITE_TOKEN_PATTERN})/grant`,
  1057. RateLimiterMiddleware.rateLimit({
  1058. endpointName: 'grant-token-access-read-write',
  1059. maxRequests: 10,
  1060. timeInterval: 60,
  1061. }),
  1062. TokenAccessController.grantTokenAccessReadAndWrite
  1063. )
  1064. webRouter.post(
  1065. `/read/:token(${TokenAccessController.READ_ONLY_TOKEN_PATTERN})/grant`,
  1066. RateLimiterMiddleware.rateLimit({
  1067. endpointName: 'grant-token-access-read-only',
  1068. maxRequests: 10,
  1069. timeInterval: 60,
  1070. }),
  1071. TokenAccessController.grantTokenAccessReadOnly
  1072. )
  1073. webRouter.get('/unsupported-browser', renderUnsupportedBrowserPage)
  1074. webRouter.get('*', ErrorController.notFound)
  1075. }