Skip to content

Audit safe exec #380

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

Merged
merged 6 commits into from
Mar 10, 2025
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
3 changes: 2 additions & 1 deletion web/apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
"uuid": "^10.0.0",
"zod": "^3.23.8",
"zustand": "^4.5.4",
"zustand-computed-state": "^0.1.8"
"zustand-computed-state": "^0.1.8",
"isomorphic-dompurify": "^2.14.0"
},
"devDependencies": {
"@shellagent/eslint-config": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { $getNodeByKey, COMMAND_PRIORITY_EDITOR } from 'lexical';
import { memo, useRef, useEffect } from 'react';
import { sanitize } from 'isomorphic-dompurify';

import { cn } from '@/utils/cn';

Expand Down Expand Up @@ -125,21 +126,23 @@ const VariableValueBlockComponent = ({
};

const renderParts = () => {
return parts
.map(part => {
if (part.type === PartType.TEXT) {
return part.content;
} else if (part.type === PartType.VARIABLE) {
return `<span
return sanitize(
parts
.map(part => {
if (part.type === PartType.TEXT) {
return part.content;
} else if (part.type === PartType.VARIABLE) {
return `<span
data-origin-value="${part.content}"
data-display="${part.display}"
contenteditable="false"
class="inline-block text-blue-700 cursor-text px-0.5"
>${part.display}</span>`;
}
return '';
})
.join('');
}
return '';
})
.join(''),
);
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { observer } from 'mobx-react-lite';
import React, { useEffect, useRef } from 'react';
import { Box, Flex } from 'react-system';
import { sanitize } from 'isomorphic-dompurify';

import {
DEFAULT_MODAL_STYLES,
Expand Down Expand Up @@ -226,7 +227,9 @@ export const ComfyUIEditorModal = observer(() => {
]}>
<div
dangerouslySetInnerHTML={{
__html: model.messageDetail?.replaceAll('\n', '<br />') || '',
__html: sanitize(
model.messageDetail?.replaceAll('\n', '<br />') || '',
),
}}
/>
</Modal>
Expand Down
22 changes: 19 additions & 3 deletions web/apps/web/src/services/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,27 @@ export const customFetch = async <T>(
{ toastId: 'login-error' },
);
setTimeout(() => {
const currentUrl = window.location.href;
const isValidRedirectUrl = (() => {
try {
const url = new URL(currentUrl);
return (
url.hostname.endsWith('myshell.fun') ||
url.hostname.endsWith('myshell.ai') ||
url.hostname.endsWith('myshell.life')
);
} catch (e) {
return false;
}
})();

const redirectUrl = isValidRedirectUrl
? currentUrl
: 'https://myshell.ai';

window.location.href = `${
process.env.NEXT_PUBLIC_LOGIN_URL
}?login=true&redirect=${decodeURIComponent(
window.location.href,
)}`;
}?login=true&redirect=${encodeURIComponent(redirectUrl)}`;
}, 3000);
}

Expand Down
1 change: 1 addition & 0 deletions web/packages/form-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"react-dnd": "^16.0.1",
"react-dnd-html5-backend": "^16.0.1",
"react-error-boundary": "^4.0.13",
"sval": "^0.6.1",
"tailwind-merge": "^2.5.2",
"zod": "^3.23.8",
"zustand": "^4.5.5"
Expand Down
45 changes: 45 additions & 0 deletions web/packages/form-engine/src/utils/exec.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { exec } from './exec';
import Sval from 'sval';

describe('exec', () => {
it('should return the result of the code', () => {
const result = exec(`$this.value === "image"`, {
$this: {
value: 'text',
},
});
expect(result).toBe(false);
});

it('exec sval ver', () => {
const result = exec(`$this.value === "image"`, {
$this: {
value: 'image',
},
});
expect(result).toBe(true);

const result2 = exec(`$this.value === "image"`, {
$this: {
value: 'text',
},
});
expect(result2).toBe(false);
});

it('sval', () => {
const interpreter = new Sval({
ecmaVer: 'latest',
sourceType: 'script',
sandBox: true,
});
const code = `
globalThis.$this = {
value: "text"
}
exports.a = $this.value === "image"
`;
interpreter.run(code);
expect(interpreter.exports.a).toBe(false);
});
});
37 changes: 24 additions & 13 deletions web/packages/form-engine/src/utils/exec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import { TContext } from '../types';
import Sval from 'sval';

function exec(code: string, scope: TContext) {
try {
const str = `
var ${'____data'} = arguments[0];
with(${'____data'}) {
return ${code}
}
`;
function safeExec(expression: string, scope: TContext) {
const interpreter = new Sval({
ecmaVer: 'latest',
sourceType: 'script',
sandBox: true,
});

return new Function(str)(scope);
} catch (e) {
console.log(e);
}
let globalThisAssignments = '';

Object.keys(scope).forEach(key => {
const value = scope[key];
globalThisAssignments += `globalThis[${JSON.stringify(
key,
)}] = ${JSON.stringify(value)};\n`;
});

const code = `
${globalThisAssignments}
exports.a = ${expression};`;

interpreter.run(code);

return interpreter.exports.a;
}

export { exec };
export { safeExec as exec };
5 changes: 5 additions & 0 deletions web/packages/form-engine/wallaby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function (wallaby) {
return {
autoDetect: ['jest'],
};
};
Loading