Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Commit

Permalink
BugFix: Validate ObjectId
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-takei committed Oct 10, 2020
1 parent 770fc02 commit cbc2565
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/server/routes/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ const logger = loggerFactory('growi-plugin:attachment-refs:routes:refs');

module.exports = (crowi) => {
const express = crowi.require('express');
const mongoose = crowi.require('mongoose');
const router = express.Router();

const ObjectId = mongoose.Types.ObjectId;

const User = crowi.model('User');
const Page = crowi.model('Page');
const Attachment = crowi.model('Attachment');
Expand Down Expand Up @@ -90,13 +93,16 @@ module.exports = (crowi) => {
creatorPopulateOpt = User.IMAGE_POPULATION;
}

// convert ObjectId
const orConditions = [{ originalName: fileNameOrId }];
if (ObjectId.isValid(fileNameOrId)) {
orConditions.push({ _id: ObjectId(fileNameOrId) });
}

const attachment = await Attachment
.findOne({
page: page._id,
$or: [
{ _id: fileNameOrId },
{ originalName: fileNameOrId },
],
$or: orConditions,
})
.populate({ path: 'creator', select: User.USER_PUBLIC_FIELDS, populate: creatorPopulateOpt });

Expand Down

0 comments on commit cbc2565

Please sign in to comment.