/*
*
- * Copyright (C) 1994-2021, OFFIS e.V.
+ * Copyright (C) 1994-2022, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
sprintf(imageFileName, "%s.%s",
dcmSOPClassUIDToModality(req->AffectedSOPClassUID),
req->AffectedSOPInstanceUID);
+ OFStandard::sanitizeFilename(imageFileName);
}
OFString temp_str;
if (!subdirectoryName.empty())
subdirectoryName += '_';
subdirectoryName += currentStudyInstanceUID;
+ OFStandard::sanitizeFilename(subdirectoryName);
break;
case ESM_PatientName:
// pattern: "[Patient's Name]_[YYYYMMDD]_[HHMMSSMMM]"
subdirectoryName = currentPatientName;
subdirectoryName += '_';
subdirectoryName += timestamp;
+ OFStandard::sanitizeFilename(subdirectoryName);
break;
case ESM_None:
break;
}
else
{
- // don't create new UID, use the study instance UID as found in object
+ // Use the SOP instance UID as found in the C-STORE request message as part of the filename
+ OFString uid = req->AffectedSOPInstanceUID;
+ OFStandard::sanitizeFilename(uid);
sprintf(imageFileName, "%s%c%s.%s%s", opt_outputDirectory.c_str(), PATH_SEPARATOR, dcmSOPClassUIDToModality(req->AffectedSOPClassUID, "UNKNOWN"),
- req->AffectedSOPInstanceUID, opt_fileNameExtension.c_str());
+ uid.c_str(), opt_fileNameExtension.c_str());
}
}
/*
*
- * Copyright (C) 2013-2021, OFFIS e.V.
+ * Copyright (C) 2013-2022, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
generatedFileName = tmpString;
OFSTRINGSTREAM_FREESTR(tmpString);
// combine the generated file name with the directory name
+ OFStandard::sanitizeFilename(generatedFileName);
OFStandard::combineDirAndFilename(filename, directoryName, generatedFileName);
}
break;
generatedFileName = tmpString;
OFSTRINGSTREAM_FREESTR(tmpString);
// combine the generated file name with the directory name
+ OFStandard::sanitizeFilename(generatedFileName);
OFStandard::combineDirAndFilename(filename, directoryName, generatedFileName);
break;
}
generatedFileName = tmpString;
OFSTRINGSTREAM_FREESTR(tmpString);
// combine the generated file name
+ OFStandard::sanitizeFilename(generatedFileName);
OFStandard::combineDirAndFilename(filename, directoryName, generatedFileName);
} else
status = EC_CouldNotGenerateFilename;
OFString name = dcmSOPClassUIDToModality(sopClassUID.c_str(), "UNKNOWN");
name += ".";
name += sopInstanceUID;
+ OFStandard::sanitizeFilename(name);
OFString returnStr;
OFStandard::combineDirAndFilename(returnStr, m_storageDir, name, OFTrue);
return returnStr;
/*
*
- * Copyright (C) 2000-2021, OFFIS e.V.
+ * Copyright (C) 2000-2022, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*/
static void forceSleep(Uint32 seconds);
+ /** sanitize a filename (NOT a path name!) by replacing all path
+ * separators with underscores. This avoids possible path traversal
+ * vulnerabilities if malformed data read from file or received over
+ * a network is used as part of a filename.
+ * @param fname filename to be sanitized
+ */
+ static void sanitizeFilename(OFString& fname);
+
+ /** sanitize a filename (NOT a path name!) by replacing all path
+ * separators with underscores. This avoids possible path traversal
+ * vulnerabilities if malformed data read from file or received over
+ * a network is used as part of a filename.
+ * @param fname filename to be sanitized
+ */
+ static void sanitizeFilename(char *fname);
+
private:
/** private implementation of strlcpy. Called when strlcpy
/*
*
- * Copyright (C) 1997-2021, OFFIS e.V.
+ * Copyright (C) 1997-2022, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
{
// create filename
filename.clear();
- if (dir)
- {
- filename = dir;
- filename += PATH_SEPARATOR;
- }
- if (prefix) filename += prefix;
+ if (prefix) filename = prefix;
addLongToString(creation_time, filename);
// on some systems OFrand_r may produce only 16-bit random numbers.
// To be on the safe side, we use two random numbers for the upper and the lower 16 bits.
addLongToString((((OFrand_r(seed) & 0xFFFF) << 16) | (OFrand_r(seed) & 0xFFFF)), filename);
if (postfix) filename += postfix;
+ OFStandard::sanitizeFilename(filename);
+
+ if (dir)
+ {
+ OFString dirname = dir;
+ dirname += PATH_SEPARATOR;
+ dirname += filename;
+ filename = dirname;
+ }
+
// check if filename exists
stat_result = stat(filename.c_str(), &stat_buf);
if (stat_result == 0)
/*
*
- * Copyright (C) 2001-2021, OFFIS e.V.
+ * Copyright (C) 2001-2022, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
}
}
+
+void OFStandard::sanitizeFilename(OFString& fname)
+{
+ size_t len = fname.length();
+ for (size_t i=0; i<len; ++i)
+ {
+#ifdef _WIN32
+ if ((fname[i] == PATH_SEPARATOR)||(fname[i] == '/')) fname[i] = '_';
+#else
+ if (fname[i] == PATH_SEPARATOR) fname[i] = '_';
+#endif
+ }
+}
+
+
+void OFStandard::sanitizeFilename(char *fname)
+{
+ if (fname)
+ {
+ char *c = fname;
+ while (*c)
+ {
+#ifdef _WIN32
+ if ((*c == PATH_SEPARATOR)||(*c == '/')) *c = '_';
+#else
+ if (*c == PATH_SEPARATOR) *c = '_';
+#endif
+ ++c;
+ }
+ }
+}
+
+
#include DCMTK_DIAGNOSTIC_IGNORE_STRICT_ALIASING_WARNING
// black magic: