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

block javascript urls in @alifd/biz-anchor #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions components/anchor/src/link.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import React from 'react';
import classNames from 'classnames';

const isJavaScriptProtocol = /^[\u0000-\u001F ]*j[\r\n\t]*a[\r\n\t]*v[\r\n\t]*a[\r\n\t]*s[\r\n\t]*c[\r\n\t]*r[\r\n\t]*i[\r\n\t]*p[\r\n\t]*t[\r\n\t]*\:/i

class Link extends React.Component {
handleClick = (e) => {
this.props.onItemClick(e);
}

render() {
const { className, children, href, title, active, level, ...others } = this.props;
const { className, children, href, title, active, level, allowJavaScriptUrls, ...others } = this.props;

if (isJavaScriptProtocol.test(href) && !allowJavaScriptUrls) {
console.warn(`Link has blocked a javascript: URL as a security precaution`);
return null;
}

const cls = classNames({
className: !!className,
Expand All @@ -28,7 +35,8 @@ class Link extends React.Component {
Link.displayName = 'Link';

Link.defaultProps = {
onItemClick: () => {}
onItemClick: () => {},
allowJavaScriptUrls: true
};

export default Link;