Skip to content

Fix HTML in marker message - #10278

Merged
kevinushey merged 6 commits into
spotted-wakerobinfrom
feature/add-source-marker-to-source-code-pane
Jan 10, 2022
Merged

kevinushey merged 6 commits into
spotted-wakerobinfrom
feature/add-source-marker-to-source-code-pane

Conversation

@timtmok

@timtmok timtmok commented Dec 22, 2021 •

Copy link
Copy Markdown
Contributor

Intent

Addresses #9010
Allow HTML in the marker message
image

Approach

Unescapes text when setting innerHTML for marker. The marker has escaped text so it would never be processed as HTML in the marker pane.

Automated Tests

none

QA Notes

Marker text containing HTML will need to be escaped or it will be rendered as HTML.
Setting the text to <div style="color:red;">I am red</div> should be treated as literal text when showing up in the marker pane.

Checklist

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests

@kevinushey

Copy link
Copy Markdown
Collaborator

Do we need to worry about XSS here?

@timtmok timtmok closed this Dec 23, 2021
@timtmok
timtmok force-pushed the feature/add-source-marker-to-source-code-pane branch from c4a5a20 to e3f38ad Compare December 23, 2021 14:04
Unescapes text when setting innerHTML for marker
@timtmok

timtmok commented Jan 3, 2022

Copy link
Copy Markdown
Contributor Author

I accidentally closed this!

I don't think XSS is an issue but I only tried setting a marker message with <script>alert("test");</script> and didn't get an alert. It does show up in the HTML source even though the marker pane doesn't show the actual text. It is concerning that there would be hidden HTML and I think there is potential for an exploit.

Maybe it makes more sense to allow certain HTML tags or build options for setting text colours?

@timtmok timtmok reopened this Jan 3, 2022
@kevinushey

Copy link
Copy Markdown
Collaborator

Maybe it makes more sense to allow certain HTML tags or build options for setting text colours?

I think this is the right call. Or even, since we already support ANSI escapes... we could allow (and document) the use of ANSI escapes for colored text, and ask users to use things like cli::col_red() to color their text.

Screen Shot 2022-01-03 at 2 01 04 PM

If we go that route, I think it would suffice to have the text "rendered" by a virtual console. I think we do something similar e.g. here:

VirtualConsole vc = RStudioGinjector.INSTANCE.getVirtualConsoleFactory().create(errorMessage.getElement());
vc.submit(err.getErrorMessage().trim());
errorMessage.addStyleName(errorClass);

Allows ANSI colors and formatting for marker text
@timtmok
timtmok changed the base branch from main to spotted-wakerobin January 5, 2022 20:07
@timtmok

timtmok commented Jan 5, 2022

Copy link
Copy Markdown
Contributor Author

I've managed to switch over to using the VirtualConsole to render the ANSI codes. I was a bit stuck for a bit until I realized the trim was removing the ANSI escape characters. I have tried to inject HTML into the marker text and it's escaped as expected.

I've retargeted to merge into spotted wakerobin since the docs will need updating.

image

@kevinushey kevinushey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very nice!

The next thing we could do is update the documentation in the rstudioapi package, which provides the user-facing interface to source marker generation. That documentation lives here:

https://github.com/rstudio/rstudioapi/blob/96fad1d75b5cacd9a0fe9ff7f58d140f5cf4300b/R/stubs.R#L160-L200

Would you be willing to update the documentation there, giving a note that ANSI escapes are supported? (And so, colors as generated via the cli package can be used.)

@kevinushey

Copy link
Copy Markdown
Collaborator

We can also add a stub to the relevant NEWS file for SW?

@timtmok

timtmok commented Jan 7, 2022

Copy link
Copy Markdown
Contributor Author

I've reset the NEWS file and added in the change. The doc change is over here: rstudio/rstudioapi#242

@gadenbuie

Copy link
Copy Markdown
Member

What types of text and content can the VirtualConsole render using ANSI escapes?

@timtmok

timtmok commented Jan 7, 2022

Copy link
Copy Markdown
Contributor Author

Any of the color and background color codes could be used (ie. cli::col_red or cli::bg_white). The styles allow inverting the background and foreground colors, italic, bold, and underline from what I've tried. It should be any of the codes as defined here: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters. However, I think some wouldn't work like blink (uses CSS that browsers don't support anymore) and super/sub script (doesn't appear to be handled in VirtualConsole).

@gadenbuie

gadenbuie commented Jan 7, 2022 •

Copy link
Copy Markdown
Member

Thanks for the clarification @timtmok.

I understand the need to avoid exploits but I think that the colored or styled text in #9010 was primarily to demonstrate the issue and not actually how users would like to use the feature.

Personally, I was looking at using source markers with rich text as a way to provide better feedback to users who are learning R. Being able to include basic HTML, including things like links, lists, formatted text, etc (essentially the HTML produced by strict markdown) made this feature attractive for that purpose, especially since the feedback could be linked to a specific line. This would be even more useful when combined with #10062. There are also a few other IDEs that have this feature and show rich text in linting-related popovers.

@kevinushey

kevinushey commented Jan 7, 2022 •

Copy link
Copy Markdown
Collaborator

Personally, I was looking at using source markers with rich text as a way to provide better feedback to users who are learning R. Being able to include basic HTML, including things like links, lists, formatted text, etc (essentially the HTML produced by strict markdown) made this feature attractive for that purpose, especially since the feedback could be linked to a specific line. This would be even more useful when combined with #10062. There are also a few other IDEs that have this feature and show rich text in linting-related popovers.

For what it's worth, @romainfrancois's work in #10299 should make it possible to create custom hyperlinks here as well. The only thing that might not fit here would be lists.

If we wanted to support this case while remaining safe, we could (in theory) allow for strict Markdown text, which we'd then render to HTML before display.

I think it'd be helpful to have an illustrative example of what kind of error you might expect an R user to see, and what text / output you'd like to be able to display, + what interactive features that could have.

We could also just support all of HTML, ANSI text, and strict Markdown -- it sounds like I missed the simple fact that we already had the infrastructure for rendering HTML, but needed a change in .rs.scalar() as you indicated in #9010

@gadenbuie

Copy link
Copy Markdown
Member

Strict markdown would definitely work for me, or even HTML with a subset of allowed tags.

One example might be the goodpractice package, where source markers could be used to provide package authoring advice, e.g.

~/source/DESCRIPTION
Line 13 Importing packages with "Depends" is not recommended

Avoid using "Depends" in DESCRIPTION, as it can cause name clashes, and poor interaction with other packages. Use "Imports" instead.

Read more about package dependency best practices in Chapter 13: Namespaces of R Packages.

Another example would be auto-grading messages for example through gradethis. In this scenario, a grading script could read an R script or R Markdown document and produce teaching-related content or advice to a learner. Here's an example of the feedback we might want to provide:

~/source/student.R
Line 13 Incorrect

I expected you to call mean() where you called max(). Try it again. I have a good feeling about this.

This example brings up another idea. It would be extra awesome if there were a mechanism to interact with the IDE in basic ways. The one that comes to mind specifically would be to be able to open a help topic in the Help pane -- rather than linking to external documentation as in the example above.

@kevinushey
kevinushey merged commit c3854b7 into spotted-wakerobin Jan 10, 2022
@timtmok
timtmok deleted the feature/add-source-marker-to-source-code-pane branch February 22, 2022 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sourceMarkers does not evaluate HTML

3 participants