router.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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 UserOnboardingController = require('./Features/User/UserOnboardingController')
  22. const DocumentController = require('./Features/Documents/DocumentController')
  23. const CompileManager = require('./Features/Compile/CompileManager')
  24. const CompileController = require('./Features/Compile/CompileController')
  25. const ClsiCookieManager = require('./Features/Compile/ClsiCookieManager')(
  26. Settings.apis.clsi != null ? Settings.apis.clsi.backendGroupName : undefined
  27. )
  28. const HealthCheckController = require('./Features/HealthCheck/HealthCheckController')
  29. const ProjectDownloadsController = require('./Features/Downloads/ProjectDownloadsController')
  30. const FileStoreController = require('./Features/FileStore/FileStoreController')
  31. const HistoryController = require('./Features/History/HistoryController')
  32. const ExportsController = require('./Features/Exports/ExportsController')
  33. const PasswordResetRouter = require('./Features/PasswordReset/PasswordResetRouter')
  34. const StaticPagesRouter = require('./Features/StaticPages/StaticPagesRouter')
  35. const ChatController = require('./Features/Chat/ChatController')
  36. const Modules = require('./infrastructure/Modules')
  37. const RateLimiterMiddleware = require('./Features/Security/RateLimiterMiddleware')
  38. const InactiveProjectController = require('./Features/InactiveData/InactiveProjectController')
  39. const ContactRouter = require('./Features/Contacts/ContactRouter')
  40. const ReferencesController = require('./Features/References/ReferencesController')
  41. const AuthorizationMiddleware = require('./Features/Authorization/AuthorizationMiddleware')
  42. const BetaProgramController = require('./Features/BetaProgram/BetaProgramController')
  43. const AnalyticsRouter = require('./Features/Analytics/AnalyticsRouter')
  44. const MetaController = require('./Features/Metadata/MetaController')
  45. const TokenAccessController = require('./Features/TokenAccess/TokenAccessController')
  46. const Features = require('./infrastructure/Features')
  47. const LinkedFilesRouter = require('./Features/LinkedFiles/LinkedFilesRouter')
  48. const TemplatesRouter = require('./Features/Templates/TemplatesRouter')
  49. const InstitutionsController = require('./Features/Institutions/InstitutionsController')
  50. const UserMembershipRouter = require('./Features/UserMembership/UserMembershipRouter')
  51. const SystemMessageController = require('./Features/SystemMessages/SystemMessageController')
  52. const { Joi, validate } = require('./infrastructure/Validation')
  53. const logger = require('logger-sharelatex')
  54. const _ = require('underscore')
  55. module.exports = { initialize }
  56. function initialize(webRouter, privateApiRouter, publicApiRouter) {
  57. if (!Settings.allowPublicAccess) {
  58. webRouter.all('*', AuthenticationController.requireGlobalLogin)
  59. }
  60. webRouter.get('/login', UserPagesController.loginPage)
  61. AuthenticationController.addEndpointToLoginWhitelist('/login')
  62. webRouter.post('/login', AuthenticationController.passportLogin)
  63. webRouter.get(
  64. '/read-only/one-time-login',
  65. UserPagesController.oneTimeLoginPage
  66. )
  67. AuthenticationController.addEndpointToLoginWhitelist(
  68. '/read-only/one-time-login'
  69. )
  70. webRouter.get('/logout', UserPagesController.logoutPage)
  71. webRouter.post('/logout', UserController.logout)
  72. webRouter.get('/restricted', AuthorizationMiddleware.restricted)
  73. if (Features.hasFeature('registration')) {
  74. webRouter.get('/register', UserPagesController.registerPage)
  75. AuthenticationController.addEndpointToLoginWhitelist('/register')
  76. }
  77. EditorRouter.apply(webRouter, privateApiRouter)
  78. CollaboratorsRouter.apply(webRouter, privateApiRouter)
  79. SubscriptionRouter.apply(webRouter, privateApiRouter, publicApiRouter)
  80. UploadsRouter.apply(webRouter, privateApiRouter)
  81. PasswordResetRouter.apply(webRouter, privateApiRouter)
  82. StaticPagesRouter.apply(webRouter, privateApiRouter)
  83. ContactRouter.apply(webRouter, privateApiRouter)
  84. AnalyticsRouter.apply(webRouter, privateApiRouter, publicApiRouter)
  85. LinkedFilesRouter.apply(webRouter, privateApiRouter, publicApiRouter)
  86. TemplatesRouter.apply(webRouter)
  87. UserMembershipRouter.apply(webRouter)
  88. Modules.applyRouter(webRouter, privateApiRouter, publicApiRouter)
  89. if (Settings.enableSubscriptions) {
  90. webRouter.get(
  91. '/user/bonus',
  92. AuthenticationController.requireLogin(),
  93. ReferalController.bonus
  94. )
  95. }
  96. // .getMessages will generate an empty response for anonymous users.
  97. webRouter.get('/system/messages', SystemMessageController.getMessages)
  98. webRouter.get(
  99. '/user/settings',
  100. AuthenticationController.requireLogin(),
  101. UserPagesController.settingsPage
  102. )
  103. webRouter.post(
  104. '/user/settings',
  105. AuthenticationController.requireLogin(),
  106. UserController.updateUserSettings
  107. )
  108. webRouter.post(
  109. '/user/password/update',
  110. AuthenticationController.requireLogin(),
  111. RateLimiterMiddleware.rateLimit({
  112. endpointName: 'change-password',
  113. maxRequests: 10,
  114. timeInterval: 60
  115. }),
  116. UserController.changePassword
  117. )
  118. webRouter.get(
  119. '/user/emails',
  120. AuthenticationController.requireLogin(),
  121. UserController.promises.ensureAffiliationMiddleware,
  122. UserEmailsController.list
  123. )
  124. webRouter.get('/user/emails/confirm', UserEmailsController.showConfirm)
  125. webRouter.post(
  126. '/user/emails/confirm',
  127. RateLimiterMiddleware.rateLimit({
  128. endpointName: 'confirm-email',
  129. maxRequests: 10,
  130. timeInterval: 60
  131. }),
  132. UserEmailsController.confirm
  133. )
  134. webRouter.post(
  135. '/user/emails/resend_confirmation',
  136. AuthenticationController.requireLogin(),
  137. RateLimiterMiddleware.rateLimit({
  138. endpointName: 'resend-confirmation',
  139. maxRequests: 10,
  140. timeInterval: 60
  141. }),
  142. UserEmailsController.resendConfirmation
  143. )
  144. if (Features.hasFeature('affiliations')) {
  145. webRouter.post(
  146. '/user/emails',
  147. AuthenticationController.requireLogin(),
  148. RateLimiterMiddleware.rateLimit({
  149. endpointName: 'add-email',
  150. maxRequests: 10,
  151. timeInterval: 60
  152. }),
  153. UserEmailsController.add
  154. )
  155. webRouter.post(
  156. '/user/emails/delete',
  157. AuthenticationController.requireLogin(),
  158. RateLimiterMiddleware.rateLimit({
  159. endpointName: 'delete-email',
  160. maxRequests: 10,
  161. timeInterval: 60
  162. }),
  163. UserEmailsController.remove
  164. )
  165. webRouter.post(
  166. '/user/emails/default',
  167. AuthenticationController.requireLogin(),
  168. UserEmailsController.setDefault
  169. )
  170. webRouter.post(
  171. '/user/emails/endorse',
  172. AuthenticationController.requireLogin(),
  173. RateLimiterMiddleware.rateLimit({
  174. endpointName: 'endorse-email',
  175. maxRequests: 30,
  176. timeInterval: 60
  177. }),
  178. UserEmailsController.endorse
  179. )
  180. }
  181. webRouter.get(
  182. '/user/sessions',
  183. AuthenticationController.requireLogin(),
  184. UserPagesController.sessionsPage
  185. )
  186. webRouter.post(
  187. '/user/sessions/clear',
  188. AuthenticationController.requireLogin(),
  189. UserController.clearSessions
  190. )
  191. webRouter.delete(
  192. '/user/newsletter/unsubscribe',
  193. AuthenticationController.requireLogin(),
  194. UserController.unsubscribe
  195. )
  196. webRouter.post(
  197. '/user/delete',
  198. RateLimiterMiddleware.rateLimit({
  199. endpointName: 'delete-user',
  200. maxRequests: 10,
  201. timeInterval: 60
  202. }),
  203. AuthenticationController.requireLogin(),
  204. UserController.tryDeleteUser
  205. )
  206. webRouter.get(
  207. '/user/personal_info',
  208. AuthenticationController.requireLogin(),
  209. UserInfoController.getLoggedInUsersPersonalInfo
  210. )
  211. privateApiRouter.get(
  212. '/user/:user_id/personal_info',
  213. AuthenticationController.httpAuth,
  214. UserInfoController.getPersonalInfo
  215. )
  216. privateApiRouter.post(
  217. '/user/onboarding_emails',
  218. AuthenticationController.httpAuth,
  219. UserOnboardingController.sendRecentSignupOnboardingEmails
  220. )
  221. webRouter.get(
  222. '/user/reconfirm',
  223. UserPagesController.renderReconfirmAccountPage
  224. )
  225. // for /user/reconfirm POST, see password router
  226. webRouter.get(
  227. '/user/tpds/queues',
  228. AuthenticationController.requireLogin(),
  229. TpdsController.getQueues
  230. )
  231. webRouter.get(
  232. '/user/projects',
  233. AuthenticationController.requireLogin(),
  234. ProjectController.userProjectsJson
  235. )
  236. webRouter.get(
  237. '/project/:Project_id/entities',
  238. AuthenticationController.requireLogin(),
  239. AuthorizationMiddleware.ensureUserCanReadProject,
  240. ProjectController.projectEntitiesJson
  241. )
  242. webRouter.get(
  243. '/project',
  244. AuthenticationController.requireLogin(),
  245. RateLimiterMiddleware.rateLimit({
  246. endpointName: 'open-dashboard',
  247. maxRequests: 30,
  248. timeInterval: 60
  249. }),
  250. ProjectController.projectListPage
  251. )
  252. webRouter.post(
  253. '/project/new',
  254. AuthenticationController.requireLogin(),
  255. RateLimiterMiddleware.rateLimit({
  256. endpointName: 'create-project',
  257. maxRequests: 20,
  258. timeInterval: 60
  259. }),
  260. ProjectController.newProject
  261. )
  262. webRouter.get(
  263. '/Project/:Project_id',
  264. RateLimiterMiddleware.rateLimit({
  265. endpointName: 'open-project',
  266. params: ['Project_id'],
  267. maxRequests: 15,
  268. timeInterval: 60
  269. }),
  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. webRouter.post(
  889. '/admin/closeEditor',
  890. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  891. AdminController.closeEditor
  892. )
  893. webRouter.post(
  894. '/admin/dissconectAllUsers',
  895. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  896. AdminController.dissconectAllUsers
  897. )
  898. webRouter.post(
  899. '/admin/flushProjectToTpds',
  900. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  901. AdminController.flushProjectToTpds
  902. )
  903. webRouter.post(
  904. '/admin/pollDropboxForUser',
  905. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  906. AdminController.pollDropboxForUser
  907. )
  908. webRouter.post(
  909. '/admin/messages',
  910. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  911. AdminController.createMessage
  912. )
  913. webRouter.post(
  914. '/admin/messages/clear',
  915. AuthorizationMiddleware.ensureUserIsSiteAdmin,
  916. AdminController.clearMessages
  917. )
  918. privateApiRouter.post(
  919. '/disconnectAllUsers',
  920. AdminController.dissconectAllUsers
  921. )
  922. privateApiRouter.get('/perfTest', (req, res) => res.send('hello'))
  923. publicApiRouter.get('/status', (req, res) =>
  924. res.send('web sharelatex is alive (web)')
  925. )
  926. privateApiRouter.get('/status', (req, res) =>
  927. res.send('web sharelatex is alive (api)')
  928. )
  929. // used by kubernetes health-check and acceptance tests
  930. webRouter.get('/dev/csrf', (req, res) => res.send(res.locals.csrfToken))
  931. publicApiRouter.get(
  932. '/health_check',
  933. HealthCheckController.checkActiveHandles,
  934. HealthCheckController.check
  935. )
  936. privateApiRouter.get(
  937. '/health_check',
  938. HealthCheckController.checkActiveHandles,
  939. HealthCheckController.checkApi
  940. )
  941. publicApiRouter.get(
  942. '/health_check/full',
  943. HealthCheckController.checkActiveHandles,
  944. HealthCheckController.check
  945. )
  946. privateApiRouter.get(
  947. '/health_check/full',
  948. HealthCheckController.checkActiveHandles,
  949. HealthCheckController.check
  950. )
  951. publicApiRouter.get('/health_check/redis', HealthCheckController.checkRedis)
  952. privateApiRouter.get('/health_check/redis', HealthCheckController.checkRedis)
  953. publicApiRouter.get('/health_check/mongo', HealthCheckController.checkMongo)
  954. privateApiRouter.get('/health_check/mongo', HealthCheckController.checkMongo)
  955. webRouter.get(
  956. '/status/compiler/:Project_id',
  957. RateLimiterMiddleware.rateLimit({
  958. endpointName: 'status-compiler',
  959. maxRequests: 10,
  960. timeInterval: 60
  961. }),
  962. AuthorizationMiddleware.ensureUserCanReadProject,
  963. function(req, res) {
  964. const projectId = req.params.Project_id
  965. const sendRes = _.once(function(statusCode, message) {
  966. res.status(statusCode)
  967. res.send(message)
  968. ClsiCookieManager.clearServerId(projectId)
  969. }) // force every compile to a new server
  970. // set a timeout
  971. var handler = setTimeout(function() {
  972. sendRes(500, 'Compiler timed out')
  973. handler = null
  974. }, 10000)
  975. // use a valid user id for testing
  976. const testUserId = '123456789012345678901234'
  977. // run the compile
  978. CompileManager.compile(projectId, testUserId, {}, function(
  979. error,
  980. status
  981. ) {
  982. if (handler) {
  983. clearTimeout(handler)
  984. }
  985. if (error) {
  986. sendRes(500, `Compiler returned error ${error.message}`)
  987. } else if (status === 'success') {
  988. sendRes(200, 'Compiler returned in less than 10 seconds')
  989. } else {
  990. sendRes(500, `Compiler returned failure ${status}`)
  991. }
  992. })
  993. }
  994. )
  995. webRouter.get('/no-cache', function(req, res, next) {
  996. res.header('Cache-Control', 'max-age=0')
  997. res.sendStatus(404)
  998. })
  999. webRouter.get('/oops-express', (req, res, next) =>
  1000. next(new Error('Test error'))
  1001. )
  1002. webRouter.get('/oops-internal', function(req, res, next) {
  1003. throw new Error('Test error')
  1004. })
  1005. webRouter.get('/oops-mongo', (req, res, next) =>
  1006. require('./models/Project').Project.findOne({}, function() {
  1007. throw new Error('Test error')
  1008. })
  1009. )
  1010. privateApiRouter.get('/opps-small', function(req, res, next) {
  1011. logger.err('test error occured')
  1012. res.sendStatus(200)
  1013. })
  1014. webRouter.post('/error/client', function(req, res, next) {
  1015. logger.warn(
  1016. { err: req.body.error, meta: req.body.meta },
  1017. 'client side error'
  1018. )
  1019. metrics.inc('client-side-error')
  1020. res.sendStatus(204)
  1021. })
  1022. webRouter.get(
  1023. `/read/:token(${TokenAccessController.READ_ONLY_TOKEN_PATTERN})`,
  1024. RateLimiterMiddleware.rateLimit({
  1025. endpointName: 'read-only-token',
  1026. maxRequests: 15,
  1027. timeInterval: 60
  1028. }),
  1029. TokenAccessController.tokenAccessPage
  1030. )
  1031. webRouter.get(
  1032. `/:token(${TokenAccessController.READ_AND_WRITE_TOKEN_PATTERN})`,
  1033. RateLimiterMiddleware.rateLimit({
  1034. endpointName: 'read-and-write-token',
  1035. maxRequests: 15,
  1036. timeInterval: 60
  1037. }),
  1038. TokenAccessController.tokenAccessPage
  1039. )
  1040. webRouter.post(
  1041. `/:token(${TokenAccessController.READ_AND_WRITE_TOKEN_PATTERN})/grant`,
  1042. RateLimiterMiddleware.rateLimit({
  1043. endpointName: 'grant-token-access-read-write',
  1044. maxRequests: 10,
  1045. timeInterval: 60
  1046. }),
  1047. TokenAccessController.grantTokenAccessReadAndWrite
  1048. )
  1049. webRouter.post(
  1050. `/read/:token(${TokenAccessController.READ_ONLY_TOKEN_PATTERN})/grant`,
  1051. RateLimiterMiddleware.rateLimit({
  1052. endpointName: 'grant-token-access-read-only',
  1053. maxRequests: 10,
  1054. timeInterval: 60
  1055. }),
  1056. TokenAccessController.grantTokenAccessReadOnly
  1057. )
  1058. webRouter.get('*', ErrorController.notFound)
  1059. }