diff --git a/packages/server/src/routes/default.ts b/packages/server/src/routes/default.ts index 11b71884eb..3e9090366a 100644 --- a/packages/server/src/routes/default.ts +++ b/packages/server/src/routes/default.ts @@ -8,6 +8,7 @@ import { AppContext, RouteType } from '../utils/types'; import { localFileFromUrl } from '../utils/joplinUtils'; import { homeUrl, loginUrl } from '../utils/urlUtils'; import * as mime from '@joplin/lib/mime-utils'; +import { hasOwnProperty } from '@joplin/utils/object'; const publicDir = `${dirname(dirname(__dirname))}/public`; @@ -33,7 +34,7 @@ async function findLocalFile(path: string): Promise { const appFilePath = await localFileFromUrl(path); if (appFilePath) return appFilePath; - if (path in pathToFileMap) return pathToFileMap[path]; + if (hasOwnProperty(pathToFileMap, path)) return pathToFileMap[path]; // For now a bit of a hack to load FontAwesome fonts. if (path.indexOf('css/fontawesome/webfonts/fa-') === 0) return `node_modules/@fortawesome/fontawesome-free/${path.substr(16)}`; diff --git a/packages/server/src/utils/routeUtils.ts b/packages/server/src/utils/routeUtils.ts index 1025c5d7cf..a39cf39c46 100644 --- a/packages/server/src/utils/routeUtils.ts +++ b/packages/server/src/utils/routeUtils.ts @@ -7,6 +7,7 @@ import { URL } from 'url'; import { csrfCheck } from './csrf'; import { contextSessionId } from './requestUtils'; import { stripOffQueryParameters } from './urlUtils'; +import { hasOwnProperty } from '@joplin/utils/object'; const { ltrimSlashes, rtrimSlashes } = require('@joplin/lib/path-utils'); @@ -262,7 +263,7 @@ export function findMatchingRoute(path: string, routes: Routers): MatchedRoute { // Create the base path, eg. "api/files", to match it to one of the // routes. const basePath = `${namespace ? `${namespace}/` : ''}${splittedPath[0]}/${splittedPath[1]}`; - if (routes[basePath]) { + if (hasOwnProperty(routes, basePath)) { // Remove the base path from the array so that parseSubPath() can // extract the ID and link from the URL. So the array will contain // at this point: ['SOME_ID', 'content']. @@ -278,7 +279,7 @@ export function findMatchingRoute(path: string, routes: Routers): MatchedRoute { // Paths such as "/users/:id" or "/apps/joplin/notes/:id" will get here const basePath = splittedPath[0]; const basePathNS = (namespace ? `${namespace}/` : '') + basePath; - if (routes[basePathNS]) { + if (hasOwnProperty(routes, basePathNS)) { splittedPath.splice(0, 1); return { route: routes[basePathNS], diff --git a/packages/utils/object.ts b/packages/utils/object.ts index b3b0445a4f..995d478df2 100644 --- a/packages/utils/object.ts +++ b/packages/utils/object.ts @@ -15,3 +15,7 @@ export function checkObjectHasProperties(object: any, properties: string[]) { if (!(prop in object)) throw new Error(`Missing property "${prop}": ${JSON.stringify(object)}`); } } + +export const hasOwnProperty = (object: object, property: string): boolean => { + return !!object && Object.prototype.hasOwnProperty.call(object, property); +};