Skip to content

Commit

Permalink
restricted subset of regex for INTERPOLATE
Browse files Browse the repository at this point in the history
Nothing that can execute arbitrary code is allowed into regex
interplations now.  Probably.  Maybe.
  • Loading branch information
TimToady committed Dec 19, 2015
1 parent c06c4c4 commit 28ab83f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Perl6/Grammar.nqp
Expand Up @@ -387,6 +387,11 @@ role STD {
}
self
}

token RESTRICTED {
[ <?{ $*RESTRICTED }> [ $ || <.malformed($*RESTRICTED)> ] ]?
<!>
}
}

grammar Perl6::Grammar is HLL::Grammar does STD {
Expand Down Expand Up @@ -1995,12 +2000,12 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
| <special_variable>
| <sigil> $<index>=[\d+] [<?{ $*IN_DECL }> <.typed_panic: "X::Syntax::Variable::Numeric">]?
| <sigil> <?[<]> <postcircumfix> [<?{ $*IN_DECL }> <.typed_panic('X::Syntax::Variable::Match')>]?
| <?before <sigil> <?[ ( [ { ]>> <?{ !$*IN_DECL }> <contextualizer>
| <?before <sigil> <?[ ( [ { ]>> <!RESTRICTED> <?{ !$*IN_DECL }> <contextualizer>
| $<sigil>=['$'] $<desigilname>=[<[/_!¢]>]
| {} <sigil> <!{ $*QSIGIL }> <?MARKER('baresigil')> # try last, to allow sublanguages to redefine sigils (like & in regex)
]
[ <?{ $<twigil> && $<twigil> eq '.' }>
[ <.unsp> | '\\' | <?> ] <?[(]> <arglist=.postcircumfix>
[ <.unsp> | '\\' | <?> ] <?[(]> <!RESTRICTED> <arglist=.postcircumfix>
]?
{ $*LEFTSIGIL := nqp::substr(self.orig(), self.from, 1) unless $*LEFTSIGIL }
}
Expand Down Expand Up @@ -2950,7 +2955,7 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
| ['*'|'<...>'|'<*>'] <?{ $*MULTINESS eq 'proto' }> $<onlystar>={1}
| <nibble(self.quote_lang(%*RX<P5> ?? %*LANG<P5Regex> !! %*LANG<Regex>, '{', '}'))>
]
'}'<?ENDSTMT>
'}'<!RESTRICTED><?ENDSTMT>
{ $*CURPAD := $*W.pop_lexpad() }
] || <.malformed('regex')>
}
Expand Down Expand Up @@ -4729,7 +4734,7 @@ grammar Perl6::QGrammar is HLL::Grammar does STD {
}

role c1 {
token escape:sym<{ }> { :my $*ESCAPEBLOCK := 1; <?[{]> <block=.LANG('MAIN','block')> }
token escape:sym<{ }> { :my $*ESCAPEBLOCK := 1; <?[{]> <!RESTRICTED> <block=.LANG('MAIN','block')> }
}

role c0 {
Expand All @@ -4740,6 +4745,7 @@ grammar Perl6::QGrammar is HLL::Grammar does STD {
token escape:sym<$> {
:my $*QSIGIL := '$';
<?[$]>
<!RESTRICTED>
[ <EXPR=.LANG('MAIN', 'EXPR', 'y=')> || { $*W.throw($/, 'X::Backslash::NonVariableDollar') } ]
}
}
Expand All @@ -4752,6 +4758,7 @@ grammar Perl6::QGrammar is HLL::Grammar does STD {
token escape:sym<@> {
:my $*QSIGIL := '@';
<?[@]>
<!RESTRICTED>
<EXPR=.LANG('MAIN', 'EXPR', 'y=')>
}
}
Expand All @@ -4764,6 +4771,7 @@ grammar Perl6::QGrammar is HLL::Grammar does STD {
token escape:sym<%> {
:my $*QSIGIL := '%';
<?[%]>
<!RESTRICTED>
<EXPR=.LANG('MAIN', 'EXPR', 'y=')>
}
}
Expand All @@ -4776,6 +4784,7 @@ grammar Perl6::QGrammar is HLL::Grammar does STD {
token escape:sym<&> {
:my $*QSIGIL := '&';
<?[&]>
<!RESTRICTED>
<EXPR=.LANG('MAIN', 'EXPR', 'y=')>
}
}
Expand All @@ -4798,6 +4807,7 @@ grammar Perl6::QGrammar is HLL::Grammar does STD {
<?[]> <quote=.LANG('MAIN','quote')>
}
token escape:sym<colonpair> {
<!RESTRICTED>
<?[:]> <colonpair=.LANG('MAIN','colonpair')>
}
token escape:sym<#> {
Expand Down Expand Up @@ -5073,6 +5083,7 @@ grammar Perl6::RegexGrammar is QRegex::P6Regex::Grammar does STD does CursorPack

token metachar:sym<:my> {
':' <?before 'my'|'constant'|'state'|'our'|'temp'|'let'> <statement=.LANG('MAIN', 'statement')>
<!RESTRICTED>
<.LANG('MAIN', 'eat_terminator')>
}

Expand Down Expand Up @@ -5118,26 +5129,32 @@ grammar Perl6::RegexGrammar is QRegex::P6Regex::Grammar does STD does CursorPack
}

token assertion:sym<var> {
<!RESTRICTED>
[
| <?[&]> <var=.LANG('MAIN', 'term:sym<variable>')>
[
| ':' <arglist>
| '(' <arglist> ')'
]?
| <?sigil> <var=.LANG('MAIN', 'term:sym<variable>')>
]
}

token assertion:sym<~~> {
<sym>
<!RESTRICTED>
[ <?[>]> | $<num>=[\d+] | <desigilname=.LANG('MAIN','desigilname')> ]
}

token codeblock {
:my $*ESCAPEBLOCK := 1;
<!RESTRICTED>
<block=.LANG('MAIN','block')>
}

token arglist {
:my $*IN_REGEX_ASSERTION := 1;
<!RESTRICTED>
<arglist=.LANG('MAIN','arglist')>
}

Expand Down
1 change: 1 addition & 0 deletions src/core/Cursor.pm
Expand Up @@ -359,6 +359,7 @@ sub MAKE_REGEX($arg, :$i, :$m, :$context) {
$arg.regex
}
else {
my $*RESTRICTED = 'regex interpolation';
my $rx := $i && $m ?? EVAL("anon regex \{ :i :m $arg\}", :$context) !!
$i ?? EVAL("anon regex \{ :i $arg\}", :$context) !!
$m ?? EVAL("anon regex \{ :m $arg\}", :$context) !!
Expand Down

0 comments on commit 28ab83f

Please sign in to comment.