Skip to content

Commit eb93752

Browse files
committed
docs(py): document the allowed_xxxx functions recently added to Compiler
1 parent cd80efa commit eb93752

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

site/content/docs/api/python.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,72 @@ compiler.add_source("rule test { condition: false }")
246246
rules = compiler.build()
247247
```
248248

249+
#### .allowed_metadata(identifier, value_type, required=False, error=False, regexp=None)
250+
251+
Defines expectations for a specific metadata field.
252+
253+
When rules are compiled, the compiler will check if the metadata fields match the specified expectations. If not, it will trigger a warning (or an error if `error` is `True`).
254+
255+
- `identifier`: The metadata name (e.g., `"author"`).
256+
- `value_type`: The expected type, which must be one of the `yara_x.MetaType` constants:
257+
- `MetaType.STRING`
258+
- `MetaType.INTEGER`
259+
- `MetaType.FLOAT`
260+
- `MetaType.BOOL`
261+
- `MetaType.SHA256`
262+
- `MetaType.SHA1`
263+
- `MetaType.MD5`
264+
- `MetaType.HASH`
265+
- `required`: If `True`, the metadata field must be present in every rule. Defaults to `False`.
266+
- `error`: If `True`, failure to meet the expectation triggers an error instead of a warning. Defaults to `False`.
267+
- `regexp`: An optional regular expression that the metadata value must match. Only applicable if `value_type` is `MetaType.STRING`.
268+
269+
##### Example
270+
271+
```python
272+
compiler = yara_x.Compiler()
273+
compiler.allowed_metadata("author", yara_x.MetaType.STRING, required=True)
274+
compiler.allowed_metadata("version", yara_x.MetaType.STRING, regexp=r"^\d+\.\d+$")
275+
```
276+
277+
#### .allowed_rule_name(regexp, error=False)
278+
279+
Specifies a regular expression that the compiler will enforce upon each rule name.
280+
Any rule with a name that does not match this regular expression will trigger a warning.
281+
If `error` is `True`, it will trigger an error instead of a warning.
282+
283+
##### Example
284+
285+
```python
286+
compiler = yara_x.Compiler()
287+
compiler.allowed_rule_name("^test_")
288+
```
289+
290+
#### .allowed_tags(tags, error=False)
291+
292+
Sets a list of allowed tags. Any rule with a tag not present in this list will trigger a warning.
293+
If `error` is `True`, it will trigger an error instead of a warning.
294+
295+
##### Example
296+
297+
```python
298+
compiler = yara_x.Compiler()
299+
compiler.allowed_tags(["foo", "bar"])
300+
```
301+
302+
#### .allowed_tags_regex(regexp, error=False)
303+
304+
Specifies a regular expression that the compiler will enforce upon each tag.
305+
Any rule with a tag that does not match this regular expression will trigger a warning.
306+
If `error` is `True`, it will trigger an error instead of a warning.
307+
308+
##### Example
309+
310+
```python
311+
compiler = yara_x.Compiler()
312+
compiler.allowed_tags_regex("^[a-z]+$")
313+
```
314+
249315
#### .errors()
250316

251317
Returns the errors found during the compilation, across all calls to

site/hugo_stats.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@
349349
"add_include_dirpath",
350350
"add_sourcestring-originnone",
351351
"aligning-metadata-and-patterns",
352+
"allowed_metadataidentifier-value_type-requiredfalse-errorfalse-regexpnone",
353+
"allowed_rule_nameregexp-errorfalse",
354+
"allowed_tags_regexregexp-errorfalse",
355+
"allowed_tagstags-errorfalse",
352356
"alphabets-for-base64-modifiers",
353357
"alternatives",
354358
"anonymous-patterns",
@@ -447,7 +451,11 @@
447451
"example-15",
448452
"example-16",
449453
"example-17",
454+
"example-18",
455+
"example-19",
450456
"example-2",
457+
"example-20",
458+
"example-21",
451459
"example-3",
452460
"example-4",
453461
"example-5",
@@ -549,7 +557,9 @@
549557
"logmessage-boolean",
550558
"logmessage-float",
551559
"logmessage-integer",
560+
"logmessage-offset-length-new-in-version-v1150",
552561
"logmessage-string",
562+
"logoffset-length-new-in-version-v1150",
553563
"logstring",
554564
"machine",
555565
"mapitem",

0 commit comments

Comments
 (0)