Skip to content

Commit

Permalink
Script may store output to absolute path only when flag enabled in co…
Browse files Browse the repository at this point in the history
…nfig.ini.
  • Loading branch information
gzotti committed Jun 21, 2018
1 parent 608bb94 commit 7481833
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmake/default_config.ini.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ landscape_name = guereins
#removable_media_path = /mount/point

[scripts]
scripting_allow_write_files = false
flag_script_allow_ui = false
flag_script_allow_write_absolute_path = false
#flag_script_allow_ui = false

#[proxy]
#host_name = proxy.org
Expand Down
5 changes: 4 additions & 1 deletion guide/app_config_ini.tex
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,10 @@ \subsection{\big[scripts\big]}

\begin{tabularx}{\textwidth}{l|l|l|X}\toprule
\emph{ID} & \emph{Type} & \emph{Default} & \emph{Description}\\\midrule
startup\_script & string & startup.ssc & name of script executed on program start\\\bottomrule
startup\_script & string & startup.ssc & name of script executed on program start\\
flag\_script\_allow\_write\_absolute\_path & bool & false & set true to let scripts store files to absolute pathnames.
This may pose a security risk if you run scripts from other authors
without checking what they are doing.\\\bottomrule
%% These are unused as on 0.15pre.
%flag\_script\_allow\_ui & bool &\\\midrule
%scripting\_allow\_write\_files & bool &\\\bottomrule
Expand Down
4 changes: 3 additions & 1 deletion src/scripting/StelScriptMgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ public slots:
//! Reset output file and cause the emission of an (empty) scriptOutput signal.
void resetOutput(void);

//! Save output file to new file (in same directory as output.txt).
//! Save output file to new file.
//! This is required to allow reading with other program on Windows while output.txt is still open.
//! @param filename new filename. If this is not an absolute path, it will be created in the same directory as output.txt
//! @note For storing to absolute path names, set [scripts]/flag_script_allow_write_absolute_path=true.
void saveOutputAs(const QString &filename);

//! Pause a running script.
Expand Down
24 changes: 17 additions & 7 deletions src/scripting/StelScriptOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/

#include "StelScriptOutput.hpp"
#include <QDir>
#include <QDebug>
#include <QSettings>
#include "StelScriptOutput.hpp"
#include "StelApp.hpp"

// Init static variables.
QFile StelScriptOutput::outputFile;
Expand Down Expand Up @@ -56,23 +58,31 @@ void StelScriptOutput::saveOutputAs(const QString &name)
QFileInfo outputInfo(outputFile);
QDir dir=outputInfo.dir(); // will hold complete dirname
QFileInfo newFileNameInfo(name);
//QDir newFileDir=newFileNameInfo.dir();

if (newFileNameInfo.fileName()==name)
bool okToSaveToAbsolutePath=StelApp::getInstance().getSettings()->value("scripts/flag_script_allow_write_absolute_path", false).toBool();

if (!okToSaveToAbsolutePath && (newFileNameInfo.isAbsolute()))
{
asFile.setFileName(dir.absolutePath() + "/" + name);
qWarning() << "SCRIPTING CONFIGURATION ISSUE: You are trying to save to an absolute pathname.";
qWarning() << " To enable this, edit config.ini and set [scripts]/flag_script_allow_write_absolute_path=true";
asFile.setFileName(dir.absolutePath() + "/" + newFileNameInfo.fileName());
qWarning() << " Storing to " << asFile.fileName() << " instead";
}
else
else if (okToSaveToAbsolutePath && (newFileNameInfo.isAbsolute()))
{
asFile.setFileName(name);
}
else
{
asFile.setFileName(dir.absolutePath() + "/" + name);
}

if (!asFile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text | QIODevice::Unbuffered))
{
qDebug() << "ERROR: Cannot open file" << dir.absolutePath() + "/" + name;
qDebug() << "ERROR: Cannot open file" << asFile.fileName();
return;
}
qDebug() << "saving copy of output.txt to " << dir.absolutePath() + "/" + name;
qDebug() << "saving copy of output.txt to " << asFile.fileName();
asFile.write(qPrintable(outputText), outputText.size());
asFile.close();
}
1 change: 1 addition & 0 deletions src/scripting/StelScriptOutput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class StelScriptOutput
//! Save to new file, re-create output file.
//! This allows reading of results on Windows, where otherwise reading programs cannot access files opened for writing by Stellarium.
//! @param name new filename. If this is not an absolute path, it will be created in the same directory as output.txt
//! @note For storing to absolute path names, set [scripts]/flag_script_allow_write_absolute_path=true.
//! Normally you would call saveOutputAs(...), then reset().
static void saveOutputAs(const QString& name);

Expand Down

0 comments on commit 7481833

Please sign in to comment.