Mercurial > hg > orthanc
changeset 7050:5e3b74be6f0d
Reject invalid characters in AET
| author | Alain Mazy <am@orthanc.team> |
|---|---|
| date | Tue, 11 Aug 2026 18:27:09 +0200 |
| parents | 3b85dc5a36ed |
| children | 61055f1ee946 |
| files | NEWS OrthancFramework/Sources/DicomNetworking/Internals/CommandDispatcher.cpp OrthancFramework/Sources/Toolbox.cpp OrthancFramework/Sources/Toolbox.h OrthancFramework/UnitTestsSources/ToolboxTests.cpp OrthancServer/Sources/OrthancConfiguration.cpp OrthancServer/Sources/OrthancConfiguration.h OrthancServer/Sources/OrthancInitialization.cpp |
| diffstat | 8 files changed, 115 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/NEWS Tue Aug 11 13:46:00 2026 +0200 +++ b/NEWS Tue Aug 11 18:27:09 2026 +0200 @@ -31,6 +31,9 @@ * Fix mismatches between the configuration default values and the documentation: - "HttpTimeout" was documented as 60 while its default value is 0 (no timeout). - "Name" was documented as "MyOrthanc" while its default value is "ORTHANC". +* Orthanc no longer accepts DICOM association from modalities whose AET contains invalid characters. +* Orthanc now ignores leading/trailing spaces in AET. + REST API --------
--- a/OrthancFramework/Sources/DicomNetworking/Internals/CommandDispatcher.cpp Tue Aug 11 13:46:00 2026 +0200 +++ b/OrthancFramework/Sources/DicomNetworking/Internals/CommandDispatcher.cpp Tue Aug 11 18:27:09 2026 +0200 @@ -328,6 +328,15 @@ calledAet = (/*OFSTRING_GUARD*/(calledAet_C)); } + if (!Toolbox::IsValidAet(remoteAet)) + { + LOG(ERROR) << "Association Received from an invalid AET (sanitized: " << Toolbox::NormalizeAet(remoteAet) << ")" + << " on IP " << remoteIp << ", rejecting the association"; + return NULL; + } + + remoteAet = Toolbox::NormalizeAet(remoteAet); // removes leading/trailing spaces that are not significant according to the standard + CLOG(INFO, DICOM) << "Association Received from AET " << remoteAet << " on IP " << remoteIp;
--- a/OrthancFramework/Sources/Toolbox.cpp Tue Aug 11 13:46:00 2026 +0200 +++ b/OrthancFramework/Sources/Toolbox.cpp Tue Aug 11 18:27:09 2026 +0200 @@ -3022,6 +3022,56 @@ } + bool Toolbox::IsValidAet(const std::string& aet) + { + if (aet.size() == 0) + { + return false; + } + + const uint8_t* p = reinterpret_cast<const uint8_t*>(aet.c_str()); + size_t spacesCount = 0; + + for (size_t i = 0; i < aet.size(); ++i, ++p) + { + if (iscntrl(*p) || *p == '\\') // formaly, only LF, FF, CR and ESC and \ are invalid but let's refuse any control character to be extra safe. + { + return false; + } + else if (*p == ' ') + { + ++spacesCount; + } + } + + if (spacesCount == aet.size()) // an AET made of only spaces is invalid + { + return false; + } + + return true; + } + + + // replaces any invalid char by * such that the AET is safe to log + // + removes leading and trailing spaces that must be ignored + std::string Toolbox::NormalizeAet(const std::string& aet) + { + std::string normalizedAet = aet; + uint8_t* p = reinterpret_cast<uint8_t*>(normalizedAet.data()); + + for (size_t i = 0; i < aet.size(); ++i, ++p) + { + if (iscntrl(*p) || *p == '\\') + { + *p = '*'; + } + } + + return StripSpaces(normalizedAet); // remove any leading/trailing spaces (all leading \t\n\r have already been sanitized before) + } + + std::string Toolbox::NormalizePath(const std::string& utf8, bool allowUtf8, bool allowSlashes)
--- a/OrthancFramework/Sources/Toolbox.h Tue Aug 11 13:46:00 2026 +0200 +++ b/OrthancFramework/Sources/Toolbox.h Tue Aug 11 18:27:09 2026 +0200 @@ -411,6 +411,10 @@ static bool IsValidUtf8(const std::string& s); + static bool IsValidAet(const std::string& aet); + + static std::string NormalizeAet(const std::string& aet); + static std::string NormalizePath(const std::string& utf8, bool allowUtf8, bool allowSlashes);
--- a/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Tue Aug 11 13:46:00 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Tue Aug 11 18:27:09 2026 +0200 @@ -461,6 +461,28 @@ ASSERT_FALSE(Orthanc::Toolbox::IsValidUtf8("\xF0\x28\x8C\x28")); } +TEST(Toolbox, IsValidAet) +{ + ASSERT_TRUE(Orthanc::Toolbox::IsValidAet("ABC_DEF")); + ASSERT_FALSE(Orthanc::Toolbox::IsValidAet("")); + ASSERT_FALSE(Orthanc::Toolbox::IsValidAet(" ")); + ASSERT_FALSE(Orthanc::Toolbox::IsValidAet(" ")); + ASSERT_FALSE(Orthanc::Toolbox::IsValidAet("NEW_LINE\n")); + ASSERT_FALSE(Orthanc::Toolbox::IsValidAet("CAR_RET\r")); + ASSERT_FALSE(Orthanc::Toolbox::IsValidAet("\\SLASH")); + ASSERT_FALSE(Orthanc::Toolbox::IsValidAet(" \x01CNTRL")); +} + +TEST(Toolbox, NormalizeAet) +{ + ASSERT_EQ("ABC_DEF", Orthanc::Toolbox::NormalizeAet(" ABC_DEF ")); + ASSERT_EQ("NEW_LINE*", Orthanc::Toolbox::NormalizeAet("NEW_LINE\n")); + ASSERT_EQ("WITH MIDDLE SPACE", Orthanc::Toolbox::NormalizeAet("WITH MIDDLE SPACE")); + ASSERT_EQ("CAR_RET*", Orthanc::Toolbox::NormalizeAet("CAR_RET\r")); + ASSERT_EQ("SLASH*", Orthanc::Toolbox::NormalizeAet("SLASH\\")); + ASSERT_EQ("CNTRL*", Orthanc::Toolbox::NormalizeAet("CNTRL\x01")); +} + TEST(Toolbox, BoundMemorySizeToCurrentArchitecture) { if (sizeof(void*) == 4)
--- a/OrthancServer/Sources/OrthancConfiguration.cpp Tue Aug 11 13:46:00 2026 +0200 +++ b/OrthancServer/Sources/OrthancConfiguration.cpp Tue Aug 11 18:27:09 2026 +0200 @@ -345,6 +345,28 @@ return workersThread; } + void OrthancConfiguration::LoadOrthancAET() + { + std::string dicomAet = GetStringParameter(ORTHANC_CONFIG_DICOM_AET); + + if (!Toolbox::IsValidAet(dicomAet)) + { + throw OrthancException(ErrorCode_BadFileFormat, std::string("The ") + ORTHANC_CONFIG_DICOM_AET + " contains characters that are not valid for an AET"); + } + + orthancDicomAet_ = Toolbox::NormalizeAet(dicomAet); + } + + std::string OrthancConfiguration::GetOrthancAET() const + { + if (orthancDicomAet_.empty()) + { + throw Orthanc::OrthancException(ErrorCode_BadSequenceOfCalls); + } + + return orthancDicomAet_; + } + void OrthancConfiguration::LoadPeers() { if (GetBooleanParameter(ORTHANC_PEERS_IN_DB))
--- a/OrthancServer/Sources/OrthancConfiguration.h Tue Aug 11 13:46:00 2026 +0200 +++ b/OrthancServer/Sources/OrthancConfiguration.h Tue Aug 11 18:27:09 2026 +0200 @@ -93,6 +93,7 @@ JobsEngineThreadsCount jobsEngineThreadsCount_; ServerIndex* serverIndex_; std::set<Warnings> disabledWarnings_; + std::string orthancDicomAet_; OrthancConfiguration() : serverIndex_(NULL) @@ -287,10 +288,9 @@ return disabledWarnings_.count(warning) == 0; } - std::string GetOrthancAET() const - { - return GetStringParameter(ORTHANC_CONFIG_DICOM_AET); - } + void LoadOrthancAET(); + + std::string GetOrthancAET() const; std::string GetOrthancName() const {
--- a/OrthancServer/Sources/OrthancInitialization.cpp Tue Aug 11 13:46:00 2026 +0200 +++ b/OrthancServer/Sources/OrthancInitialization.cpp Tue Aug 11 18:27:09 2026 +0200 @@ -349,6 +349,7 @@ // Read the user-provided configuration lock.GetConfiguration().Read(configurationFile); + lock.GetConfiguration().LoadOrthancAET(); // read it early because we need it for the ServerDatabaseIdentifier { std::string locale;
