|
|
@@ -1,54 +1,71 @@
|
|
|
+import type { IAttachment } from '@growi/core';
|
|
|
+import type { NextFunction, Request, Response } from 'express';
|
|
|
+
|
|
|
+import type { IShareLink } from '~/interfaces/share-link';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
-const url = require('url');
|
|
|
+import { getModelSafely } from '../../util/mongoose-utils';
|
|
|
+
|
|
|
|
|
|
const logger = loggerFactory('growi:middleware:certify-shared-fire');
|
|
|
|
|
|
-export const generateCertifySharedFileMiddleware = (crowi) => {
|
|
|
|
|
|
- return async(req, res, next) => {
|
|
|
- const { referer } = req.headers;
|
|
|
+interface RequestToAllowShareLink extends Request {
|
|
|
+ isSharedPage?: boolean,
|
|
|
+}
|
|
|
|
|
|
- // Attachments cannot be viewed by clients who do not send referer.
|
|
|
- // https://github.com/weseek/growi/issues/2819
|
|
|
- if (referer == null) {
|
|
|
- return next();
|
|
|
- }
|
|
|
+export const certifySharedFileMiddleware = async(req: RequestToAllowShareLink, res: Response, next: NextFunction): Promise<void> => {
|
|
|
|
|
|
- const { path } = url.parse(referer);
|
|
|
+ const { referer } = req.headers;
|
|
|
|
|
|
- if (!path.startsWith('/share/')) {
|
|
|
- return next();
|
|
|
- }
|
|
|
+ // Attachments cannot be viewed by clients who do not send referer.
|
|
|
+ // https://github.com/weseek/growi/issues/2819
|
|
|
+ if (referer == null) {
|
|
|
+ return next();
|
|
|
+ }
|
|
|
|
|
|
- const fileId = req.params.id || null;
|
|
|
+ const refererUrl = new URL(referer);
|
|
|
|
|
|
- const Attachment = crowi.model('Attachment');
|
|
|
- const ShareLink = crowi.model('ShareLink');
|
|
|
+ if (!refererUrl.pathname.startsWith('/share/')) {
|
|
|
+ return next();
|
|
|
+ }
|
|
|
|
|
|
- const attachment = await Attachment.findOne({ _id: fileId });
|
|
|
+ const fileId = req.params.id || null;
|
|
|
|
|
|
- if (attachment == null) {
|
|
|
- return next();
|
|
|
- }
|
|
|
+ const Attachment = getModelSafely<IAttachment>('Attachment');
|
|
|
+ const ShareLink = getModelSafely<IShareLink>('ShareLink');
|
|
|
+
|
|
|
+ if (Attachment == null) {
|
|
|
+ logger.warn('Could not get Attachment model. next() is called without processing anything.');
|
|
|
+ return next();
|
|
|
+ }
|
|
|
+ if (ShareLink == null) {
|
|
|
+ logger.warn('Could not get Attachment model. next() is called without processing anything.');
|
|
|
+ return next();
|
|
|
+ }
|
|
|
+
|
|
|
+ const attachment = await Attachment.findOne({ _id: fileId });
|
|
|
+
|
|
|
+ if (attachment == null) {
|
|
|
+ return next();
|
|
|
+ }
|
|
|
+
|
|
|
+ const shareLinks = await ShareLink.find({ relatedPage: attachment.page });
|
|
|
|
|
|
- const shareLinks = await ShareLink.find({ relatedPage: attachment.page });
|
|
|
+ // If sharelinks don't exist, skip it
|
|
|
+ if (shareLinks.length === 0) {
|
|
|
+ return next();
|
|
|
+ }
|
|
|
|
|
|
- // If sharelinks don't exist, skip it
|
|
|
- if (shareLinks.length === 0) {
|
|
|
- return next();
|
|
|
+ // Is there a valid share link
|
|
|
+ shareLinks.map((sharelink) => {
|
|
|
+ if (!sharelink.isExpired()) {
|
|
|
+ logger.debug('Confirmed target file belong to a share page');
|
|
|
+ req.isSharedPage = true;
|
|
|
}
|
|
|
+ return;
|
|
|
+ });
|
|
|
|
|
|
- // Is there a valid share link
|
|
|
- shareLinks.map((sharelink) => {
|
|
|
- if (!sharelink.isExpired()) {
|
|
|
- logger.debug('Confirmed target file belong to a share page');
|
|
|
- req.isSharedPage = true;
|
|
|
- }
|
|
|
- return;
|
|
|
- });
|
|
|
-
|
|
|
- next();
|
|
|
- };
|
|
|
+ next();
|
|
|
|
|
|
};
|