Skip to content

Commit 886624c

Browse files
natebiggsCommit Queue
authored andcommitted
[dart2js] Add helper to detect csp in web tests and use it to set expectations for some tests.
Fixes these: https://dart-ci.firebaseapp.com/#showLatestFailures=true&configurations=dart2js-minified-csp-linux-chrome Change-Id: I2939e3ce1fdee45d570d038f2f4382747cd617dc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/436140 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Mayank Patke <[email protected]>
1 parent 9425a79 commit 886624c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

tests/web/csp_helper.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import "dart:js" as js;
6+
7+
final isCspEnabled = (() {
8+
try {
9+
js.context.callMethod('eval', ['5;']);
10+
return false;
11+
} catch (e) {
12+
return true;
13+
}
14+
})();

tests/web/dart2js/deferred_with_csp_nonce2_test.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import "package:expect/async_helper.dart";
1010
import "package:expect/expect.dart";
1111
import "dart:html";
1212

13+
import "../csp_helper.dart";
14+
1315
main() {
1416
asyncStart();
1517

@@ -31,7 +33,11 @@ main() {
3133
Expect.equals(1, scripts.length);
3234
for (var script in scripts) {
3335
Expect.equals("an-example-nonce-string", script.nonce);
34-
Expect.equals("an-example-nonce-string", script.getAttribute('nonce'));
36+
// nonce attribute not copied through to script when csp enabled.
37+
Expect.equals(
38+
isCspEnabled ? "" : "an-example-nonce-string",
39+
script.getAttribute('nonce'),
40+
);
3541
}
3642
asyncEnd();
3743
});

tests/web/dart2js/deferred_with_csp_nonce_test.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import "package:expect/async_helper.dart";
1010
import "package:expect/expect.dart";
1111
import "dart:html";
1212

13+
import "../csp_helper.dart";
14+
1315
main() {
1416
asyncStart();
1517

@@ -31,7 +33,11 @@ main() {
3133
Expect.equals(1, scripts.length);
3234
for (var script in scripts) {
3335
Expect.equals("an-example-nonce-string", script.nonce);
34-
Expect.equals("an-example-nonce-string", script.getAttribute('nonce'));
36+
// nonce attribute not copied through to script when csp enabled.
37+
Expect.equals(
38+
isCspEnabled ? "" : "an-example-nonce-string",
39+
script.getAttribute('nonce'),
40+
);
3541
}
3642
asyncEnd();
3743
});

0 commit comments

Comments
 (0)