mirror of
https://github.com/hoshikawa2/python_redaction.git
synced 2026-03-06 18:21:00 +00:00
adjustments
This commit is contained in:
87
Redaction.py
Normal file
87
Redaction.py
Normal file
@@ -0,0 +1,87 @@
|
||||
import re
|
||||
|
||||
# This class does not work with numeric attributes, only with String
|
||||
# Use the method redact and put your patterns as the example:
|
||||
# SENSITIVE_PATTERNS = [
|
||||
# r"\d{3}-\d{2}-\d{4}", # Social Security Number (SSN) pattern
|
||||
# r"\d{4}[-\s]\d{4}[-\s]\d{4}[-\s]\d{4}", # Credit card number pattern
|
||||
# r"\(?\d{3}\)?[-\s.]?\d{3}[-\s.]?\d{4}", # Phone number
|
||||
# r"(0[1-9]|1[0-2])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\d\d", # date of birth
|
||||
# r"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", # IP address
|
||||
# r"[a-zA-Z0-9]{32}" # API key
|
||||
# ]
|
||||
#
|
||||
# ATTRIBUTE_PATTERNS = [
|
||||
# "nome",
|
||||
# "cpf",
|
||||
# "teste",
|
||||
# "valor",
|
||||
# "original",
|
||||
# "type",
|
||||
# "solicitacaoPagador",
|
||||
# "chave",
|
||||
# "description",
|
||||
# "items",
|
||||
# "example"
|
||||
# ]
|
||||
|
||||
class Redaction():
|
||||
|
||||
def repl_value(self, message, pattern, r):
|
||||
flag_aspas = False
|
||||
flag_attribute = False
|
||||
# flag_vezes = 0
|
||||
flag_dois_pontos = False
|
||||
i = 0
|
||||
z = pattern
|
||||
str_acc = ""
|
||||
while (i < len(message)):
|
||||
try:
|
||||
if (message[i:i + len(z)] == z and (message[i + len(z):i + len(z) + 1] == "'" or message[i + len(z):i + len(z) + 1] == "\"")):
|
||||
flag_attribute = True
|
||||
flag_aspas = True
|
||||
except:
|
||||
print("except")
|
||||
if (message[i] == ":" and not flag_aspas and flag_attribute):
|
||||
flag_dois_pontos = True
|
||||
if (flag_aspas and message[i] != "'" and message[i] != "\"" and flag_attribute and flag_dois_pontos):
|
||||
str_acc = str_acc + message[i]
|
||||
message = message[0:i] + "*" + message[i + 1:len(message) + i]
|
||||
if (message[i] == "{" and flag_dois_pontos and not flag_aspas):
|
||||
flag_attribute = False
|
||||
flag_dois_pontos = False
|
||||
flag_aspas = False
|
||||
if ((message[i] == "}" or message[i] == "]" or message[i] == ",") and flag_dois_pontos and not flag_aspas and flag_attribute):
|
||||
flag_attribute = False
|
||||
flag_dois_pontos = False
|
||||
flag_aspas = False
|
||||
str_acc = ""
|
||||
if ((message[i] == "'" or message[i] == "\"")):
|
||||
flag_aspas = not flag_aspas
|
||||
if (flag_aspas == False and flag_attribute == True and flag_dois_pontos and len(str_acc) > 0):
|
||||
flag_attribute = False
|
||||
flag_dois_pontos = False
|
||||
str_acc = ""
|
||||
i = i + 1
|
||||
return message
|
||||
|
||||
def repl(self, attribute_pattern, message):
|
||||
msg_return = []
|
||||
for pattern in attribute_pattern:
|
||||
x = re.finditer(pattern, message)
|
||||
for y in x:
|
||||
msg_aux1 = []
|
||||
for reg in y.regs:
|
||||
for r in reg:
|
||||
message = self.repl_value(message, pattern, r)
|
||||
return message
|
||||
|
||||
def change(self, sensitive_pattern, message):
|
||||
for pattern in sensitive_pattern:
|
||||
message = re.sub(pattern, "<REDACTED>", message)
|
||||
return message
|
||||
|
||||
def redact(self, sensitive_pattern, attribute_pattern, message):
|
||||
message = self.repl(attribute_pattern, message)
|
||||
message = self.change(sensitive_pattern, message)
|
||||
return message
|
||||
82
redact.py
82
redact.py
@@ -1,6 +1,4 @@
|
||||
import logging
|
||||
import re
|
||||
from ctypes import string_at
|
||||
import Redaction
|
||||
|
||||
SENSITIVE_PATTERNS = [
|
||||
r"\d{3}-\d{2}-\d{4}", # Social Security Number (SSN) pattern
|
||||
@@ -16,14 +14,21 @@ ATTRIBUTE_PATTERNS = [
|
||||
"cpf",
|
||||
"teste",
|
||||
"valor",
|
||||
"original"
|
||||
"original",
|
||||
"type",
|
||||
"solicitacaoPagador",
|
||||
"expiracao", #Não funciona para atributos numéricos, apenas para String
|
||||
"chave",
|
||||
"description",
|
||||
"items",
|
||||
"example"
|
||||
]
|
||||
|
||||
Messages = [
|
||||
"Erro na validação do schema: None is not of type 'object' Failed validating 'type' in schema: {'properties': {'calendario': {'$ref': '#/components/schemas/CalendarioCob', 'x-scope': ['']}, 'chave': {'description': 'Obrigat\\xf3rio - Determina a ' 'chave Pix registrada no DICT ' 'que ser\\xe1 utilizada para a ' 'cobran\\xe7a.', 'example': '7d9f0335-8dcc-4054-9bf9-0dbd61d36906', 'maxLength': 77, 'type': 'string'}, 'devedor': {'$ref': '#/components/schemas/IPessoaCob', 'x-scope': ['']}, 'infoAdicionais': {'description': 'Cada respectiva ' 'informa\\xe7\\xe3o ' 'adicional contida ' 'na lista (nome e ' 'valor) ser\\xe1 ' 'apresentada ao ' 'pagador.', 'items': {'$ref': '#/components/schemas/InformacaoAdicional', 'x-scope': ['']}, 'maxLength': 50, 'type': 'array'}, 'loc': {'$ref': '#/components/schemas/LocationPayloadID', 'x-scope': ['']}, 'solicitacaoPagador': {'description': 'Determina um ' 'texto a ser ' 'apresentado ao ' 'pagador para ' 'que ele possa ' 'digitar uma ' 'informa\\xe7\\xe3o ' 'correlata.', 'example': 'Servi\\xe7o realizado.', 'maxLength': 140, 'type': 'string'}, 'valor': {'$ref': '#/components/schemas/ValorCob', 'x-scope': ['']}}, 'required': ['calendario', 'chave', 'valor'], 'type': 'object'} On instance: None",
|
||||
"Erro na validação do schema: User's SSN: 123-45-6789 / None is not of type 'object' Failed validating 'type' in schema: {'properties': {'calendario': {'$ref': '#/components/schemas/CalendarioCob', 'x-scope': ['']}, 'chave': {'description': 'Obrigat\\xf3rio - Determina a ' 'chave Pix registrada no DICT ' 'que ser\\xe1 utilizada para a ' 'cobran\\xe7a.', 'example': '7d9f0335-8dcc-4054-9bf9-0dbd61d36906', 'maxLength': 77, 'type': 'string'}, 'devedor': {'$ref': '#/components/schemas/IPessoaCob', 'x-scope': ['']}, 'infoAdicionais': {'description': 'Cada respectiva ' 'informa\\xe7\\xe3o ' 'adicional contida ' 'na lista (nome e ' 'valor) ser\\xe1 ' 'apresentada ao ' 'pagador.', 'items': {'$ref': '#/components/schemas/InformacaoAdicional', 'x-scope': ['']}, 'maxLength': 50, 'type': 'array'}, 'loc': {'$ref': '#/components/schemas/LocationPayloadID', 'x-scope': ['']}, 'solicitacaoPagador': {'description': 'Determina um ' 'texto a ser ' 'apresentado ao ' 'pagador para ' 'que ele possa ' 'digitar uma ' 'informa\\xe7\\xe3o ' 'correlata.', 'example': 'Servi\\xe7o realizado.', 'maxLength': 140, 'type': 'string'}, 'valor': {'$ref': '#/components/schemas/ValorCob', 'x-scope': ['']}}, 'required': ['calendario', 'chave', 'valor'], 'type': 'object'} On instance: None",
|
||||
"Mensagem de erro: 'valor' is a required property Failed validating 'required' in schema: {'properties': {'calendario': {'$ref': '#/components/schemas/CalendarioCob', 'x-scope': ['']}, 'chave': {'description': 'Obrigat\\xf3rio - Determina a ' 'chave Pix registrada no DICT ' 'que ser\\xe1 utilizada para a ' 'cobran\\xe7a.', 'example': '7d9f0335-8dcc-4054-9bf9-0dbd61d36906', 'maxLength': 77, 'type': 'string'}, 'devedor': {'$ref': '#/components/schemas/IPessoaCob', 'x-scope': ['']}, 'infoAdicionais': {'description': 'Cada respectiva ' 'informa\\xe7\\xe3o ' 'adicional contida ' 'na lista (nome e ' 'valor) ser\\xe1 ' 'apresentada ao ' 'pagador.', 'items': {'$ref': '#/components/schemas/InformacaoAdicional', 'x-scope': ['']}, 'maxLength': 50, 'type': 'array'}, 'loc': {'$ref': '#/components/schemas/LocationPayloadID', 'x-scope': ['']}, 'solicitacaoPagador': {'description': 'Determina um ' 'texto a ser ' 'apresentado ao ' 'pagador para ' 'que ele possa ' 'digitar uma ' 'informa\\xe7\\xe3o ' 'correlata.', 'example': 'Servi\\xe7o realizado.', 'maxLength': 140, 'type': 'string'}, 'valor': {'$ref': '#/components/schemas/ValorCob', 'x-scope': ['']}}, 'required': ['calendario', 'chave', 'valor'], 'type': 'object'} On instance: {'calendario': {'expiracao': 3600}, 'chave': '1eeeb615-93af-4923-a40f-16d057f9e23d', 'devedor': {'cpf': '12345678900', 'nome': 'teste'}, 'solicitacaoPagador': 'Servi\\xe7o realizado.', 'teste': 'teste'}",
|
||||
"Mensagem de erro: 'chave' is a required property Failed validating 'required' in schema: {'properties': {'calendario': {'$ref': '#/components/schemas/CalendarioCob', 'x-scope': ['']}, 'chave': {'description': 'Obrigat\\xf3rio - Determina a ' 'chave Pix registrada no DICT ' 'que ser\\xe1 utilizada para a ' 'cobran\\xe7a.', 'example': '7d9f0335-8dcc-4054-9bf9-0dbd61d36906', 'maxLength': 77, 'type': 'string'}, 'devedor': {'$ref': '#/components/schemas/IPessoaCob', 'x-scope': ['']}, 'infoAdicionais': {'description': 'Cada respectiva ' 'informa\\xe7\\xe3o ' 'adicional contida ' 'na lista (nome e ' 'valor) ser\\xe1 ' 'apresentada ao ' 'pagador.', 'items': {'$ref': '#/components/schemas/InformacaoAdicional', 'x-scope': ['']}, 'maxLength': 50, 'type': 'array'}, 'loc': {'$ref': '#/components/schemas/LocationPayloadID', 'x-scope': ['']}, 'solicitacaoPagador': {'description': 'Determina um ' 'texto a ser ' 'apresentado ao ' 'pagador para ' 'que ele possa ' 'digitar uma ' 'informa\\xe7\\xe3o ' 'correlata.', 'example': 'Servi\\xe7o realizado.', 'maxLength': 140, 'type': 'string'}, 'valor': {'$ref': '#/components/schemas/ValorCob', 'x-scope': ['']}}, 'required': ['calendario', 'chave', 'valor'], 'type': 'object'} On instance: {'calendario': {'expiracao': 3600}, 'devedor': {'cpf': '12345678900', 'nome': 'teste'}, 'solicitacaoPagador': 'Servi\\xe7o realizado.', 'teste': 'teste', 'valor': {'original': '2.44'}}",
|
||||
"Mensagem de erro:{'cpf': '123455968767', 'nome': 'Manoel da Silva'} is not valid under any of the given schemas Failed validating 'oneOf' in schema['properties']['devedor']: {'description': 'Identifica o devedor, ou seja, a pessoa ou a ' 'institui\\xe7\\xe3o a quem a cobran\\xe7a est\\xe1 endere\\xe7ada.', 'oneOf': [{'$ref': '#/components/schemas/PessoaFisicaCob', 'x-scope': ['', '#/components/schemas/IPessoaCob']}, {'$ref': '#/components/schemas/PessoaJuridicaCob', 'x-scope': ['', '#/components/schemas/IPessoaCob']}], 'properties': {'nome': {'type': 'string'}}, 'type': 'object'} On instance['devedor']: {'cpf': '12312312322', 'nome': 'Cristiano'}",
|
||||
"Mensagem de erro:{'cpf': '123.455.968-67', 'nome': 'Manoel da Silva'} is not valid under any of the given schemas Failed validating 'oneOf' in schema['properties']['devedor']: {'description': 'Identifica o devedor, ou seja, a pessoa ou a ' 'institui\\xe7\\xe3o a quem a cobran\\xe7a est\\xe1 endere\\xe7ada.', 'oneOf': [{'$ref': '#/components/schemas/PessoaFisicaCob', 'x-scope': ['', '#/components/schemas/IPessoaCob']}, {'$ref': '#/components/schemas/PessoaJuridicaCob', 'x-scope': ['', '#/components/schemas/IPessoaCob']}], 'properties': {'nome': {'type': 'string'}}, 'type': 'object'} On instance['devedor']: {'cpf': '12312312322', 'nome': 'Cristiano'}",
|
||||
"User's SSN: 123-45-6789",
|
||||
"User's Credit Card: 1234 5678 9012 3456",
|
||||
"User's phone number: (123) 456-7890",
|
||||
@@ -32,64 +37,11 @@ Messages = [
|
||||
"User's API key: a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6"
|
||||
]
|
||||
|
||||
def redact(sensitive_pattern, attribute_pattern, message):
|
||||
message = repl(attribute_pattern, message)
|
||||
message = change(sensitive_pattern, message)
|
||||
return message
|
||||
|
||||
def repl(attribute_pattern, message):
|
||||
msg_return = []
|
||||
for pattern in attribute_pattern:
|
||||
x = re.finditer(pattern, message)
|
||||
for y in x:
|
||||
msg_aux1 = []
|
||||
for reg in y.regs:
|
||||
for r in reg:
|
||||
message = repl_value(message, pattern, r)
|
||||
return message
|
||||
|
||||
def repl_value(message, pattern, r):
|
||||
flag_aspas = False
|
||||
flag_attribute = False
|
||||
# flag_vezes = 0
|
||||
flag_dois_pontos = False
|
||||
i = 0
|
||||
z = pattern
|
||||
str_acc = ""
|
||||
while (i < len(message)):
|
||||
try:
|
||||
if (message[i:i + len(z)] == z):
|
||||
flag_attribute = True
|
||||
except:
|
||||
print("except")
|
||||
if (message[i] == ":" and not flag_aspas and flag_attribute):
|
||||
flag_dois_pontos = True
|
||||
if (flag_aspas and message[i] != "'" and message[i] != "\"" and flag_attribute and flag_dois_pontos):
|
||||
str_acc = str_acc + message[i]
|
||||
message = message[0:i] + "*" + message[i + 1:len(message) + i]
|
||||
if (message[i] == "{" and flag_dois_pontos):
|
||||
flag_attribute = False
|
||||
flag_dois_pontos = False
|
||||
flag_aspas = False
|
||||
if ((message[i] == "'" or message[i] == "\"")):
|
||||
flag_aspas = not flag_aspas
|
||||
if (flag_aspas == False and flag_attribute == True and flag_dois_pontos and len(str_acc) > 0):
|
||||
flag_attribute = False
|
||||
flag_dois_pontos = False
|
||||
str_acc = ""
|
||||
i = i + 1
|
||||
return message
|
||||
|
||||
def change(sensitive_pattern, message):
|
||||
for pattern in sensitive_pattern:
|
||||
message = re.sub(pattern, "<REDACTED>", message)
|
||||
return message
|
||||
|
||||
if __name__ == "__main__":
|
||||
for message in Messages:
|
||||
message = redact(sensitive_pattern=SENSITIVE_PATTERNS, attribute_pattern=ATTRIBUTE_PATTERNS, message=message)
|
||||
redaction = Redaction.Redaction()
|
||||
for message in Messages:
|
||||
message = redaction.redact(sensitive_pattern=SENSITIVE_PATTERNS, attribute_pattern=ATTRIBUTE_PATTERNS, message=message)
|
||||
print(message)
|
||||
|
||||
# message = "Mensagem de erro:{'cpf': '123455968767', 'nome': 'Manoel da Silva'} is not valid under any of the given schemas Failed validating 'oneOf' in schema['properties']['devedor']: {'description': 'Identifica o devedor, ou seja, a pessoa ou a ' 'institui\xe7\xe3o a quem a cobran\xe7a est\xe1 endere\xe7ada.', 'oneOf': [{'$ref': '#/components/schemas/PessoaFisicaCob', 'x-scope': ['', '#/components/schemas/IPessoaCob']}, {'$ref': '#/components/schemas/PessoaJuridicaCob', 'x-scope': ['', '#/components/schemas/IPessoaCob']}], 'properties': {'nome': {'type': 'string'}}, 'type': 'object'} On instance['devedor']: {'cpf': '12312312322', 'nome': 'Cristiano'}"
|
||||
# message = redact(sensitive_pattern=SENSITIVE_PATTERNS, attribute_pattern=ATTRIBUTE_PATTERNS, message=message)
|
||||
# print(message)
|
||||
# message = "Mensagem de erro:{'cpf': '123455968767', 'nome': 'Manoel da Silva'} is not valid under any of the given schemas Failed validating 'oneOf' in schema['properties']['devedor']: {'description': 'Identifica o devedor, ou seja, a pessoa ou a ' 'institui\xe7\xe3o a quem a cobran\xe7a est\xe1 endere\xe7ada.', 'oneOf': [{'$ref': '#/components/schemas/PessoaFisicaCob', 'x-scope': ['', '#/components/schemas/IPessoaCob']}, {'$ref': '#/components/schemas/PessoaJuridicaCob', 'x-scope': ['', '#/components/schemas/IPessoaCob']}], 'properties': {'nome': {'type': 'string'}}, 'type': 'object'} On instance['devedor']: {'cpf': '12312312322', 'nome': 'Cristiano'}"
|
||||
# message = redact(sensitive_pattern=SENSITIVE_PATTERNS, attribute_pattern=ATTRIBUTE_PATTERNS, message=message)
|
||||
# print(message)
|
||||
|
||||
Reference in New Issue
Block a user