router.js 34 KB

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