Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for issue #48 and logging fix #72

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
23 changes: 21 additions & 2 deletions laikaboss/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ def ensureNotUnicode(buffer):
else:
return buffer

# Utility function to make sure the buffer is a string and not None
# (or whatever other weirdness comes through)
def ensureStr(child_buffer):
# buffers and bytearrays can be cast to string
try:
if type(child_buffer) == buffer or type(child_buffer) == bytearray:
child_buffer = str(child_buffer)
except:
# Test cases do not produce any exceptions, but it's here just in case
raise Exception("Buffer of %s found, not creating child scanObject" % str(type(child_buffer)))

# refuse to process anything else, as non-string objects can crash the worker
if not type(child_buffer) is str:
raise Exception("Buffer of %s found, not creating child scanObject" % str(type(child_buffer)))

child_buffer = ensureNotUnicode(child_buffer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have a logic error here. This function will never get called for a unicode object because you're raising an exception above. Therefore instead of a unicode object getting encoded to a utf-8 string, you'd get an exception instead. Can you address this?

Copy link

@ewalkup ewalkup Oct 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right - we've run initial tests and put the code in our environment, we'll push the fix once it's run for awhile/been monitored for errors.


return child_buffer

def cleanKey(key):
bad_chars = ['\0', '.', '$']
new_key = key
Expand Down Expand Up @@ -101,7 +120,7 @@ def __init__(self, objectHash = "",
self.scanModules = []
self.flags = []
self.objectHash = objectHash
self.buffer = ensureNotUnicode(buffer)
self.buffer = ensureStr(buffer)
self.objectSize = objectSize
self.filename = filename
self.ephID = convertToUTF8(ephID)
Expand Down Expand Up @@ -228,7 +247,7 @@ def __init__(self, source=None, level=None, rootUID=None):

class SI_Object(object):
def __init__(self, buffer, externalVars):
self.buffer = ensureNotUnicode(buffer)
self.buffer = ensureStr(buffer)
self.externalVars = externalVars
buffer = ""
externalVars = None
Expand Down
12 changes: 10 additions & 2 deletions laikamilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ def __init__(self, logger):
self.milterConfig.loadAll()
self.logger = logger
self.attachements = ""
self.uuid = ""


#Required final call to close zmq connection
Expand All @@ -588,7 +589,14 @@ def close(self):

#public function to get flags from si-scan
def zmqGetFlagswithRetry(self, numRetries, milterContext):
self.uuid = milterContext.uuid
sendResponse = self._zmqGetFlags(numRetries, milterContext)
if sendResponse == -1:
#If your servers list is blank, you need to add servers into your laikamilter config
self.logger.writeLog(syslog.LOG_ERR, \
"ERROR Laikamilter failed to get a response from the Laikad servers:"\
+ str(milterContext.milterConfig.servers)
)


def _getNextScanServer(self):
Expand Down Expand Up @@ -628,15 +636,15 @@ def _zmqGetFlags(self, numRetries, milterContext):
def _zmqSendBuffer(self, milterContext,numRetries, REQUEST_TIMEOUT,SERVER_ENDPOINT):
gotResponseFromScanner=-1
self.client = Client(SERVER_ENDPOINT)

log = milterContext.uuid+" Sending "+ str(milterContext.qid)+" to "+ SERVER_ENDPOINT
self.logger.writeLog(syslog.LOG_DEBUG, "%s"%(str(log)))
myhostname = socket.gethostname()
externalObject = ExternalObject(
buffer=milterContext.fileBuffer,
externalVars=ExternalVars(
filename=milterContext.archiveFileName,
source=milterContext.milterConfig.milterName+"-"+str(myhostname[:myhostname.index(".")]),
source=milterContext.milterConfig.milterName+"-"+ \
myhostname.split(".")[0],
ephID=milterContext.qid,
uniqID=milterContext.messageID
),
Expand Down