33 Commits

Author SHA1 Message Date
Wyatt Allen
db8691a96b Remove styles for markdown syntax
Support for markdown files would only typeset text as bold or italic
when it appeared inside the corresponding inline Markdown syntax. It
didn't render the Markdown, but rather approximated what the text might
look like if rendered. Because this provides little real value, and
because HLJS sometimes incorrectly parses these Markdown constructs,
markdown is dropped from the highlighting layer.

Bug: Issue 9056
Change-Id: I97865dc8b522dca5a50264456274590fac83dae9
2018-06-06 09:27:20 -07:00
Wyatt Allen
e9bf51941a Generalize library loader
Until recently, the only side-loaded resource was the HLJS library for
computing syntax highlighting. For this task, the gr-syntax-lib-loader
provided an interface to load the library whether or not PG is being
served from a CDN.

With this change, the component is refactored to allow loading resources
other than the syntax library. A method is added for loading the
"dark-theme" document independently of whether a CDN is configured.

Also, some documentation comments are added to the existing methods.

Change-Id: I9891539cd4cf76ac0fe430ff3988e3a9dfbb0ca3
2018-05-21 15:54:38 -07:00
Paladox none
0e0bb8eeb5 Add support for jsx lang to be syntax highlighted
It is highlighted in with javascript.js in highlight.js so it dosen't
have a seperate file for it's lang.

Bug: Issue 8646
Change-Id: I5f561c342b96334eda82f8165b018209f10cab36
2018-04-05 17:47:53 +00:00
Dave Borowitz
8cdc76ba4c Add @license tags to PG HTML and JS assets
These tags are preserved by the Closure compiler and vulcanize in order
to serve the license notices embedded in the outputs. In a standalone
Gerrit server, these license are also covered in the LICENSES.txt served
with the documentation. When serving PG assets from a CDN, it's less
obvious what the corresponding LICENSES.txt file is, since the CDN is
not directly linked to a running Gerrit server. Safer to embed the
licenses in the assets themselves.

Change-Id: Id1add1451fad1baa7916882a6bda02c326ccc988
2018-03-26 10:47:55 -04:00
Paladox none
656ad15271 Re organise CLASS_WHITELIST
Puts it into alphabetical order

Change-Id: Ib4fcada7a85bdb07bf2b73c956ef87609f2defcd
2018-03-05 00:00:39 +00:00
Becky Siegel
8d92d53db5 Annotation updates
Change-Id: I146f76b9dcc1a92e18acec01481ad280fb431868
2017-08-12 11:49:52 -07:00
Paladox none
e28533f5c8 PolyGerrit: Add support for erlang and puppet in highlight
Change-Id: I37bfe95ba0a392ebe1b7620d780e7affeedcb318
2017-07-14 00:43:01 +00:00
Paladox none
f4e0308f0c PolyGerrit: Add support for php and shell in highlight
shell is a common languge + php is used alot downstream.

Change-Id: I419bdee569d5763b03414f7527ede3b7aefc4ae7
2017-07-13 22:42:34 +00:00
Logan Hanks
58ad06614d Update highlight.js to 9.12.0 and enable kotlin
This increases the uncompressed payload by about 6 KB (2 KB compressed).

Bug: Issue 6470
Change-Id: I5333e3826097765f1aee16770769a1bcd8da8f5c
2017-06-21 19:59:18 +00:00
Mike Samuel
e07c4b2ea1 Add polygerrit-ui/app/test/common-test-setup.html
This is a partial roll-forward of c/106190

This replaces all loads of iron-test-helpers with a load of a file
that wraps it, and adds that file to test files that do not currently
load iron-test-helpers.

A future CL will also install polymer-resin via common-test-helpers.html.

I tested by running

$ WCT_ARGS="-l chrome" ./polygerrit-ui/app/run_test.sh

Change-Id: Ifb3cd2c8db13d724f57e56e7e78045470d103a43
2017-06-05 22:10:12 +00:00
Wyatt Allen
c601abccc3 Revert "Polygerrit now loads polymer-resin"
This reverts commit 0895052c01ac5ac657a9763d2ad9967d9ae55c18.

Reason for revert: issue 6387

Change-Id: I14e00addeab53606952aa3ea2d45a74eac7a9d8a
2017-06-02 09:37:37 -07:00
Mike Samuel
0895052c01 Polygerrit now loads polymer-resin
polymer-resin intercepts polymer property assignments
before they reach XSS-vulnerable sinks like `href="..."`
and text nodes in `<script>` elements.

This follows the instructions in WORKSPACE for adding a new bower
dependency with kaspern's tweak to use the dependency in a rule so
that it's found.  //lib/js/bower_components.bzl has already been
rolled-back per those instructions.

The license is the polymer license as can be seen at
https://github.com/Polymer/polymer-resin/blob/master/LICENSE though
I'm not sure that //tools/js/bower2bazel.py recognizes it as such.

Docs for the added component are available at
https://github.com/Polymer/polymer-resin/blob/master/README.md
https://github.com/Polymer/polymer-resin/blob/master/getting-started.md

With this change, when I introduce an XSS vulnerability as below,
polymer-resin intercepts and stops it.

Patch that introduces a strawman vulnerability.

--- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js
+++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js
@@ -55,6 +55,10 @@
         url: '/q/status:abandoned',
         name: 'Abandoned',
       },
+      {
+        url: location.hash.replace(/^#/, '') || 'http://example.com/#fragment_echoed_here',
+        name: 'XSS Me',
+      },
     ],
   }];

---

Address kaspern's and paladox's comments.

---

Undo version bumps for bower dependencies.

---

Change Soy index template to parallel app/index.html.

---

update polymer-resin to version 1.1.1-beta

----

Load polymer-resin into polygerrit-ui/**/*_test.html

After this, I ran the tests with
  -l chrome
  -l firefox

I ran a handful of tests with -p and observed that the
console shows "initResin" is called before test cases start
executing.

These changes were done programmaticly by running the script below
(approximately) thus:
```
gerrit/ $ cd polygerrit-ui/app
app/ $ find . -name \*test.html | xargs perl hack-tests.pl
```

```
use strict;

sub removeResin($) {
  my $s = $_[0];
  $s =~ s@<link rel="import" href="[^"]*/polymer-resin/[^"]*"[^>]*>\n?@@;
  $s =~ s@<script src="[^"]*/polymer-resin/[^"]*"></script>\n?@@;
  $s =~ s@<script>\s*security\.polymer_resin.*?</script>\n?@@s;
  return $s;
}

for my $f (@ARGV) {
  next if $f =~ m@/bower_components/|/node_modules/@;

  system('git', 'checkout', $f);
  print "$f\n";

  my @lines = ();
  open(IN, "<$f") or die "$f: $!";
  my $maxLineOfMatch = 0;
  while (<IN>) {
    push(@lines, $_);
    # Put a marker after core loading directives.
    $maxLineOfMatch = scalar(@lines)
      if m@/webcomponentsjs/|/polymer[.]html\b|/browser[.]js@;
  }
  close(IN) or die "$f: $!";

  die "$f missing loading directives" unless $maxLineOfMatch;

  # Given ./a/b/c/my_test.html, $pathToRoot is "../../.."
  # assuming no non-leading . or .. components in the path from find.
  my $pathToRoot = $f;
  $pathToRoot =~ s@^\.\/@@;
  $pathToRoot =~ s@^(.*?/)?app/@@;
  $pathToRoot =~ s@\/[^\/]*$@@;
  $pathToRoot =~ s@[^/]+@..@g;

  my $nLines = scalar(@lines);
  open(OUT, ">$f") or die "$f: $!";

  # Output the lines up to the last polymer-resin dependency
  # loaded explicitly by this test.
  my $before = join '', @lines[0..($maxLineOfMatch - 1)];
  $before = removeResin($before);
  print OUT "$before";

  # Dump out the lines that load polymer-resin and configure it for
  # polygerrit.
  if (1) {
      print OUT qq'<link rel="import" href="$pathToRoot/bower_components/polymer-resin/standalone/polymer-resin-debug.html"/>
<script>
security.polymer_resin.install({allowedIdentifierPrefixes: [\'\']});
</script>
    ';
  }

  # Emit any remaining lines.
  my $after = join '', @lines[$maxLineOfMatch..$#lines];
  $after = removeResin($after);
  $after =~ s/^\n*//;
  print OUT "$after";

  close(OUT) or die "$f: $!";
}
```

---

update polymer-resin to version 1.2.1-beta

---

update Soy index template to new style polymer-resin initialization

----

fix lint warnings

----

Load test/common-test-setup.html into *_test.html

Instead of inserting instructions to load and initialize polymer-resin into
every test file, add a common-test-setup.html that does that and also fold
iron-test-helpers loading into it.

----

imported files do not need to load webcomponentsjs

Change-Id: I71221c36ed8a0fe7f8720c1064a2fcc9555bb8df
2017-05-30 23:16:09 -04:00
Kasper Nilsson
c0c600d616 ES6ify /gr-syntax-layer/*
Bug: Issue 6179
Change-Id: I588d313c20647a6121e28be4d8167413e19c647e
2017-05-17 11:19:50 -07:00
Wyatt Allen
8bbb4bb80d Handle wchar_t escape sequences in syntax workaround
Bug: Issue 5930
Change-Id: I10433c748f0e07ee463a138e73d6a689a23609d4
2017-04-04 11:00:45 -07:00
Viktar Donich
29e1ce5e84 Collection of prospective test flake fixes
Potentially related:
https://github.com/Polymer/web-component-tester/issues/505

Bug: Issue 5792
Change-Id: I9ab6e8e40d9811dd52906335426764c052907609
2017-03-30 13:46:58 -07:00
Wyatt Allen
437dc44709 Allow async recursion for syntax on zeroth diff section
The logic inside the syntax layer that decides whether the next step of
the processing is done synchronously or after a timeout would not choose
to use a timeout if it is processing the zeroth section of the diff.

For diffs with very large initial shared chunks (for example a chunk of
more than 20,000 lines as linked in the bug) this results in the syntax
layer processing the entire chunk using synchronous recursion.

As a result, the (1) UI would lock up while processing the syntax for
this chunk, and (2) when rendering all diffs on the change, the call
stack would be exceeded.

With this change, the syntax layer allows asynchrony when processing the
zeroth chunk of the diff.

Bug: Issue 5654
Change-Id: I0e60479b2c59c9c626199e7a6b8d63ccb55ebaa7
2017-02-28 13:13:22 -08:00
Wyatt Allen
e9b9d8d101 Remove color from "params" syntax class
HLJS emits a syntax class for function parameters which the PolyGerrit
syntax stylesheet would color blue. However, HLJS did not always apply
this class accurately, for example, in the C++ case described in the
linked issue.

Because the class was not very informative anyway, and the HLJS default
stylesheet does not style it either, this change removes it from the
PolyGerrit syntax styles.

Bug: Issue 4975
Change-Id: I26ed0b8f745ac6add994a5d1cfc8eb1303dac8cf
2017-02-16 14:51:56 -08:00
Wyatt Allen
4704d2dd69 Include workarounds for character literal syntax bugs
Include workarounds with bug references for a pair of simple HLJS bugs
related to parsing character literals.
* In Go, HLJS misunderstands backslash character literals.
* In C++, HLJS misunderstands wchar_t character literals.

Bug: Issue 5007
Bug: Issue 5242
Change-Id: I4c92b254062198dbf043ccd401e3224961a84a33
2017-01-10 17:05:23 -08:00
Wyatt Allen
696a8473bc Workarounds for HLJS parsing issues
In lieu of upstream fixes to Highlight.js, write local workarounds for
some known parsing bugs.

Bug: Issue 4864
Bug: Issue 4776
Change-Id: I6bde7d44821df18a07ea45dbbc2a7343d597963d
2016-11-28 10:34:29 -08:00
Urs Wolfer
a010547eef Fix issues detected by 'JSHint' and 'JSCS'
Change-Id: Id16d7abe53d5f65c97bf778dc532e404b41283d8
2016-10-20 20:41:00 +02:00
Wyatt Allen
6c8fd1e781 Fix syntax class whitelist test
The syntax class whitelist is a feature that restricts the DOM elements
created for syntax highlighting only to classes which have CSS styles.
However, the test for this whitelist was being applied at the wrong
place, and would ignore whitelisted classes that were nested inside
non-whitelisted classes.

With this change, the location of the whitelist test is fixed, and a
unit test is added to encode the correct behavior.

Bug: Issue 4578
Change-Id: Ic3631b0fb44dc2c691069b8134ad88a55feb977a
2016-10-11 13:09:50 -07:00
Wyatt Allen
e7a51875e6 Address race condition in HLJS loader
Tapping the "Show Diffs" link in the change view to display all of a
change's diffs inline spawns several asynchronous syntax highlighting
processes. Sometimes (especially for long file lists and slower HLJS
download rates) multiple highlight processes would request HLJS before
it had been loaded, resulting in multiple load/definitions of the
library. Occasionally this would result in a JS exception.

With this change, the HLJS loader functionality of GR-SYNTAX-LAYER is
separated into the GR-SYNTAX-LIB-LOADER component. This component
employs a singleton property to track state across multiple lib requests
behind a promise interface.

Tests are updated.

Change-Id: I673998d406a33afa158e04c4dbb91fe31442d527
2016-09-12 16:00:23 -07:00
Viktar Donich
c11b6c573a Apply syntax highlight to .cc files
Change-Id: I6820d4ae9a7d25d750b639ea4efcac1ce8ea86f9
2016-08-12 11:51:16 -07:00
Wyatt Allen
afbc603265 Remove unused syntax elements
The syntax layer sometimes emitted syntax annotations which did not have
corresponding styles, making the resulting elements dead weight in the
DOM. With this change, the syntax layer only attaches the subset of
syntax annotations which have styles by comparing them against a
whitelist.

Change-Id: Id6ced6df2ef765abd3d93cd4798381726619230b
2016-08-11 10:42:17 -07:00
Dave Borowitz
8e475690f3 Merge "Update syntax highlighting support" 2016-08-08 20:42:34 +00:00
Wyatt Allen
c5794e442d Improve performance for unsupported languages
Applies a minor performance improvement when both sides of the diff are
labeled by an unsupported language. Previously, the GR-SYNTAX-LAYER
`process` method would needlessly traverse the entire diff when the
languages were not in the support map. With this change, the `process`
method resolves early in such a scenario.

Change-Id: I8e5ef9fa410facdd60ca33c234ced682ffb182d7
2016-08-05 15:05:32 -07:00
Wyatt Allen
23ab9cc117 Make HLJS loading more CDN friendly
Previously, the absolute path HighlighJS loader did not work when served
from a CDN. With this change, the absolute path for the script considers
the domain that the GR-APP element is served from.

Change-Id: I024507aed175ce7000e3db0c711cbf559e3d2126
2016-08-04 14:37:41 -07:00
Andrew Bonventre
84ab583b8b Don’t use relative path for loading hljs
This causes issues when the app is vulcanized and the paths are
different as a result. The async/defer attributes will ensure
the script does not block parsing of the page.

Adds a flag to the Vulcanize rule that excludes HLJS. Confirmed that the
tag is ignored by Vulcanize phase in the compiled WAR file. Confirmed
that the `onload` case functions as expected via browser throttling.

Change-Id: Ie9603a5a07d4bae198ccc1ee56f408bfa0d37d49
2016-08-03 14:33:59 -07:00
Wyatt Allen
03f0ce6c0f Update syntax highlighting support
Adds new languages and removes an unused language. The built, minified
and gzipped size of the HighlightJS library increases from 17KB to 25KB.

Change-Id: Id3c83e5b0592b85c9f2adf53f07b6852966dcced
2016-08-02 13:15:09 -07:00
Wyatt Allen
db56a6f62c Lazy load HighlightJS library
Previously, the HighlightJS library (HLJS) was being Vulcanized into the
PolyGerrit build, even if the library was not being used (for example if
the user does not navigate to a diff or has syntax highlighting disabled
in preferences). Because HLJS is a substantial file -- ranging in size
from 47KB to more than 455KB (ungizpped) depending on the build
environment, this is not ideal.

In order to lazy load a JS library:

1)  It needs to be copied into the built WAR file to be addressable, and
2)  It should not be Vulcanized into the built gr-app.js file.

Previously, the PolyGerrit build system supported copying and
non-vulcanizing exactly one JS library: namely webcomponents-lite.js.
This change generalizes the mechanism by which webcomponents is copied
and adds HLJS to that list. This satisfies **1**.

Furthermore, the GR-SYNTAX-LAYER is rewritten to dynamically import HLJS
in the `process` step by crafting a SCRIPT element and attaching it to
the local DOM. (Syntax processing is invoked only after entire diff is
rendered.) Code that depends on the library awaits this load before
using it. Thus, the conventional JS import can be removed from the
element's HTML and will not be recognized by the Vulcanize phase. This
satisfies **2**.

Tests are updated accordingly.

Bug: Issue 4298
Change-Id: I9a95d6d4c211cd8f1ca1bc4daec770b64b22b3d1
2016-08-02 09:51:56 -07:00
Wyatt Allen
7c66d99a1c Respect the diff preference for syntax highlighting
Adds checkboxes to both diff preferences controls, adds an `enabled`
boolean property to the `gr-syntax-layer` element, and updates all
relevant tests.

Bug: Issue 4297
Change-Id: I10cef760c354c53e03acfb3c84379e82859ef25f
2016-07-27 11:14:54 -07:00
Wyatt Allen
5c5f00fb13 Refactors annotation layer
Formerly, the annotation layer interface provided the GrAnnotation
library as a parameter to the `annotate` method. This was so the layer
would not necessarily need to import the library at the module level
and instead could use it as a utility toolbox.

With this change, the library is no-longer part of the interface and the
layers are now expected to import it at the module layer (if they have
a use for it).

Change-Id: I49b96c67ec724708c2861ab6be3ce27a53cc1b05
2016-07-25 22:47:01 -07:00
Wyatt Allen
650c529276 Syntax highlighting
Introduces the gr-syntax-layer element. This element works as an
annotation layer that is configured with the diff and asynchronously
computes/applies syntax for the diff.

Introduces a custom build of Highlight.js which gr-syntax-layer makes
use of. Building the script is documented in
scripts/vendor/highlight/building.md.

The layer is connected to the annotation pipeline in gr-diff-builder as
the lowest layer and syntax processing is triggered only after a diff
has been completely rendered.

A number of styles are added to the gr-diff element for syntax markers.
Tests added for gr-syntax-layer.

Bug: Issue 3916
Change-Id: Ic33e40f4fe39dfce1a62de133cfaf32be5e3f25a
2016-07-25 13:47:30 -07:00