prevent sending dependencies when hiding file names

This commit is contained in:
Alan Hamlett 2017-10-31 08:57:30 -07:00
parent 8b2170d409
commit e0fa82e2c9
4 changed files with 77 additions and 76 deletions

View file

@ -3,4 +3,4 @@ debug = false
api_key = 033c47c9-0441-4eb5-8b3f-b51f27b31049 api_key = 033c47c9-0441-4eb5-8b3f-b51f27b31049
hidefilenames = hidefilenames =
missingfile missingfile
twolinefile\.txt$ python\.py$

View file

@ -0,0 +1,6 @@
[settings]
debug = false
api_key = 033c47c9-0441-4eb5-8b3f-b51f27b31049
hidefilenames =
missingfile
twolinefile\.txt$

View file

@ -203,7 +203,6 @@ class ConfigsTestCase(utils.TestCase):
expected = '' expected = ''
self.assertEquals(log_output, expected) self.assertEquals(log_output, expected)
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called() self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
@ -261,9 +260,9 @@ class ConfigsTestCase(utils.TestCase):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.TemporaryDirectory() as tempdir: with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/twolinefile.txt' entity = 'tests/samples/codefiles/python.py'
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) shutil.copy(entity, os.path.join(tempdir, 'python.py'))
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) entity = os.path.realpath(os.path.join(tempdir, 'python.py'))
now = u(int(time.time())) now = u(int(time.time()))
config = 'tests/samples/configs/paranoid.cfg' config = 'tests/samples/configs/paranoid.cfg'
key = str(uuid.uuid4()) key = str(uuid.uuid4())
@ -280,25 +279,19 @@ class ConfigsTestCase(utils.TestCase):
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
heartbeat = { heartbeat = {
'language': 'Text only', 'language': 'Python',
'lines': 2, 'entity': 'HIDDEN.py',
'entity': 'HIDDEN.txt',
'project': os.path.basename(os.path.abspath('.')), 'project': os.path.basename(os.path.abspath('.')),
'time': float(now), 'time': float(now),
'type': 'file', 'type': 'file',
} }
stats = {
u('cursorpos'): None,
u('dependencies'): [],
u('language'): u('Text only'),
u('lineno'): None,
u('lines'): 2,
}
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): ANY, cert=None, proxies={}, stream=False, timeout=60, verify=True,
self.assertEquals(heartbeat[key], val) )
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) body = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].body
for key, val in json.loads(body).items():
self.assertEquals(val, heartbeat.get(key))
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
def test_hide_all_filenames_from_cli_arg(self): def test_hide_all_filenames_from_cli_arg(self):
@ -307,9 +300,9 @@ class ConfigsTestCase(utils.TestCase):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.TemporaryDirectory() as tempdir: with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/twolinefile.txt' entity = 'tests/samples/codefiles/python.py'
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) shutil.copy(entity, os.path.join(tempdir, 'python.py'))
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) entity = os.path.realpath(os.path.join(tempdir, 'python.py'))
now = u(int(time.time())) now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg' config = 'tests/samples/configs/good_config.cfg'
key = str(uuid.uuid4()) key = str(uuid.uuid4())
@ -326,25 +319,19 @@ class ConfigsTestCase(utils.TestCase):
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
heartbeat = { heartbeat = {
'language': 'Text only', 'language': 'Python',
'lines': 2, 'entity': 'HIDDEN.py',
'entity': 'HIDDEN.txt',
'project': os.path.basename(os.path.abspath('.')), 'project': os.path.basename(os.path.abspath('.')),
'time': float(now), 'time': float(now),
'type': 'file', 'type': 'file',
} }
stats = {
u('cursorpos'): None,
u('dependencies'): [],
u('language'): u('Text only'),
u('lineno'): None,
u('lines'): 2,
}
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): ANY, cert=None, proxies={}, stream=False, timeout=60, verify=True,
self.assertEquals(heartbeat[key], val) )
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) body = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].body
for key, val in json.loads(body).items():
self.assertEquals(val, heartbeat.get(key))
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
def test_hide_matching_filenames(self): def test_hide_matching_filenames(self):
@ -353,9 +340,9 @@ class ConfigsTestCase(utils.TestCase):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.TemporaryDirectory() as tempdir: with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/twolinefile.txt' entity = 'tests/samples/codefiles/python.py'
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) shutil.copy(entity, os.path.join(tempdir, 'python.py'))
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) entity = os.path.realpath(os.path.join(tempdir, 'python.py'))
now = u(int(time.time())) now = u(int(time.time()))
config = 'tests/samples/configs/hide_file_names.cfg' config = 'tests/samples/configs/hide_file_names.cfg'
key = str(uuid.uuid4()) key = str(uuid.uuid4())
@ -372,25 +359,19 @@ class ConfigsTestCase(utils.TestCase):
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
heartbeat = { heartbeat = {
'language': 'Text only', 'language': 'Python',
'lines': 2, 'entity': 'HIDDEN.py',
'entity': 'HIDDEN.txt',
'project': os.path.basename(os.path.abspath('.')), 'project': os.path.basename(os.path.abspath('.')),
'time': float(now), 'time': float(now),
'type': 'file', 'type': 'file',
} }
stats = {
u('cursorpos'): None,
u('dependencies'): [],
u('language'): u('Text only'),
u('lineno'): None,
u('lines'): 2,
}
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): ANY, cert=None, proxies={}, stream=False, timeout=60, verify=True,
self.assertEquals(heartbeat[key], val) )
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) body = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].body
for key, val in json.loads(body).items():
self.assertEquals(val, heartbeat.get(key))
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
def test_does_not_hide_unmatching_filenames(self): def test_does_not_hide_unmatching_filenames(self):
@ -399,12 +380,13 @@ class ConfigsTestCase(utils.TestCase):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.TemporaryDirectory() as tempdir: with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/emptyfile.txt' entity = 'tests/samples/codefiles/python.py'
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) shutil.copy(entity, os.path.join(tempdir, 'python.py'))
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) entity = os.path.realpath(os.path.join(tempdir, 'python.py'))
now = u(int(time.time())) now = u(int(time.time()))
config = 'tests/samples/configs/hide_file_names.cfg' config = 'tests/samples/configs/hide_file_names_not_python.cfg'
key = str(uuid.uuid4()) key = str(uuid.uuid4())
dependencies = ['sqlalchemy', 'jinja', 'simplejson', 'flask', 'app', 'django', 'pygments', 'unittest', 'mock']
args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--logfile', '~/.wakatime.log'] args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--logfile', '~/.wakatime.log']
@ -418,23 +400,27 @@ class ConfigsTestCase(utils.TestCase):
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
heartbeat = { heartbeat = {
'language': 'Text only', 'language': 'Python',
'lines': 0, 'lines': 37,
'entity': entity, 'entity': entity,
'project': os.path.basename(os.path.abspath('.')), 'project': os.path.basename(os.path.abspath('.')),
'dependencies': dependencies,
'time': float(now), 'time': float(now),
'type': 'file', 'type': 'file',
} }
stats = { stats = {
u('cursorpos'): None, u('cursorpos'): None,
u('dependencies'): [], u('dependencies'): dependencies,
u('language'): u('Text only'), u('language'): u('Python'),
u('lineno'): None, u('lineno'): None,
u('lines'): 0, u('lines'): 37,
} }
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None)
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items():
if key == 'dependencies':
self.assertEquals(sorted(heartbeat[key]), sorted(val))
else:
self.assertEquals(heartbeat[key], val) self.assertEquals(heartbeat[key], val)
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()

View file

@ -80,19 +80,6 @@ def send_heartbeat(project=None, branch=None, hostname=None, stats={}, key=None,
'entity': entity, 'entity': entity,
'type': entity_type, 'type': entity_type,
} }
if hidefilenames and entity is not None and entity_type == 'file':
for pattern in hidefilenames:
try:
compiled = re.compile(pattern, re.IGNORECASE)
if compiled.search(entity):
extension = u(os.path.splitext(data['entity'])[1])
data['entity'] = u('HIDDEN{0}').format(extension)
break
except re.error as ex:
log.warning(u('Regex error ({msg}) for include pattern: {pattern}').format(
msg=u(ex),
pattern=u(pattern),
))
if stats.get('lines'): if stats.get('lines'):
data['lines'] = stats['lines'] data['lines'] = stats['lines']
if stats.get('language'): if stats.get('language'):
@ -109,6 +96,28 @@ def send_heartbeat(project=None, branch=None, hostname=None, stats={}, key=None,
data['project'] = project data['project'] = project
if branch: if branch:
data['branch'] = branch data['branch'] = branch
if hidefilenames and entity is not None and entity_type == 'file':
for pattern in hidefilenames:
try:
compiled = re.compile(pattern, re.IGNORECASE)
if compiled.search(entity):
extension = u(os.path.splitext(data['entity'])[1])
data['entity'] = u('HIDDEN{0}').format(extension)
# also delete any sensitive info when hiding file names
sensitive = ['dependencies', 'lines', 'lineno', 'cursorpos', 'branch']
for key in sensitive:
if key in data:
del data[key]
break
except re.error as ex:
log.warning(u('Regex error ({msg}) for include pattern: {pattern}').format(
msg=u(ex),
pattern=u(pattern),
))
log.debug(data) log.debug(data)
# setup api request # setup api request