Skip to content

Add per-build type LavaMoat policies#12702

Merged
rekmarks merged 6 commits intodevelopfrom
lavamoat-multiple-policies
Nov 15, 2021
Merged

Add per-build type LavaMoat policies#12702
rekmarks merged 6 commits intodevelopfrom
lavamoat-multiple-policies

Conversation

@rekmarks
Copy link
Copy Markdown
Member

@rekmarks rekmarks commented Nov 14, 2021

This PR adds one LavaMoat background script policy or each build type. It also renames the build system policy directory from node to build-system to make its purpose more clear. Each build type has the original policy-override.json for main builds.

We need to maintain separate policies for each build type because each type will produce different bundles with different internal and external modules.

In addition to the LavaMoat-related changes, this PR updates the lint scripts to no longer lint the auto-generated LavaMoat policy files. This should save off a significant amount of time from our JSON linting (which we do with prettier directly).

Closes #12690

@rekmarks rekmarks requested a review from kumavis November 14, 2021 22:24
@rekmarks rekmarks marked this pull request as ready for review November 14, 2021 22:26
@rekmarks rekmarks requested a review from a team as a code owner November 14, 2021 22:26
Comment on lines +7 to +12
# Generate LavaMoat policies for the extension background script for each build
# type.
concurrently --kill-others-on-fail -n main,beta,flask \
"WRITE_AUTO_POLICY=1 yarn dist" \
"WRITE_AUTO_POLICY=1 yarn dist --build-type beta" \
"WRITE_AUTO_POLICY=1 yarn dist --build-type flask"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note: Your device may board the Struggle Bus when running this locally.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

load testing!

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [e7876fb]
Page Load Metrics (730 ± 14 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint28478136615876
domContentLoaded6998187293014
load7008207303014
domInteractive6998187293014

@kumavis
Copy link
Copy Markdown
Member

kumavis commented Nov 14, 2021

this sounds like hell for the security engineers charged with reviewing policies and policy changes
but i dont have a better solution on deck

kumavis
kumavis previously approved these changes Nov 14, 2021
Copy link
Copy Markdown
Member

@kumavis kumavis left a comment

Choose a reason for hiding this comment

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

so be it

@rekmarks rekmarks requested a review from Gudahtt November 15, 2021 00:01
README.md Outdated
* Unfortunately, `yarn lavamoat:auto` will behave inconsistently on different platforms. macOS and Windows users may see extraneous changes relating to optional dependencies.
* The build system LavaMoat policy file (`lavamoat/build-system/policy.json`)
* Run `yarn lavamoat:build:auto` to re-generate this policy file.
* This only applies if the external modules imported by the build system or its dependencies have changed. Keep in mind that some modules may be imported dynamically, and may elude LavaMoat's static analysis. If this occurs, debug the build system with [`ndb`](https://www.npmjs.com/package/ndb) to determine which packages are dynamically imported, add them to the "useless" imports in `development/build/index.js`, and re-run the auto policy script.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This advice seems... overly specific? Dynamic imports aren't the only Lavamoat-related problem people are likely to run up against, nor is it specific to the build system. Other problems include dynamic global usage, attempts to mutate frozen globals, and incorrect policy generation due to multiple copies of a dependency being in the dependency tree.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, fair enough! Updated here: bb28367

# type.
# ATTN: This may tax your device when running it locally.
concurrently --kill-others-on-fail -n main,beta,flask \
"WRITE_AUTO_POLICY=1 yarn dist" \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this safe to run concurrently? All three of these builds will write to the dist directory, so I'd expect these to be overwriting each other.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm. For the purposes of validating the LavaMoat config, it doesn't matter what's in the dist directory or whether files are being overwritten. The build processes just need to complete normally. We could really use a --no-emit option, actually.

My question is whether this could interfere with any of the other build scripts we run, if they also write or read from dist?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My concern was that LavaMoat itself might be generating the policy files from-disk. So the version of the build it was generating a policy for might be reliant on this race condition of which build finishes when.

But on second thought... that's not how any of this works. LavaMoat runs on the JavaScript while it's still in stream form, before it gets written to disk. At least I'm pretty sure. So yeah you're probably right.

And I agree, a --no-emit option would be useful here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

im not sure if:

  • clean is safe (removes and creates the dir)
  • inpage/contentscript is safe (creates the inpage bundle and puts it in the contentscript bundle)
    but if it works it works

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
Gudahtt
Gudahtt previously approved these changes Nov 15, 2021
Copy link
Copy Markdown
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM! Though regarding that README comment, I think that could be improved by moving that debugging advice to a separate bullet point or section. So that it can be expanded later, and inclusive of more than just the build system policy.

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [bb28367]
Page Load Metrics (744 ± 19 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint28476539517584
domContentLoaded7088977433919
load7098997443919
domInteractive7088977433919

Copy link
Copy Markdown
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

@rekmarks rekmarks merged commit d4c71b8 into develop Nov 15, 2021
@rekmarks rekmarks deleted the lavamoat-multiple-policies branch November 15, 2021 22:23
@github-actions github-actions bot locked and limited conversation to collaborators Nov 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create and use separate LavaMoat policies for each build type

4 participants