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

Decode at-mark and backslash ( #1307 ) #1588

Closed
wants to merge 3 commits into from

Conversation

apple502j
Copy link
Contributor

Resolves

Fixes #1307

Proposed Changes

Decode \\@ and \\\\ using regex and replace. The issue didn't contain the backslash one, but I also found that, so it contains fix for that.

Reason for Changes

Not to change custom block name.

Test Coverage

https://scratch.mit.edu/projects/246543955/
This project has 2 custom blocks named @atmark@ and \backslash\

Tested using above with Windows 7, Firefox 62 and Chrome 68

@joker314
Copy link
Contributor

This is a nice pull request, but it doesn't account for % being encoded.

Scratch 3.0 with a custom block that has a percent symbol
Take note of the backslash before the final percent sign
Scratch 2.0 with a custom block that has a percent symbol
Take note of the absence of a backslash before the final percent sign

I haven't tested your PR locally yet, I'm just assuming % won't work from looking through the code. I may be mistaken however.

Note that, per my issuecomment, only backslash, at, and percent are likely to be affected.

@apple502j
Copy link
Contributor Author

OK, I will decode percent too. Thanks @joker314

@joker314
Copy link
Contributor

joker314 commented Sep 16, 2018

In Scratch 2.0, the algorithm for escaping the data is not the mirror image of the algorithm for unescaping that data.

This pull request, however, takes the mirror image of the escape function.

This is absolutely fine; and the only time this will not work is if there's 'hacked' JSON.

Somebody may modify their string like so:

- ask user how they are doing
+ \a\s\k\ \u\s\e\r\ \h\o\w\ \t\h\e\y\ \a\r\e\ \d\o\i\n\g

Upon performing such a modification, their project would behave identically. There's no reason to make such a change as a JSON hacker (and so there's no reason to worry about it in this PR, maybe), however, if such a modification has been made, this pull request would leave the backslashes in.

I'd say that this is absolutely fine and we shouldn't worry about it.


Regarding the chaining of .replace()s:

.replace(/\\([%@\\])/g, '$1')

might be shorter. I'm not sure if this is more readable? I also don't know how this may impact efficiency -- it's a style choice that you may wish to consider.

This could be modified to resolve the above issue about hacked JSONs like so:

.replace(/\\(.)/g, '$1')

Though, as stated earlier, the hacked JSON issue seems like a non-issue.

@apple502j
Copy link
Contributor Author

I added percent decoding, but it uses full-width percent to avoid scratchfoundation/scratch-blocks#1368

@joker314
Copy link
Contributor

joker314 commented Sep 16, 2018

Hmm. Maybe I was wrong about us needing to decode \% as %. If Scratch 3.0 still considers it special, leave it as-is. Note that in my picture, only one of the percents was keeping the backslash: the one which didn't have a character after it. I'm investigating when Scratch 2.0 escapes it and when it doesn't.


EDIT: Scratch 2.0 will always encode percent symbols with a preceding backslash. Hence, Scratch 3.0 is assigning some percent signs special meanings -- but others not. Likely, if there's a character after the percent sign, it'll become a special token.


EDIT: I can only seem to get the first percent sign to be treated as something that can be escaped.

@joker314
Copy link
Contributor

joker314 commented Sep 16, 2018

Okay, some things to note:

  • It looks like the first escaped percent sign to appear in the string, will be stripped of its backslash. The rest won't. This leads me to believe that there may be a forgotten g flag on a regular expression, elsewhere, and would be out of the scope of this pull request. Perhaps not, though.
  • This isn't strictly related to this PR, but it is related to the stuff it's addressing:
    https://github.com/LLK/scratch-vm/blob/64a1d3e02b6aaaac9d6ec5e7299fc13e8199e136/src/serialization/sb2.js#L60
    This looks like it would not account for an escaped backslash before a percent sign (i.e., \\%), because it'd detect there was a \% present and incorrectly ignore it.

    Should an issue be filed?

@apple502j
Copy link
Contributor Author

@joker314 It's already help-wanted, scratchfoundation/scratch-blocks#1368

@joker314
Copy link
Contributor

@apple502j When you made the change to the full-width percent sign, did you actually manage to get a working example of a 2.0 project that would cause scratchfoundation/scratch-blocks#1368 to happen?

@apple502j
Copy link
Contributor Author

@joker314 Oops, 1368 was not related at all.

Copy link
Contributor

@joker314 joker314 left a comment

Choose a reason for hiding this comment

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

It's probably okay to remove the replacement to the full-width percent sign, then?

@kchadha
Copy link
Contributor

kchadha commented Sep 25, 2018

@apple502j, Thanks for making this pull request! We are a little backed up on code reviews at the moment, but we'll take a look at these changes soon.

Copy link
Contributor

@kchadha kchadha left a comment

Choose a reason for hiding this comment

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

@apple502j, Thank you for the pull request! I have left a comment in the code changes.

@@ -1061,7 +1061,8 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
children: [],
mutation: {
tagName: 'mutation',
proccode: procData[0], // e.g., "abc %n %b %s"
proccode: procData[0].replace(/\\%/g, '%').replace(/\\(.)/g, '$1'),

This comment was marked as abuse.

@@ -1061,7 +1061,8 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
children: [],
mutation: {
tagName: 'mutation',
proccode: procData[0], // e.g., "abc %n %b %s"
proccode: procData[0].replace(/\\%/g, '%').replace(/\\(.)/g, '$1'),
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this is a fullwidth percent sign-- "replace this character with another very similar looking character" strikes me as somewhat janky

@apple502j
Copy link
Contributor Author

Hmm, maybe I should make better one

@apple502j apple502j closed this May 15, 2020
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.

Custom blocks import "@" in the label as "\@"
5 participants