Skip to content

Commit 6655c62

Browse files
smkarwaBrendan Linn
authored andcommitted
Add BlessStringAsTrustedResourceUrlForLegacy escaping directive. For more details refer:
1. https://docs.google.com[]document/d/13Eu-tLawTt8LsSU4Vyr41c1QU8BFqDEKnDfWp_uSfmM/edit?usp=sharing 2. https://docs.google.com[]document/d/1o75ucvux0_Y7m4FLYpyeLfqQdvURQvadhUX-FbDXzio/edit?usp=sharing ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=105018708
1 parent b436769 commit 6655c62

File tree

8 files changed

+186
-13
lines changed

8 files changed

+186
-13
lines changed

java/src/com/google/template/soy/basicdirectives/BasicDirectivesModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class BasicDirectivesModule extends AbstractModule {
6060
soyDirectivesSetBinder.addBinding().to(CleanHtmlDirective.class);
6161
soyDirectivesSetBinder.addBinding().to(FilterImageDataUriDirective.class);
6262
soyDirectivesSetBinder.addBinding().to(FilterTrustedResourceUriDirective.class);
63+
soyDirectivesSetBinder.addBinding().to(BlessStringAsTrustedResourceUrlForLegacyDirective.class);
6364
}
6465

6566
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2015 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.template.soy.basicdirectives;
18+
19+
import com.google.common.collect.ImmutableSet;
20+
import com.google.template.soy.data.SoyValue;
21+
import com.google.template.soy.jssrc.restricted.JsExpr;
22+
import com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective;
23+
import com.google.template.soy.pysrc.restricted.PyExpr;
24+
import com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective;
25+
import com.google.template.soy.shared.restricted.Sanitizers;
26+
import com.google.template.soy.shared.restricted.SoyJavaPrintDirective;
27+
import com.google.template.soy.shared.restricted.SoyPurePrintDirective;
28+
29+
import java.util.List;
30+
import java.util.Set;
31+
32+
import javax.inject.Inject;
33+
import javax.inject.Singleton;
34+
35+
/**
36+
* Implements the |blessStringAsTrustedResourceUrlForLegacy directive, which accepts resource
37+
* URIs like script src and blesses them as TrustedResourceUri.
38+
*
39+
* <p>
40+
* Note that this directive is not autoescape cancelling, and can thus be used in strict templates.
41+
* The directive returns its result as an object of type SoyValue.
42+
*/
43+
@Singleton
44+
@SoyPurePrintDirective
45+
final class BlessStringAsTrustedResourceUrlForLegacyDirective implements SoyJavaPrintDirective,
46+
SoyJsSrcPrintDirective, SoyPySrcPrintDirective {
47+
48+
private static final Set<Integer> VALID_ARGS_SIZES = ImmutableSet.of(0);
49+
50+
@Inject
51+
public BlessStringAsTrustedResourceUrlForLegacyDirective() {}
52+
53+
@Override public String getName() {
54+
return "|blessStringAsTrustedResourceUrlForLegacy";
55+
}
56+
57+
@Override public final Set<Integer> getValidArgsSizes() {
58+
return VALID_ARGS_SIZES;
59+
}
60+
61+
@Override public boolean shouldCancelAutoescape() {
62+
return false;
63+
}
64+
65+
@Override public SoyValue applyForJava(SoyValue value, List<SoyValue> args) {
66+
return Sanitizers.blessStringAsTrustedResourceUrlForLegacy(value);
67+
}
68+
69+
@Override public JsExpr applyForJsSrc(JsExpr value, List<JsExpr> args) {
70+
return new JsExpr("soy.$$blessStringAsTrustedResourceUrlForLegacy(" + value.getText() + ")",
71+
Integer.MAX_VALUE);
72+
}
73+
74+
@Override public PyExpr applyForPySrc(PyExpr value, List<PyExpr> args) {
75+
return new PyExpr(
76+
"sanitize.bless_string_as_trusted_resource_url_for_legacy(" + value.getText() + ")",
77+
Integer.MAX_VALUE);
78+
}
79+
80+
}

java/src/com/google/template/soy/basicdirectives/FilterTrustedResourceUriDirective.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
*/
4343
@Singleton
4444
@SoyPurePrintDirective
45-
public class FilterTrustedResourceUriDirective implements SoyJavaPrintDirective,
46-
SoyJsSrcPrintDirective, SoyPySrcPrintDirective{
45+
final class FilterTrustedResourceUriDirective implements SoyJavaPrintDirective,
46+
SoyJsSrcPrintDirective, SoyPySrcPrintDirective {
4747

4848
private static final Set<Integer> VALID_ARGS_SIZES = ImmutableSet.of(0);
4949

java/src/com/google/template/soy/parsepasses/contextautoesc/EscapingMode.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,14 @@ public enum EscapingMode {
132132
* Makes sure there URIs are trusted and not input variables. Currently used only for script
133133
* sources.
134134
*/
135-
FILTER_TRUSTED_RESOURCE_URI(false, ContentKind.TRUSTED_RESOURCE_URI),
135+
// TODO(shwetakarwa): Change second argument when function is implemented.
136+
FILTER_TRUSTED_RESOURCE_URI(false, null),
137+
138+
/**
139+
* Makes sure that legacy resource URI are not filtered for being not marked as trusted.
140+
*/
141+
// TODO(shwetakarwa): Change second argument when function is implemented.
142+
BLESS_STRING_AS_TRUSTED_RESOURCE_URL_FOR_LEGACY(false, null),
136143

137144
/**
138145
* The explicit rejection of escaping.
@@ -154,7 +161,7 @@ public enum EscapingMode {
154161
public final boolean isHtmlEmbeddable;
155162

156163
/** The kind of content produced by the escaping directive associated with this escaping mode. */
157-
public final @Nullable ContentKind contentKind;
164+
@Nullable public final ContentKind contentKind;
158165

159166
/** Whether this directive is only for internal use by the contextual autoescaper. */
160167
public final boolean isInternalOnly;
@@ -175,7 +182,7 @@ public enum EscapingMode {
175182
/**
176183
* The escaping mode corresponding to the given directive or null.
177184
*/
178-
public static @Nullable EscapingMode fromDirective(String directiveName) {
185+
@Nullable public static EscapingMode fromDirective(String directiveName) {
179186
return DIRECTIVE_TO_ESCAPING_MODE.get(directiveName);
180187
}
181188

java/src/com/google/template/soy/shared/restricted/Sanitizers.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,8 @@ public static String filterNormalizeMediaUri(String value) {
454454

455455

456456
/**
457-
* This is supposed to make sure the the given input is an instance of either trustedResourceUrl
458-
* or trustedString. But for now only calls filterNormalizeUri.
457+
* This is supposed to make sure the given input is an instance of either trustedResourceUrl
458+
* or trustedString. But for now only return the value coerced to string.
459459
*/
460460
public static SoyValue filterTrustedResourceUri(SoyValue value) {
461461
// TODO(shwetakarwa): This needs to be changed once all the legacy URLs are taken care of.
@@ -464,15 +464,35 @@ public static SoyValue filterTrustedResourceUri(SoyValue value) {
464464

465465

466466
/**
467-
* Makes sure that the given input doesn't specify a dangerous protocol and also
468-
* {@link #normalizeUri normalizes} it.
467+
* For string inputs this function just returns the input string itself change to SoyValue.
469468
*/
470469
public static SoyValue filterTrustedResourceUri(String value) {
471470
// TODO(shwetakarwa): This needs to be changed once all the legacy URLs are taken care of. Will
472471
// probably need to return string.
473472
return StringData.forValue(value);
474473
}
475474

475+
/**
476+
* For any resource string/variable which has
477+
* |blessStringAsTrustedResuorceUrlForLegacy directive unsafely changes it to
478+
* sanitizedContent of kind TRUSTED_RESOURCE_URI.
479+
*/
480+
public static SoyValue blessStringAsTrustedResourceUrlForLegacy(SoyValue value) {
481+
// TODO(shwetakarwa): Implement this while implementing filterTrustedResourceUri.
482+
return value;
483+
}
484+
485+
486+
/**
487+
* For any resource string/variable which has
488+
* |blessStringAsTrustedResuorceUrlForLegacy directive unsafely changes it to
489+
* sanitizedContent of kind TRUSTED_RESOURCE_URI.
490+
*/
491+
public static SoyValue blessStringAsTrustedResourceUrlForLegacy(String value) {
492+
// TODO(shwetakarwa): Implement this while implementing filterTrustedResourceUri.
493+
return StringData.forValue(value);
494+
}
495+
476496

477497
/**
478498
* Makes sure that the given input is a data URI corresponding to an image.

javascript/soyutils_usegoog.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,12 @@ soy.$$filterNormalizeUri = function(value) {
15081508
goog.asserts.assert(value.constructor === soydata.SanitizedUri);
15091509
return soy.$$normalizeUri(value);
15101510
}
1511+
if (soydata.isContentKind(value,
1512+
soydata.SanitizedContentKind.TRUSTED_RESOURCE_URI)) {
1513+
goog.asserts.assert(
1514+
value.constructor === soydata.SanitizedTrustedResourceUri);
1515+
return soy.$$normalizeUri(value);
1516+
}
15111517
if (value instanceof goog.html.SafeUrl) {
15121518
return soy.$$normalizeUri(goog.html.SafeUrl.unwrap(value));
15131519
}
@@ -1527,10 +1533,18 @@ soy.$$filterNormalizeUri = function(value) {
15271533
*/
15281534
soy.$$filterNormalizeMediaUri = function(value) {
15291535
// Image URIs are filtered strictly more loosely than other types of URIs.
1536+
// TODO(shwetakarwa): Add tests for this in soyutils_test_helper while adding
1537+
// tests for filterTrustedResourceUri.
15301538
if (soydata.isContentKind(value, soydata.SanitizedContentKind.URI)) {
15311539
goog.asserts.assert(value.constructor === soydata.SanitizedUri);
15321540
return soy.$$normalizeUri(value);
15331541
}
1542+
if (soydata.isContentKind(value,
1543+
soydata.SanitizedContentKind.TRUSTED_RESOURCE_URI)) {
1544+
goog.asserts.assert(
1545+
value.constructor === soydata.SanitizedTrustedResourceUri);
1546+
return soy.$$normalizeUri(value);
1547+
}
15341548
if (value instanceof goog.html.SafeUrl) {
15351549
return soy.$$normalizeUri(goog.html.SafeUrl.unwrap(value));
15361550
}
@@ -1544,8 +1558,7 @@ soy.$$filterNormalizeMediaUri = function(value) {
15441558
/**
15451559
* Vets a URI for usage as a resource.
15461560
*
1547-
* @param {*} value The value to filter. Might not be a string, but the value
1548-
* will be coerced to a string.
1561+
* @param {*} value The value to filter.
15491562
* @return {*} current just the value.
15501563
*/
15511564
soy.$$filterTrustedResourceUri = function(value) {
@@ -1557,6 +1570,21 @@ soy.$$filterTrustedResourceUri = function(value) {
15571570
};
15581571

15591572

1573+
/**
1574+
* For any resource string/variable which has
1575+
* |blessStringAsTrustedResuorceUrlForLegacy directive unsafely change it to
1576+
* sanitizedContent of kind TRUSTED_RESOURCE_URI.
1577+
*
1578+
* @param {*} value The value to be blessed. Might not be a string
1579+
* @return {*} current just the value.
1580+
*/
1581+
soy.$$blessStringAsTrustedResourceUrlForLegacy = function(value) {
1582+
// TODO(shwetakarwa): Implement this while implementing
1583+
// filterTrustedResourceUri.
1584+
return value;
1585+
};
1586+
1587+
15601588
/**
15611589
* Allows only data-protocol image URI's.
15621590
*

python/sanitize.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ def filter_trusted_resource_uri(value):
251251
return value
252252

253253

254+
def bless_string_as_trusted_resource_url_for_legacy(value):
255+
# TODO(shwetakarwa): Change this to sanitized content of kind
256+
# TRUSTED_RESOURCE_URI.
257+
return value
258+
259+
254260
def normalize_html(value):
255261
return generated_sanitize.normalize_html_helper(value)
256262

testdata/javascript/soy_usegoog_lib.js

Lines changed: 33 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)