@@ -51,6 +51,21 @@ func IgnoreModule(module string) CompileOption {
51
51
}
52
52
}
53
53
54
+ // BanModule is an option for [NewCompiler] and [Compile] that allows
55
+ // banning the use of a given module.
56
+ //
57
+ // Import statements for the banned module will cause an error. The error
58
+ // message can be customized by using the given error title and message.
59
+ //
60
+ // If this function is called multiple times with the same module name,
61
+ // the error title and message will be updated.
62
+ func BanModule (module string , errTitle string , errMessage string ) CompileOption {
63
+ return func (c * Compiler ) error {
64
+ c .bannedModules [module ] = bannedModule {errTitle , errMessage }
65
+ return nil
66
+ }
67
+ }
68
+
54
69
// RelaxedReSyntax is an option for [NewCompiler] and [Compile] that
55
70
// determines whether the compiler should adopt a more relaxed approach
56
71
// while parsing regular expressions.
@@ -104,7 +119,7 @@ type SourceOption func(opt *sourceOptions) error
104
119
// The origin is usually the path of the file containing the source code,
105
120
// but it can be any arbitrary string that conveys information of the
106
121
// source's origin. This origin appears in error reports, for instance, if
107
- // if origin is "some_file.yar", error reports will look like:
122
+ // origin is "some_file.yar", error reports will look like:
108
123
//
109
124
// error: syntax error
110
125
// --> some_file.yar:4:17
@@ -194,20 +209,27 @@ func (c CompileError) Error() string {
194
209
return c .Text
195
210
}
196
211
212
+ type bannedModule struct {
213
+ errTitle string
214
+ errMsg string
215
+ }
216
+
197
217
// Compiler represent a YARA compiler.
198
218
type Compiler struct {
199
219
cCompiler * C.YRX_COMPILER
200
220
relaxedReSyntax bool
201
221
errorOnSlowPattern bool
202
222
errorOnSlowLoop bool
203
223
ignoredModules map [string ]bool
224
+ bannedModules map [string ]bannedModule
204
225
vars map [string ]interface {}
205
226
}
206
227
207
228
// NewCompiler creates a new compiler.
208
229
func NewCompiler (opts ... CompileOption ) (* Compiler , error ) {
209
230
c := & Compiler {
210
231
ignoredModules : make (map [string ]bool ),
232
+ bannedModules : make (map [string ]bannedModule ),
211
233
vars : make (map [string ]interface {}),
212
234
}
213
235
@@ -244,6 +266,9 @@ func (c *Compiler) initialize() error {
244
266
for name , _ := range c .ignoredModules {
245
267
c .ignoreModule (name )
246
268
}
269
+ for name , v := range c .bannedModules {
270
+ c .banModule (name , v .errTitle , v .errMsg )
271
+ }
247
272
for ident , value := range c .vars {
248
273
if err := c .DefineGlobal (ident , value ); err != nil {
249
274
return err
@@ -325,6 +350,23 @@ func (c *Compiler) ignoreModule(module string) {
325
350
runtime .KeepAlive (c )
326
351
}
327
352
353
+ func (c * Compiler ) banModule (module , error_title , error_message string ) {
354
+ cModule := C .CString (module )
355
+ defer C .free (unsafe .Pointer (cModule ))
356
+
357
+ cErrTitle := C .CString (error_title )
358
+ defer C .free (unsafe .Pointer (cErrTitle ))
359
+
360
+ cErrMsg := C .CString (error_message )
361
+ defer C .free (unsafe .Pointer (cErrMsg ))
362
+
363
+ result := C .yrx_compiler_ban_module (c .cCompiler , cModule , cErrTitle , cErrMsg )
364
+ if result != C .SUCCESS {
365
+ panic ("yrx_compiler_add_unsupported_module failed" )
366
+ }
367
+ runtime .KeepAlive (c )
368
+ }
369
+
328
370
// NewNamespace creates a new namespace.
329
371
//
330
372
// Later calls to [Compiler.AddSource] will put the rules under the newly created
0 commit comments