From 70418ef8c89c55da52ff13ec3e72e703011bdc83 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Mon, 4 Jul 2022 11:01:14 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E5=A4=8D=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=A8=E7=B3=BB=E7=BB=9F=E7=94=A8=E6=88=B7=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E9=97=AE=E9=A2=98=EF=BC=8C=E6=88=AA=E5=8F=96?= =?UTF-8?q?=E6=88=9064=E5=AD=97=E7=AC=A6=20(#8520)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: 修复命令表系统用户字段长度问题,截取成64字符 * perf: 优化截取方法 * perf: 优化截取方法 Co-authored-by: halo --- apps/terminal/backends/command/serializers.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/apps/terminal/backends/command/serializers.py b/apps/terminal/backends/command/serializers.py index 9c8484efb..83b9d55d2 100644 --- a/apps/terminal/backends/command/serializers.py +++ b/apps/terminal/backends/command/serializers.py @@ -2,6 +2,7 @@ from django.utils.translation import ugettext_lazy as _ from rest_framework import serializers +from common.utils import pretty_string from .models import AbstractSessionCommand __all__ = ['SessionCommandSerializer', 'InsecureCommandAlertSerializer'] @@ -32,7 +33,7 @@ class SessionCommandSerializer(SimpleSessionCommandSerializer): """使用这个类作为基础Command Log Serializer类, 用来序列化""" id = serializers.UUIDField(read_only=True) - system_user = serializers.CharField(max_length=64, label=_("System user")) + system_user = serializers.CharField(label=_("System user")) # 限制 64 字符,不能直接迁移成 128 字符,命令表数据量会比较大 output = serializers.CharField(max_length=2048, allow_blank=True, label=_("Output")) risk_level_display = serializers.SerializerMethodField(label=_('Risk level display')) timestamp = serializers.IntegerField(label=_('Timestamp')) @@ -43,3 +44,8 @@ class SessionCommandSerializer(SimpleSessionCommandSerializer): def get_risk_level_display(obj): risk_mapper = dict(AbstractSessionCommand.RISK_LEVEL_CHOICES) return risk_mapper.get(obj.risk_level) + + def validate_system_user(self, value): + if len(value) > 64: + value = pretty_string(value, 64) + return value