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

#853 - Content Security Policy - "unsafe-eval" #4749

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/ketcher-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
},
"dependencies": {
"@babel/runtime": "^7.17.9",
"ajv": "^8.10.0",
"assert": "^2.0.0",
"d3": "^7.8.5",
"file-saver": "^2.0.5",
Expand Down Expand Up @@ -74,6 +73,7 @@
"eslint-plugin-jest": "^25.3.0",
"jest": "26.6.0",
"jest-mock-extended": "^2.0.4",
"jsonschema": "^1.4.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.1",
"rollup": "^2.60.2",
Expand Down
7 changes: 3 additions & 4 deletions packages/ketcher-core/src/domain/serializers/ket/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
* limitations under the License.
***************************************************************************/

import Ajv from 'ajv';
import { Validator, Schema } from 'jsonschema';
import schema from './schema.json';

export function validate(ket: any): boolean {
const ajv = new Ajv();
const validate = ajv.compile(schema);
return validate(ket);
const validator = new Validator();
return validator.validate(ket, schema as unknown as Schema).valid;
}
2 changes: 1 addition & 1 deletion packages/ketcher-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@emotion/react": "^11.7.1",
"@emotion/styled": "^11.6.0",
"@mui/material": "^5.2.4",
"ajv": "^8.10.0",
"cfb": "^1.2.2",
"clsx": "^1.1.1",
"draft-js": "^0.11.7",
Expand Down Expand Up @@ -119,6 +118,7 @@
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-testing-library": "^5.0.1",
"jest": "^27.4.5",
"jsonschema": "^1.4.1",
"less": "^3.12.2",
"npm-run-all": "^4.1.5",
"postcss": "^8.2.10",
Expand Down
26 changes: 13 additions & 13 deletions packages/ketcher-react/src/script/ui/component/form/form/form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import { Component, useCallback, useState } from 'react';

import Ajv from 'ajv';
import { ErrorPopover } from './errorPopover';
import { FormContext } from '../../../../../contexts';
import Input from '../Input/Input';
Expand All @@ -29,6 +28,7 @@ import { updateFormState } from '../../../state/modal/form';
import { useFormContext } from '../../../../../hooks';
import { cloneDeep } from 'lodash';
import { IconButton } from 'components';
import { Validator } from 'jsonschema';

class Form extends Component {
constructor(props) {
Expand Down Expand Up @@ -336,12 +336,12 @@ const SelectOneOf = (props) => {
//

function propSchema(schema, { customValid, serialize = {}, deserialize = {} }) {
const ajv = new Ajv({ allErrors: true, verbose: true, strictSchema: false });
const schemaCopy = cloneDeep(schema);

Validator.prototype.customFormats = {};
if (customValid) {
Object.entries(customValid).forEach(([formatName, formatValidator]) => {
ajv.addFormat(formatName, formatValidator);
Validator.prototype.customFormats[formatName] = formatValidator;
const {
/* eslint-disable @typescript-eslint/no-unused-vars */
pattern,
Expand All @@ -358,20 +358,20 @@ function propSchema(schema, { customValid, serialize = {}, deserialize = {} }) {
});
}

const validate = ajv.compile(schemaCopy);
const validator = new Validator();

return {
key: schema.key || '',
serialize: (inst) => {
validate(inst);
const result = validator.validate(inst, schemaCopy);
return {
instance: serializeRewrite(serialize, inst, schemaCopy),
valid: validate(inst),
errors: validate.errors || [],
valid: result.valid,
errors: result.errors,
};
},
deserialize: (inst) => {
validate(inst);
validator.validate(inst, schemaCopy);
return deserializeRewrite(deserialize, inst);
},
};
Expand All @@ -395,18 +395,18 @@ function deserializeRewrite(deserializeMap, instance) {
}

function getInvalidMessage(item) {
if (!item.parentSchema.invalidMessage) return item.message;
return typeof item.parentSchema.invalidMessage === 'function'
? item.parentSchema.invalidMessage(item.data)
: item.parentSchema.invalidMessage;
if (!item.schema.invalidMessage) return item.message;
return typeof item.schema.invalidMessage === 'function'
? item.schema.invalidMessage(item.data)
: item.schema.invalidMessage;
}

function getErrorsObj(errors) {
const errs = {};
let field;

errors.forEach((item) => {
field = item.instancePath.slice(1);
field = item.path[item.path.length - 1];
if (!errs[field]) errs[field] = getInvalidMessage(item);
});

Expand Down
18 changes: 6 additions & 12 deletions packages/ketcher-react/src/script/ui/data/schema/options-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
* limitations under the License.
***************************************************************************/

import { Schema, Validator } from 'jsonschema';
import {
StereLabelStyleType,
StereoColoringType,
ShowHydrogenLabels,
ShowHydrogenLabelNames,
defaultBondThickness,
} from 'ketcher-core';
import Ajv, { SchemaObject } from 'ajv';

type ExtendedSchema = SchemaObject & {
type ExtendedSchema = Schema & {
enumNames?: Array<string>;
default?: any;
};
Expand Down Expand Up @@ -361,23 +361,17 @@ export function getDefaultOptions(): Record<string, any> {
if (!optionsSchema.properties) return {};

return Object.keys(optionsSchema.properties).reduce((res, prop) => {
res[prop] = (optionsSchema.properties[prop] as ExtendedSchema).default;
res[prop] = (optionsSchema.properties?.[prop] as ExtendedSchema)?.default;
return res;
}, {});
}

export function validation(settings): Record<string, string> | null {
if (typeof settings !== 'object' || settings === null) return null;

const ajv = new Ajv({
allErrors: true,
keywords: [{ keyword: 'enumNames', schemaType: 'array' }],
});

const validate = ajv.compile(optionsSchema);
validate(settings);
const errors = validate.errors || [];
const errorsProps = errors.map((el) => el.instancePath.slice(1));
const validator = new Validator();
const result = validator.validate(settings, optionsSchema);
const errorsProps = result.errors.map((el) => el.path[el.path.length - 1]);

return Object.keys(settings).reduce((res, prop) => {
if (!optionsSchema.properties) return res;
Expand Down