Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): reject routeBasePath: '' #3377

Merged
merged 4 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-client-redirects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@docusaurus/core": "^2.0.0-alpha.62",
"@docusaurus/types": "^2.0.0-alpha.62",
"@docusaurus/utils": "^2.0.0-alpha.62",
"@docusaurus/utils-validation": "^2.0.0-alpha.62",
"@hapi/joi": "^17.1.1",
"@types/hapi__joi": "^17.1.2",
"chalk": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import {PluginOptions, RedirectOption, UserPluginOptions} from './types';
import * as Joi from '@hapi/joi';
import {PathnameValidator} from './redirectValidation';
import {PathnameSchema} from '@docusaurus/utils-validation';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';

export const DefaultPluginOptions: PluginOptions = {
Expand All @@ -18,10 +18,10 @@ export const DefaultPluginOptions: PluginOptions = {
};

const RedirectPluginOptionValidation = Joi.object<RedirectOption>({
to: PathnameValidator.required(),
to: PathnameSchema.required(),
from: Joi.alternatives().try(
PathnameValidator.required(),
Joi.array().items(PathnameValidator.required()),
PathnameSchema.required(),
Joi.array().items(PathnameSchema.required()),
),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,12 @@
*/

import * as Joi from '@hapi/joi';
import {isValidPathname} from '@docusaurus/utils';
import {RedirectMetadata} from './types';

export const PathnameValidator = Joi.string()
.custom((val) => {
if (!isValidPathname(val)) {
throw new Error();
} else {
return val;
}
})
.message(
'{{#label}} is not a valid pathname. Pathname should start with / and not contain any domain or query string',
);
import {PathnameSchema} from '@docusaurus/utils-validation';

const RedirectSchema = Joi.object<RedirectMetadata>({
from: PathnameValidator.required(),
to: PathnameValidator.required(),
from: PathnameSchema.required(),
to: PathnameSchema.required(),
});

export function validateRedirect(redirect: RedirectMetadata): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('should accept correctly defined user options', () => {
...DEFAULT_OPTIONS,
feedOptions: {type: 'rss', title: 'myTitle'},
path: 'not_blog',
routeBasePath: '',
routeBasePath: 'myBlog',
postsPerPage: 5,
include: ['api/*', 'docs/*'],
};
Expand All @@ -37,7 +37,7 @@ test('should accept correctly defined user options', () => {
test('should accept valid user options', async () => {
const userOptions = {
...DEFAULT_OPTIONS,
routeBasePath: '',
routeBasePath: 'myBlog',
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export const DEFAULT_OPTIONS = {

export const PluginOptionSchema = Joi.object({
path: Joi.string().default(DEFAULT_OPTIONS.path),
routeBasePath: Joi.string().allow('').default(DEFAULT_OPTIONS.routeBasePath),
routeBasePath: Joi.string()
// '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
// .allow('')
.default(DEFAULT_OPTIONS.routeBasePath),
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
postsPerPage: Joi.number()
.integer()
Expand Down
5 changes: 4 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ const VersionsOptionsSchema = Joi.object()
export const OptionsSchema = Joi.object({
path: Joi.string().default(DEFAULT_OPTIONS.path),
editUrl: URISchema,
routeBasePath: Joi.string().allow('').default(DEFAULT_OPTIONS.routeBasePath),
routeBasePath: Joi.string()
// '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
// .allow('') ""
.default(DEFAULT_OPTIONS.routeBasePath),
homePageId: Joi.string().optional(),
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
sidebarPath: Joi.string().allow('').default(DEFAULT_OPTIONS.sidebarPath),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

export const DEFAULT_OPTIONS: PluginOptions = {
path: 'src/pages', // Path to data on filesystem, relative to site dir.
routeBasePath: '', // URL Route.
routeBasePath: '/', // URL Route.
include: ['**/*.{js,jsx,ts,tsx,md,mdx}'], // Extensions to include.
mdxPageComponent: '@theme/MDXPage',
remarkPlugins: [],
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-utils-validation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@types/hapi__joi": "^17.1.2"
},
"dependencies": {
"@docusaurus/utils": "^2.0.0-alpha.62",
"@hapi/joi": "17.1.1",
"chalk": "^3.0.0"
},
Expand Down
13 changes: 13 additions & 0 deletions packages/docusaurus-utils-validation/src/validationSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import * as Joi from '@hapi/joi';
import {isValidPathname} from '@docusaurus/utils';

export const PluginIdSchema = Joi.string()
.regex(/^[a-zA-Z_\-]+$/)
Expand Down Expand Up @@ -39,3 +40,15 @@ export const URISchema = Joi.alternatives(
}
}),
);

export const PathnameSchema = Joi.string()
.custom((val) => {
if (!isValidPathname(val)) {
throw new Error();
} else {
return val;
}
})
.message(
'{{#label}} is not a valid pathname. Pathname should start with / and not contain any domain or query string',
);