Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deny/ban the use of certain external functions

Tags:

rust

I have a rust project which multiple people work on. There are certain problematic functions defined by external crates we use, that is the source of a lot of confusion and errors. I want to totally deny/ban the use of these functions crate-wide at compile time. Is there any way to do this?

like image 863
pbsds Avatar asked Oct 07 '21 16:10

pbsds


People also ask

How do I use external functions in a script?

You can use external functions that are written in any language that supports dynamic libraries. Before you can use an external function in a script, you must declare it as one of two types: These are available anywhere in the application. These are defined for a particular type of window, menu, user object, or user-defined function.

What are the different types of external functions?

You can declare two types of external functions: Global external functions, which are available anywhere in the application Local external functions, which are defined for a particular type of window, menu, or user object These functions are part of the object's definition and can always be used in scripts for the object itself.

What do you need to know before calling an external function?

Before an external function can be called the first time, an administrator must do some configuration work. This work requires knowledge of the cloud platform (e.g. AWS or Microsoft Azure), especially about security.

What are the security risks of external functions?

External functions can raise additional security issues. For example, if you call a third party’s function, that party could keep copies of the data passed to the function.


Video Answer


1 Answers

You can use the disallowed_method Clippy lint to accomplish this. (There's also disallowed_type for types.)

For example, to disallow Box::new:

#![deny(clippy::disallowed_method)]

fn main() {
    let my_box = Box::new(123);
}

Add this to clippy.toml in the workspace root:

disallowed-methods = [
    # fully qualified function/method name:
    "std::boxed::Box::new",
]

When you then run cargo clippy, you will get an error about the usage of the disallowed function:

error: use of a disallowed method `alloc::boxed::Box::new`
 --> src/main.rs:4:16
  |
4 |   let my_box = Box::new(123);
  |                ^^^^^^^^^^^^^
  |
like image 141
Frxstrem Avatar answered Oct 21 '22 15:10

Frxstrem