Skip to content

Commit

Permalink
Check injections in PythonLayer
Browse files Browse the repository at this point in the history
* Modified Makefile to link boost_regex for PythonLayer.
* Travis installs boost_regex
* Add a note of a new dependency boost_regex on Makefile.config.example
  • Loading branch information
tnarihi committed Mar 2, 2015
1 parent 40dc686 commit 6d4f992
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ endif
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 m \
opencv_core opencv_highgui opencv_imgproc
PYTHON_LIBRARIES := boost_python python2.7
PYTHON_LIBRARIES := boost_python python2.7 boost_regex
WARNINGS := -Wall -Wno-sign-compare

##############################
Expand Down
1 change: 1 addition & 0 deletions Makefile.config.example
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ PYTHON_LIB := /usr/lib
# PYTHON_LIB := $(ANACONDA_HOME)/lib

# Uncomment to support layers written in Python (will link against Python libs)
# This will require an additional dependency boost_regex provided by boost.
# WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
Expand Down
1 change: 1 addition & 0 deletions scripts/travis/travis_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ apt-get install \
python-dev python-numpy \
libleveldb-dev libsnappy-dev libopencv-dev \
libboost-dev libboost-system-dev libboost-python-dev libboost-thread-dev \
libboost-regex-dev \
libprotobuf-dev protobuf-compiler \
libatlas-dev libatlas-base-dev \
libhdf5-serial-dev libgflags-dev libgoogle-glog-dev \
Expand Down
7 changes: 7 additions & 0 deletions src/caffe/layer_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "caffe/vision_layers.hpp"

#ifdef WITH_PYTHON_LAYER
#include <boost/regex.hpp>
#include "caffe/python_layer.hpp"
#endif

Expand Down Expand Up @@ -162,6 +163,12 @@ template <typename Dtype>
shared_ptr<Layer<Dtype> > GetPythonLayer(const LayerParameter& param) {
string module_name = param.python_param().module();
string layer_name = param.python_param().layer();
// Check injection. This doesn't allow nested importing.
boost::regex expression("[a-zA-Z_][a-zA-Z0-9_]*");
CHECK(boost::regex_match(module_name, expression))
<< "Module name is invalid: " << module_name;
CHECK(boost::regex_match(layer_name, expression))
<< "Layer name is invalid: " << layer_name;
Py_Initialize();
try {
bp::object globals = bp::import("__main__").attr("__dict__");
Expand Down

0 comments on commit 6d4f992

Please sign in to comment.