Skip to content

perf: use predefined function for context check policies #959

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

Merged
merged 4 commits into from
Jan 11, 2025

Conversation

Natoandro
Copy link
Contributor

@Natoandro Natoandro commented Jan 11, 2025

Improve performance by running the functions in the main thread when it can be done securely:

  • Use predefined function for context check policies
  • Use context check policy for admin_only

Migration notes


  • The change comes with new or modified tests
  • Hard-to-understand functions have explanatory comments
  • End-user documentation is updated to reflect the change

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced predefined function handling with optional parameters.
    • Introduced a more flexible context checking mechanism.
    • Simplified policy definition for admin access.
  • Improvements

    • Updated runtime function registration process.
    • Improved type safety for predefined function validation.
    • Streamlined error handling for function materialization.
  • Changes

    • Removed deprecated error handling functions.
    • Modified internal representations of predefined functions.
    • Updated function signatures for predefined Deno functions.

Copy link
Contributor

coderabbitai bot commented Jan 11, 2025

📝 Walkthrough

Walkthrough

This pull request introduces significant changes to the handling of predefined functions and context checks across multiple files. The modifications focus on enhancing the flexibility and type safety of predefined functions by introducing new data structures like ContextCheckX and PredefinedFunctionMatData. The changes span several components, including runtime implementations, core libraries, and validation logic, with a particular emphasis on improving how Deno functions are defined, registered, and processed.

Changes

File Change Summary
src/common/src/typegraph/runtimes/deno.rs Added new enums ContextCheckX and PredefinedFunctionMatData, and struct PredefinedFunctionMatDataRaw with associated methods.
src/typegate/src/runtimes/deno/deno.ts Refactored predefined functions to return parameterized functions, added context_check function.
src/typegate/src/typegraphs/typegate.py Simplified admin_only policy definition using Policy.context().
src/typegraph/core/src/conversion/runtimes.rs Modified Predefined variant conversion to serialize entire predef object.
src/typegraph/core/src/errors.rs Removed unknown_predefined_function error handling method.
src/typegraph/core/src/global_store.rs Updated predefined_deno_functions to use PredefinedFunctionMatData as key.
src/typegraph/core/wit/typegraph.wit Added optional param to materializer-deno-predefined record.
src/typegraph/core/src/runtimes/deno.rs Updated DenoMaterializer enum to use PredefinedFunctionMatData.
src/typegraph/core/src/runtimes/mod.rs Updated get_predefined_deno_func function to include an additional param parameter.
src/typegraph/core/src/validation/errors.rs Removed unknown_predefined_function error handling method.
src/typegraph/core/src/validation/materializers.rs Updated validate_deno_mat function to utilize PredefinedFunctionMatData enum.
src/typegraph/deno/src/runtimes/deno.ts Minor formatting changes in fetchContext method.
src/typegraph/python/typegraph/runtimes/deno.py Updated identity method to include a param parameter in MaterializerDenoPredefined instantiation.
src/typegate/src/typegraphs/typegate.json Renamed function materializer to predefined_function and updated its data structure.

Suggested reviewers

  • zifeo
  • michael-0acf4
  • Yohe-Am

Possibly related PRs

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/typegraph/core/src/lib.rs (1)

234-235: Avoid including potentially sensitive debug information in error messages

Using format!("Error while serializing context check: {e:?}") may expose internal details. Consider using e's Display implementation or chaining the error for a cleaner message without revealing sensitive information.

Apply this diff to adjust the error message:

-            .map_err(|e| format!("Error while serializing context check: {e:?}"))?,
+            .map_err(|e| format!("Error while serializing context check: {}", e))?,
src/common/src/typegraph/runtimes/deno.rs (1)

52-52: Avoid including potentially sensitive debug information in error messages

Using anyhow!("invalid predefined function materializer: {e:?}") may expose internal details. Consider using e's Display implementation or chaining the error with .context() to avoid revealing sensitive information.

Apply this diff to adjust the error message:

-                .map_err(|e| anyhow!("invalid predefined function materializer: {e:?}"))
+                .map_err(|e| anyhow!("invalid predefined function materializer: {}", e))
src/typegraph/core/src/validation/materializers.rs (1)

31-72: Consider extracting error message mapping to a separate function

The match expression for error messages is repeated across multiple variants. Consider extracting this to a helper function to improve maintainability.

+fn get_predefined_function_name(predef: &PredefinedFunctionMatData) -> &'static str {
+    match predef {
+        P::True => "true",
+        P::False => "false",
+        P::Allow => "allow",
+        P::Deny => "deny",
+        P::Pass => "pass",
+        P::ContextCheck { .. } => "context_check",
+        P::InternalPolicy { .. } => "internal_policy",
+        _ => unreachable!(),
+    }
+}
src/typegate/src/runtimes/deno/deno.ts (1)

37-42: Consider using constants for policy outputs

The string literals "ALLOW", "DENY", "PASS" are used multiple times. Consider defining them as constants to prevent typos and improve maintainability.

+const POLICY_RESULT = {
+  ALLOW: "ALLOW",
+  DENY: "DENY",
+  PASS: "PASS",
+} as const;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0f2c8fa and de5c7c1.

📒 Files selected for processing (14)
  • src/common/src/typegraph/runtimes/deno.rs (2 hunks)
  • src/typegate/src/runtimes/deno/deno.ts (2 hunks)
  • src/typegate/src/typegraphs/typegate.py (2 hunks)
  • src/typegraph/core/src/conversion/runtimes.rs (1 hunks)
  • src/typegraph/core/src/errors.rs (0 hunks)
  • src/typegraph/core/src/global_store.rs (4 hunks)
  • src/typegraph/core/src/lib.rs (5 hunks)
  • src/typegraph/core/src/runtimes/deno.rs (2 hunks)
  • src/typegraph/core/src/runtimes/mod.rs (1 hunks)
  • src/typegraph/core/src/validation/errors.rs (0 hunks)
  • src/typegraph/core/src/validation/materializers.rs (2 hunks)
  • src/typegraph/core/wit/typegraph.wit (1 hunks)
  • src/typegraph/deno/src/runtimes/deno.ts (1 hunks)
  • src/typegraph/python/typegraph/runtimes/deno.py (1 hunks)
💤 Files with no reviewable changes (2)
  • src/typegraph/core/src/errors.rs
  • src/typegraph/core/src/validation/errors.rs
✅ Files skipped from review due to trivial changes (1)
  • src/typegraph/deno/src/runtimes/deno.ts
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: lint-compat (macos-14, aarch64-apple-darwin, false)
  • GitHub Check: lint-compat (macos-13, x86_64-apple-darwin, false)
  • GitHub Check: bulid-docker (linux/amd64, custom-ubuntu-large)
  • GitHub Check: test-full
  • GitHub Check: pre-commit
🔇 Additional comments (18)
src/typegraph/core/src/lib.rs (3)

55-65: Conversion between ContextCheck and ContextCheckX implemented effectively

The From trait implementation allows seamless conversion between ContextCheck and ContextCheckX, enhancing code clarity and type safety.


199-199: Update to use PredefinedFunctionMatData::InternalPolicy is appropriate

The change to DenoMaterializer::Predefined(PredefinedFunctionMatData::InternalPolicy) correctly reflects the updated structure and ensures consistency in handling internal policies.


225-229: Proper serialization of context check parameters

The context check is correctly converted and serialized into JSON, ensuring accurate data is passed to the materializer.

src/typegraph/core/src/runtimes/deno.rs (1)

30-30: Update DenoMaterializer::Predefined variant to use PredefinedFunctionMatData

The DenoMaterializer enum now uses PredefinedFunctionMatData, which is consistent with the new data structures and improves the representation of predefined functions.

src/common/src/typegraph/runtimes/deno.rs (2)

15-22: Definition of ContextCheckX enum is appropriate

The ContextCheckX enum is correctly defined with necessary variants and serialization attributes, facilitating proper handling of context checks.


24-36: Definition of PredefinedFunctionMatData enum is well-structured

The PredefinedFunctionMatData enum effectively encapsulates different types of predefined function materializer data, and the use of serde attributes ensures correct serialization and deserialization.

src/typegraph/core/src/validation/materializers.rs (1)

4-4: LGTM! Good use of type-safe enums

The introduction of PredefinedFunctionMatData enhances type safety by replacing string-based matching with enum variants.

src/typegraph/python/typegraph/runtimes/deno.py (1)

121-121: LGTM! Proper alignment with new parameter structure

The addition of param=None correctly aligns with the new predefined function parameter structure while maintaining backward compatibility.

src/typegate/src/runtimes/deno/deno.ts (2)

33-36: LGTM! Improved type definition

The type definition for predefinedFuncs correctly specifies that each function takes a parameter and returns a resolver.


45-68: Verify context access security

The context_check implementation accesses nested context properties using a dot-notation path. Ensure that:

  1. The path traversal is properly bounded
  2. Access to sensitive context properties is restricted
✅ Verification successful

Context access implementation is secure

The context_check implementation uses proper path traversal with optional chaining and follows secure patterns consistent with the rest of the codebase. The path is properly bounded and access to context properties is safely implemented.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other instances of context property access to verify consistent security patterns
rg -A 3 'context\??\.\[.*\]|context\??\.[\w.]+'

Length of output: 54504

src/typegate/src/typegraphs/typegate.py (1)

97-97: LGTM! Good simplification of policy definition

The change to use Policy.context simplifies the admin-only check implementation while potentially improving performance by avoiding runtime overhead.

src/typegraph/core/src/global_store.rs (4)

17-17: LGTM! Import of PredefinedFunctionMatData.

The import aligns with the PR's objective to use predefined functions for context checks.


59-59: LGTM! Updated HashMap type for predefined functions.

The change from HashMap<String, MaterializerId> to HashMap<PredefinedFunctionMatData, MaterializerId> improves type safety and allows for more structured data.


92-92: LGTM! Updated default materializer.

The change to use PredefinedFunctionMatData::Pass as the default materializer aligns with the new type system.


350-367: LGTM! Enhanced predefined function handling.

The updated get_predefined_deno_function method now:

  1. Accepts an optional parameter for flexible context checks
  2. Uses structured PredefinedFunctionMatData for better type safety
  3. Maintains the same caching behavior for performance
src/typegraph/core/src/conversion/runtimes.rs (1)

131-131: LGTM! Simplified predefined function data conversion.

The change improves the conversion logic by directly serializing the entire predef object instead of manually constructing a JSON object. This approach is more maintainable and type-safe.

src/typegraph/core/wit/typegraph.wit (1)

271-271: LGTM! Added optional parameter to predefined function interface.

The addition of param: option<string> to the materializer-deno-predefined record:

  1. Enhances flexibility in context check policies
  2. Maintains backward compatibility through optional parameter
  3. Aligns with the PR's objective
src/typegraph/core/src/runtimes/mod.rs (1)

298-298: LGTM! Updated predefined function handling.

The change correctly propagates the new param field to Store::get_predefined_deno_function, maintaining consistency with the interface changes.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/typegate/src/typegraphs/typegate.json (1)

810-824: Improved type safety with structured context check definition.

The change from a script-based approach to a structured definition enhances type safety and maintainability. The predefined function pattern with explicit key-value pairs makes the context check more predictable and easier to validate.

This structured approach:

  • Reduces runtime errors by moving validation to compile time
  • Makes context checks more maintainable and discoverable
  • Aligns with the PR's goal of secure main thread execution
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between de5c7c1 and 4a44422.

📒 Files selected for processing (1)
  • src/typegate/src/typegraphs/typegate.json (2 hunks)
🔇 Additional comments (1)
src/typegate/src/typegraphs/typegate.json (1)

Line range hint 810-951: Verify authentication configuration alignment.

The context check policy and authentication configuration appear to be correctly aligned (both use "admin" as the username), but let's verify there are no discrepancies in the authentication setup.

Run this script to check the authentication configuration across the codebase:

✅ Verification successful

Authentication configuration is properly aligned

The context check policy using "admin" username is consistent with the authentication configuration across all typegraph definitions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for authentication configuration patterns
rg -A 5 '"protocol":\s*"basic"' --type json

# Search for other potential username definitions
rg -A 3 '"users":\s*\[' --type json

Length of output: 1365

Yohe-Am
Yohe-Am previously approved these changes Jan 11, 2025
@Natoandro Natoandro dismissed stale reviews from Yohe-Am and luckasRanarison via 2856393 January 11, 2025 06:44
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/typegate/src/runtimes/deno/deno.ts (2)

58-58: Improve error message in default case.

The error message "unreachable" could be more descriptive to help with debugging.

-        throw new Error("unreachable");
+        throw new Error(`Unsupported context check type: ${value.type}`);

45-68: Enhance type safety and validation in context_check.

Consider adding the following improvements:

  1. Type validation for the value parameter
  2. Validation for the key format before splitting
-  context_check: ({ key, value }) => {
+  context_check: ({ key, value }: { key: string, value: { type: 'not_null' } | { type: 'value', value: unknown } | { type: 'pattern', value: string } }) => {
+    if (!key || typeof key !== 'string') {
+      throw new Error('Context check key must be a non-empty string');
+    }
     let check: (value: any) => boolean;
     switch (value.type) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4a44422 and 2856393.

📒 Files selected for processing (1)
  • src/typegate/src/runtimes/deno/deno.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: test-full
  • GitHub Check: lint-compat (macos-14, aarch64-apple-darwin, false)
  • GitHub Check: bulid-docker (linux/amd64, custom-ubuntu-large)
  • GitHub Check: lint-compat (macos-13, x86_64-apple-darwin, false)
  • GitHub Check: pre-commit
🔇 Additional comments (3)
src/typegate/src/runtimes/deno/deno.ts (3)

259-259: LGTM! Clean and consistent with the new predefined functions structure.

The update properly handles the new function signature by passing the parameters correctly.


33-68: Well-structured implementation that achieves the PR objectives.

The changes successfully implement:

  1. Parameterized predefined functions for improved flexibility
  2. A robust context check policy with proper type handling
  3. Consistent error handling and type safety

The implementation aligns well with the goal of improving function execution while maintaining security.


54-56: Verify regex pattern safety.

The RegExp creation with user-provided patterns could potentially lead to ReDoS (Regular Expression Denial of Service) attacks if not properly validated.

Consider adding pattern validation or timeout mechanisms. Let's check for existing pattern validations in the codebase:

✅ Verification successful

RegExp usage is properly safeguarded.

The pattern implementation is secure as it's part of a type-safe policy system with multiple validation layers. The patterns are validated during policy registration and type checking, with strict rules enforced at the type system level.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for pattern validation or regex safety measures
rg -A 3 'new RegExp|pattern.*validation' 

Length of output: 1902


Script:

#!/bin/bash
# Search for pattern-related type definitions and validations
rg -A 5 'type.*Pattern|interface.*Pattern|pattern.*validate|validatePattern'

# Search for pattern-related sanitization or security measures
ast-grep --pattern 'function $FUNC($_) {
  $$$
  pattern
  $$$
}'

Length of output: 146


Script:

#!/bin/bash
# Search for pattern-related type definitions and schema validation
rg -A 5 'pattern.*type|pattern.*schema|pattern.*check'

# Look for type definitions around the pattern case
ast-grep --pattern 'case "pattern":
  $$$'

Length of output: 4699

Copy link

codecov bot commented Jan 11, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 9 lines in your changes missing coverage. Please review.

Project coverage is 77.78%. Comparing base (3d8dac2) to head (2856393).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/typegate/src/runtimes/deno/deno.ts 79.41% 7 Missing ⚠️
src/typegraph/deno/src/runtimes/deno.ts 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #959      +/-   ##
==========================================
+ Coverage   77.72%   77.78%   +0.06%     
==========================================
  Files         154      154              
  Lines       19154    19044     -110     
  Branches     1930     1925       -5     
==========================================
- Hits        14888    14814      -74     
+ Misses       4243     4207      -36     
  Partials       23       23              

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

Copy link
Contributor

@michael-0acf4 michael-0acf4 left a comment

Choose a reason for hiding this comment

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

lgtm

@Natoandro Natoandro merged commit 445bb5d into main Jan 11, 2025
12 of 13 checks passed
@Natoandro Natoandro deleted the predefined-context-policy-2 branch January 11, 2025 09:21
Yohe-Am pushed a commit that referenced this pull request Jan 15, 2025
<!--
Pull requests are squashed and merged using:
- their title as the commit message
- their description as the commit body

Having a good title and description is important for the users to get
readable changelog.
-->

<!-- 1. Explain WHAT the change is about -->

Improve performance by running the functions in the main thread when it
can be done securely:
- Use predefined function for context check policies
- Use context check policy for `admin_only`

<!-- 2. Explain WHY the change cannot be made simpler -->



<!-- 3. Explain HOW users should update their code -->

#### Migration notes

---

- [ ] The change comes with new or modified tests
- [ ] Hard-to-understand functions have explanatory comments
- [ ] End-user documentation is updated to reflect the change


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Release Notes

- **New Features**
  - Enhanced predefined function handling with optional parameters.
  - Introduced a more flexible context checking mechanism.
  - Simplified policy definition for admin access.

- **Improvements**
  - Updated runtime function registration process.
  - Improved type safety for predefined function validation.
  - Streamlined error handling for function materialization.

- **Changes**
  - Removed deprecated error handling functions.
  - Modified internal representations of predefined functions.
  - Updated function signatures for predefined Deno functions.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@coderabbitai coderabbitai bot mentioned this pull request Jan 17, 2025
3 tasks
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