Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7265: Move Nunjucks template validation to sandbox #7426

Merged
merged 12 commits into from Jan 26, 2024

Conversation

fregante
Copy link
Collaborator

@fregante fregante commented Jan 25, 2024

What does this PR do?

The validation successfully goes through the sandbox, but it requires changes to the analysis in order to accept async validation. I think that's a larger task that can be done separately from this PR.

Tasks

  • Move nunjucks validation to sandbox
  • Ensure it works outside sandbox
  • Ensure it works inside sandbox

Test strings

Future work

  • Catch and reword null reference error?

Checklist

  • Add tests
  • New files added to src/tsconfig.strictNullChecks.json (if possible)
  • Designate a primary reviewer: @grahamlangford

@fregante fregante changed the title Move Nunjucks template validation to sandbox #7265: Move Nunjucks template validation to sandbox Jan 25, 2024
@twschiller
Copy link
Contributor

@fregante see comment here on how we currently handle async methods in analyses: #7265 (comment)

Base automatically changed from F/mv3/reliably-worker to main January 25, 2024 15:31
@twschiller
Copy link
Contributor

Tagging @grahamlangford and @fungairino as reviewers for awareness around approach/discussion: #7426 (comment)

Copy link

codecov bot commented Jan 26, 2024

Codecov Report

Attention: 7 lines in your changes are missing coverage. Please review.

Comparison is base (cf080f4) 72.61% compared to head (0d845fe) 72.61%.
Report is 1 commits behind head on main.

Files Patch % Lines
src/sandbox/messenger/api.ts 0.00% 4 Missing ⚠️
src/sandbox/messenger/executor.ts 88.88% 1 Missing ⚠️
src/sandbox/messenger/registration.ts 0.00% 1 Missing ⚠️
src/starterBricks/contextMenu.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7426   +/-   ##
=======================================
  Coverage   72.61%   72.61%           
=======================================
  Files        1211     1211           
  Lines       37867    37886   +19     
  Branches     7117     7122    +5     
=======================================
+ Hits        27497    27511   +14     
- Misses      10370    10375    +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator Author

@fregante fregante left a comment

Choose a reason for hiding this comment

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

It's ready!

Tested with and without sandbox

origins: [parsedURL.href],
})
// eslint-disable-next-line promise/prefer-await-to-then -- need the complete Promise
.then((hasPermission) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nit, before:

// eslint-disable-next-line promise/prefer-await-to-then -- need the complete Promise
const promise = x().then(y => {
  z(y);
});

After:

const promise = (async () => {
  const y = await x();
  z(y);
})();

While a bit more verbose, it's easier to deal with linear await code than then/catch pipelines, which is the reason behind promise/prefer-await-to-then. Once the eslint-disable-next-line comment is factored in, it's actually shorter.

Another example

// Decorate the extension promise with tour tracking
const runPromise = promise
// eslint-disable-next-line promise/prefer-await-to-then -- avoid separate method
.then(() => {
markTourEnd(nonce, { context });
})
// eslint-disable-next-line promise/prefer-await-to-then -- avoid separate method
.catch((error) => {
markTourEnd(nonce, { error, context });
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed. I wonder what the thought process was behind using the promise callback in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know a good reason off the top of my head. In some places we used to use it because microtasks threw off browser user gesture recognition, but that wouldn't apply here

@@ -41,6 +42,10 @@ type PushAnnotationArgs = {
};

class TemplateAnalysis extends PipelineExpressionVisitor implements Analysis {
// XXX: for now we handle asynchronous pipeline traversal by gathering all the promises and awaiting them all
// see discussion https://github.com/pixiebrix/pixiebrix-extension/pull/4013#discussion_r944690969
private readonly nunjuckValidationPromises: Array<Promise<void>> = [];
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment copy-pasted from existing code

src/analysis/analysisVisitors/templateAnalysis.ts Outdated Show resolved Hide resolved
@@ -263,7 +263,7 @@ export function removeExtensions(extensionIds: UUID[]): void {
console.debug("sidebarController:removeExtensions", { extensionIds });

// `panels` is const, so replace the contents
const current = panels.splice(0, panels.length);
const current = panels.splice(0);
Copy link
Collaborator Author

@fregante fregante Jan 26, 2024

Choose a reason for hiding this comment

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

Nit: This is the default

I'm not a fan of splice in general so I'd be open to adding a cloneAndClearArray utility to make the intent clear.

Suggested change
const current = panels.splice(0);
const current = cloneAndClearArray(panels);

url: expression,
},
}),
];
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I copied this updated test format from requestPermissionAnalysis.test.ts

Copy link

No loom links were found in the first post. Please add one there if you'd like to it to appear on Slack.

Do not edit this comment manually.

@twschiller twschiller merged commit aa7f675 into main Jan 26, 2024
13 checks passed
@twschiller twschiller deleted the F/mv3/nunjucks-validation branch January 26, 2024 23:50
@grahamlangford grahamlangford added this to the 1.8.8 milestone Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CSP error from Page Editor analysis rule in MV3
3 participants