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

Ruby: Add heredoc literals #2885

Merged
merged 1 commit into from
May 14, 2021
Merged

Conversation

sj26
Copy link
Contributor

@sj26 sj26 commented May 3, 2021

This is a first pass at adding heredoc string literals for Ruby:

https://ruby-doc.org/core-2.7.0/doc/syntax/literals_rdoc.html#label-Here+Documents+-28heredocs-29

This only adds the basic form:

<<STRING
foo #{42} bar
STRING

There are plenty of other styles with slightly different forms not yet covered by this PR:

<<'STRING'
foo #{42} bar
STRING
<<`STRING`
test 0 -eq #{0}
STRING
def baz
  <<-STRING
  foo #{42} bar
  STRING
end
def baz
  <<~STRING
    foo #{42} bar
  STRING
end

@github-actions
Copy link

github-actions bot commented May 3, 2021

JS File Size Changes (gzipped)

A total of 1 files have changed, with a combined diff of +93 B (+9.2%).

file master pull size diff % diff
components/prism-ruby.min.js 1.01 KB 1.11 KB +93 B +9.2%

Generated by 🚫 dangerJS against a645e5f

Copy link
Member

@RunDevelopment RunDevelopment left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @sj26!

It looks mostly good as is. I left you a few comments and please commit the minified files as well (run npm ci && npm run build to generate those).

tests/run.js Outdated Show resolved Hide resolved
components/prism-ruby.js Outdated Show resolved Hide resolved
@sj26
Copy link
Contributor Author

sj26 commented May 5, 2021

Thanks @RunDevelopment, I've removed the test suite change and expanded the heredoc support to more forms. It's not perfect, but it works well enough.

I wasn't able to run npm ci && npm run build for you though, sorry:

$ npm ci && npm run build

added 932 packages, and audited 933 packages in 7s

12 low severity vulnerabilities

To address issues that do not require attention, run:
  npm audit fix

To address all issues possible (including breaking changes), run:
  npm audit fix --force

Some issues need review, and may require choosing
a different dependency.

Run `npm audit` for details.

> [email protected] build
> gulp

[15:09:01] Using gulpfile ~/Developer/prism/gulpfile.js
[15:09:01] Starting 'default'...
[15:09:01] Starting 'minifyComponents'...
[15:09:01] Starting 'componentsJsonToJs'...
[15:09:01] Starting 'build'...
[15:09:01] Starting 'languagePlugins'...


#
# Fatal error in , line 0
# Check failed: allocator->SetPermissions(reinterpret_cast<void*>(region.begin()), region.size(), PageAllocator::kNoAccess).
#
#
#
#FailureMessage Object: 0x16dcb09d8
 1: 0x102249758 node::NodePlatform::GetStackTracePrinter()::$_3::__invoke() [/opt/nodenv/versions/15.6.0/bin/node]
 2: 0x102f91d50 V8_Fatal(char const*, ...) [/opt/nodenv/versions/15.6.0/bin/node]
 3: 0x1028e3b98 v8::internal::wasm::WasmCodeManager::Decommit(v8::base::AddressRegion) [/opt/nodenv/versions/15.6.0/bin/node]
 4: 0x1028e7218 v8::internal::wasm::NativeModule::FreeCode(v8::internal::Vector<v8::internal::wasm::WasmCode* const>) [/opt/nodenv/versions/15.6.0/bin/node]
 5: 0x1028f6400 v8::internal::wasm::WasmEngine::FreeDeadCodeLocked(std::__1::unordered_map<v8::internal::wasm::NativeModule*, std::__1::vector<v8::internal::wasm::WasmCode*, std::__1::allocator<v8::internal::wasm::WasmCode*> >, std::__1::hash<v8::internal::wasm::NativeModule*>, std::__1::equal_to<v8::internal::wasm::NativeModule*>, std::__1::allocator<std::__1::pair<v8::internal::wasm::NativeModule* const, std::__1::vector<v8::internal::wasm::WasmCode*, std::__1::allocator<v8::internal::wasm::WasmCode*> > > > > const&) [/opt/nodenv/versions/15.6.0/bin/node]
 6: 0x1028f4108 v8::internal::wasm::WasmEngine::PotentiallyFinishCurrentGC() [/opt/nodenv/versions/15.6.0/bin/node]
 7: 0x1028f557c v8::internal::wasm::WasmEngine::ReportLiveCodeForGC(v8::internal::Isolate*, v8::internal::Vector<v8::internal::wasm::WasmCode*>) [/opt/nodenv/versions/15.6.0/bin/node]
 8: 0x1028f592c v8::internal::wasm::WasmEngine::ReportLiveCodeFromStackForGC(v8::internal::Isolate*) [/opt/nodenv/versions/15.6.0/bin/node]
 9: 0x10247f348 v8::internal::StackGuard::HandleInterrupts() [/opt/nodenv/versions/15.6.0/bin/node]
10: 0x1027ba830 v8::internal::Runtime_StackGuard(int, unsigned long*, v8::internal::Isolate*) [/opt/nodenv/versions/15.6.0/bin/node]
11: 0x102aabb6c Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/opt/nodenv/versions/15.6.0/bin/node]
12: 0x102a44910 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
13: 0x102b1fbf8 Builtins_SortCompareUserFn [/opt/nodenv/versions/15.6.0/bin/node]
14: 0x102b222dc Builtins_ArrayTimSort [/opt/nodenv/versions/15.6.0/bin/node]
15: 0x102b234c8 Builtins_ArrayPrototypeSort [/opt/nodenv/versions/15.6.0/bin/node]
16: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
17: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
18: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
19: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
20: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
21: 0x102a3bea4 Builtins_ArgumentsAdaptorTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
22: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
23: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
24: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
25: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
26: 0x102a3bea4 Builtins_ArgumentsAdaptorTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
27: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
28: 0x102a3bea4 Builtins_ArgumentsAdaptorTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
29: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
30: 0x102a44874 Builtins_InterpreterEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
31: 0x102a3bea4 Builtins_ArgumentsAdaptorTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
32: 0x102a42124 Builtins_JSEntryTrampoline [/opt/nodenv/versions/15.6.0/bin/node]
33: 0x102a41dc8 Builtins_JSEntry [/opt/nodenv/versions/15.6.0/bin/node]
34: 0x158008000 
zsh: trace trap  npm run build

Maybe because I'm on an M1 mac?

@RunDevelopment
Copy link
Member

Maybe because I'm on an M1 mac?

Yes but don't worry. According to nodejs/node#37061, this issue has been fixed in v15.9.0. You just need to update your NodeJS version.

@RunDevelopment
Copy link
Member

I have no idea why the tests fail. Everything looks fine to me. I'll investigate this tomorrow.

@RunDevelopment
Copy link
Member

Fixed the test problem. Now they fail, as they should, and give a proper error message. Let's look at the error message:

  5) Patterns of 'ruby'
       - should not cause polynomial backtracking:
     AssertionError: <Unknown>: Polynomial backtracking. By repeating any character that matches /[\n\r]/i, an attack string can be created.

    (?:.*[\r\n])*?\s*
    ^~~~~~~~~~~~~~[start]
                  ^~~[end]

Full pattern:
/<<[-~]?([a-z_]\w*)[\r\n](?:.*[\r\n])*?\s*\1/gi
                         ^~~~~~~~~~~~~~[start]
                                       ^~~[end]

Fix not available.

As we can see, an attacker could exploit this pattern's polynomial backtracking behavior. I'll give suggestions to fix this problem.

@sj26
Copy link
Contributor Author

sj26 commented May 13, 2021

Thanks @RunDevelopment, I've folded those changes in 👍

@RunDevelopment
Copy link
Member

Please rebuild @sj26 and then we can merge this.

Adds support for heredoc-style string literals in the Ruby syntax.

https://ruby-doc.org/core-2.7.0/doc/syntax/literals_rdoc.html#label-Here+Documents+-28heredocs-29

This isn't perfect, but it's a good start.
@sj26
Copy link
Contributor Author

sj26 commented May 14, 2021

Sorry I forgot to rebuild, but done now 👍

@RunDevelopment RunDevelopment merged commit 20b77bf into PrismJS:master May 14, 2021
@RunDevelopment
Copy link
Member

Thank you for contributing @sj26!

@sj26 sj26 deleted the ruby-heredoc branch May 21, 2021 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants