Skip to content

Add scuttler prop instead of fixed Snow integration#475

Merged
weizman merged 11 commits into
snow-scuttling-integrationfrom
snow-scuttling-integration-propose-scuttler
May 24, 2023
Merged

Add scuttler prop instead of fixed Snow integration#475
weizman merged 11 commits into
snow-scuttling-integrationfrom
snow-scuttling-integration-propose-scuttler

Conversation

@weizman

@weizman weizman commented Mar 26, 2023

Copy link
Copy Markdown
Member

Instead of recursive pass scuttler which will be a string for a reference on the global object for a scuttler function.
Also, a demo of how to use this option to scuttle all realms using snow.

TODO

  • make sure its clear this is experimental
  • document this experimental option
  • make sure node env can't pass this option (irrelevant in node)

@weizman weizman force-pushed the snow-scuttling-integration-propose-scuttler branch from a8d8176 to d07f04d Compare March 26, 2023 13:13
Comment thread packages/core/src/kernelCoreTemplate.js Outdated
}
snow(realm => performScuttleGlobalThis(realm, scuttleGlobalThisExceptions), globalRef)
scuttler(globalRef, {
scuttle: realm => performScuttleGlobalThis(realm, scuttleGlobalThisExceptions),

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.

previously we had (cb, realm); here we have (realm, {scuttle:cb}).

I would prefer a more typical (realm, cb) and do away with wrapping the function in an object. If we ever need to introduce more extensibility, I would see options as a separate argument, like (realm, cb, {})

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.

(cb, realm) was proposed originally because the only "option" was to use snow, which accepts (cb, realm) because cb is required while realm is optional.
Now that we go with a more generic option that just snow, it makes sense to go by standard which is first realm and cb after.

I do agree with (realm, cb, {}) however, that's a fair suggestion.

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.

(You can see what I mean btw by visiting packages/browserify/examples/01-simple-js/index.html:5 on this PR)

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.

Comment thread packages/core/src/kernelCoreTemplate.js Outdated
enabled: scuttleGlobalThisEnabled,
exceptions: scuttleGlobalThisExceptions,
recursive: scuttleGlobalThisRecursive,
scuttler: scuttleGlobalThisScuttler,

@legobeat legobeat Mar 27, 2023

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.

TBH I find this block (:79-83) a bit terse and tricky to parse and my eyes keep jumping back and forth especially now that scuttleGlobalThisScuttler is a field that is not "the final word" in this scope anymore.

I'd personally find it more readable to have a local options hash instead of local variables for each option.

Something like:

const scuttleOpts = scuttleGlobalThis === true
  ? scuttleGlobalThisDefaults
  : {
    ...scuttleGlobalThis,
    _scuttler: scuttleGlobalThis?.scuttler && globalRef[scuttleGlobalThis.scuttler] || scuttleGlobalThisDefaults._scuttler
  }

And then in the following local scope we would refer to e.g. scuttleOpts.enabled instead of scuttleGlobalThisEnabled. We would also be calling scuttleOpts._scuttler, here prefixed with an underscore to distinguish the function-referencing key to the name-string referencing one in the options.

Might just be that I'm not fully accustomed to the new syntax here, just noting.

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.

I think it's really a matter of taste. I'm mostly trying to remain as align as possible with the rest of the codebase previous choices.
You can lookup } = in the codebase - this style is all over pretty much.

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.

Sure, I guess my main point is that with the changes in this PR, to answer the question "what is the effective scuttler", the reader will need walk up through lines 93,85,83,77,63 and make the connection that the falsiness of the empty string in defaults changes the outcome in the default scenario. (or walk down and keep this in mind at :87).

In the proposed alternative above, the logic for the specific field is kept at the same location.

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.

I accepted parts of your suggestion (left the part where an exception is thrown if a scuttler was specified but was not found) 6fe045a

@weizman weizman marked this pull request as ready for review March 27, 2023 12:07
@leotm

leotm commented Mar 27, 2023

Copy link
Copy Markdown
Member

i wasn't able to test our example at first ^ so updated our examples separately as follow-up in

(both console errors from before unrelated to scuttler so looks good)

@leotm

This comment was marked as duplicate.

@weizman weizman requested a review from legobeat March 27, 2023 13:53
@weizman

weizman commented Mar 27, 2023

Copy link
Copy Markdown
Member Author

added support restriction for scuttler in node env e01281d

@weizman weizman changed the title [WIP] [Draft] Propose scuttler Add scuttler prop instead of fixed Snow integration Mar 28, 2023
@weizman

weizman commented Mar 28, 2023

Copy link
Copy Markdown
Member Author

added documentation PR #477

@leotm leotm left a comment

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.

lgtm, but would wait on @legobeat

Comment thread packages/browserify/examples/01-simple-js/index.html Outdated
Comment thread packages/browserify/examples/01-simple-js/build.js Outdated
Comment thread packages/core/src/kernelCoreTemplate.js Outdated
if (scuttleGlobalThisRecursive) {
if (!globalRef.SNOW) {

const scuttleOpts = scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis

@legobeat legobeat Mar 30, 2023

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.

This will still potentially modify element in the exceptions property of the supplied scuttleGlobalThis down at https://github.com/LavaMoat/LavaMoat/pull/475/files#diff-cbec1016c903fe52b8d76860ebf3400e172b83a3374b8796bab149b3721d8064R113.

Hence preferable to consolidate the instantion and transformations of options with a pattern like

const scuttleOpts = {
  // [...]
  scuttlerFunc: scuttleGlobalThis?.scuttler ? ///....,
  exceptions: scuttleGlobalThis?.exceptions?.map(e => scuttleExceptionToRegexp(e)) || scuttleGlobalThisDefaults.exceptions,
  
}

(the same input validation can still be done, just moved to happen before (or if there is reason to do so, after) the creation of scuttleOpts)

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.

Comment thread packages/lavapack/src/runtime.js Outdated
if (scuttleGlobalThisRecursive) {
if (!globalRef.SNOW) {

const scuttleOpts = scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis

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.

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.

Comment thread packages/core/src/kernelCoreTemplate.js Outdated
// turn scuttleGlobalThis.exceptions regexes strings to actual regexes
for (let i = 0; i < scuttleGlobalThisExceptions.length; i++) {
const prop = scuttleGlobalThisExceptions[i]
for (let i = 0; i < scuttleOpts.exceptions.length; i++) {

@kumavis kumavis Mar 30, 2023

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.

for (const prop of ...)

Comment thread packages/core/src/kernelCoreTemplate.js Outdated
enabled: true,
exceptions: [],
recursive: false,
scuttler: '',

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.

weird default

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.

this prop is expected to be a string, this is the default value of type:string (thought it would make sense to stick to one type if possible)

Comment thread packages/node/src/kernel.js Outdated

function createKernel ({ projectRoot, lavamoatPolicy, canonicalNameMap, debugMode, statsMode, scuttleGlobalThis, scuttleGlobalThisExceptions }) {
if (scuttleGlobalThis?.scuttler) {
throw new Error('Lavamoat - "scuttleGlobalThis.scuttler" option is not supported for node environment.')

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.

why not?

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.

  • we decided to allow using "scuttlerName" config option in node env
  • since it's experimental, we decided to allow using it similar to how it's used for browser envs (by defining a prop on globalRef)
  • later on we'd like to allow passing an actual reference instead of a string prop
  • fix 6bd2c52

Comment thread packages/browserify/examples/01-simple-js/index.html Outdated
Comment thread packages/lavapack/src/runtime.js Outdated
}
snow = globalRef.SNOW
scuttleOpts.scuttlerFunc = globalRef[scuttleOpts.scuttler]
}

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.

maybe should create a configuration obj that holds the scuttlerFunc instead of mutating the options obj

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.

Comment thread packages/lavapack/src/runtime.js Outdated
enabled: true,
exceptions: [],
recursive: false,
scuttler: '',

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.

should rename to scuttlerName or something

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.

weizman and others added 2 commits April 19, 2023 14:06
@weizman weizman force-pushed the snow-scuttling-integration-propose-scuttler branch from 3cec4e1 to 419e2a6 Compare April 19, 2023 12:53
Comment thread packages/core/src/kernelCoreTemplate.js Outdated
}

const scuttleOpts = scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis
const scuttleOpts = Object.assign({}, scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis)

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.

Why not use the pattern as here instead?

This isn't a deep copy, so for example the input scuttleOpts.exceptions still get mutated here.

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.

you're right, my bad, thank you

#482

Comment thread packages/lavapack/src/runtime.js Outdated
}

const scuttleOpts = scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis
const scuttleOpts = Object.assign({}, scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis)

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.

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.

you're right, my bad, thank you

#482

@weizman weizman merged commit 66e156f into snow-scuttling-integration May 24, 2023
@weizman weizman deleted the snow-scuttling-integration-propose-scuttler branch May 24, 2023 09:03
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.

4 participants