--- APP LOAD RESOURCE ---
| 1611 | |
| 1612 | // --- APP LOAD RESOURCE --- |
| 1613 | NymphMessage* app_loadResource(int session, NymphMessage* msg, void* data) { |
| 1614 | NymphMessage* returnMsg = msg->getReplyMessage(); |
| 1615 | |
| 1616 | // Validate the application ID, try to find running instance, else launch new app instance. |
| 1617 | std::string appId = msg->parameters()[0]->getString(); |
| 1618 | std::string name = msg->parameters()[1]->getString(); |
| 1619 | |
| 1620 | // Find the application details. |
| 1621 | std::string* result = new std::string(); |
| 1622 | |
| 1623 | if (appId.empty()) { |
| 1624 | // Use root folder. |
| 1625 | // First check that the name doesn't contain a '/' or '\' as this might be used to create |
| 1626 | // a relative path that breaks security (hierarchy travel). |
| 1627 | if (name.find('/') != std::string::npos || name.find('\\') != std::string::npos) { |
| 1628 | NYMPH_LOG_ERROR("File name contained illegal directory separator character."); |
| 1629 | returnMsg->setResultValue(new NymphType(result, true)); |
| 1630 | msg->discard(); |
| 1631 | |
| 1632 | return returnMsg; |
| 1633 | } |
| 1634 | |
| 1635 | fs::path f = appsFolder + name; |
| 1636 | if (!fs::exists(f)) { |
| 1637 | NYMPH_LOG_ERROR("Failed to find requested file '" + f.string() + "'."); |
| 1638 | returnMsg->setResultValue(new NymphType(result, true)); |
| 1639 | msg->discard(); |
| 1640 | |
| 1641 | return returnMsg; |
| 1642 | } |
| 1643 | |
| 1644 | // Read in file data. |
| 1645 | NYMPH_LOG_INFORMATION("Reading file: " + f.string()); |
| 1646 | std::ifstream fstr(f.string()); |
| 1647 | fstr.seekg(0, std::ios::end); |
| 1648 | size_t size = fstr.tellg(); |
| 1649 | std::string buffer(size, ' '); |
| 1650 | fstr.seekg(0); |
| 1651 | fstr.read(&buffer[0], size); |
| 1652 | result->swap(buffer); |
| 1653 | } |
| 1654 | else { |
| 1655 | // Use App folder. |
| 1656 | // First check that the app really exists, as a safety feature. This should prevent |
| 1657 | // relative path that lead up the hierarchy. |
| 1658 | NymphCastApp app = nc_apps.findApp(appId); |
| 1659 | if (app.id.empty()) { |
| 1660 | NYMPH_LOG_ERROR("Failed to find a matching application for '" + appId + "'."); |
| 1661 | returnMsg->setResultValue(new NymphType(result, true)); |
| 1662 | msg->discard(); |
| 1663 | |
| 1664 | return returnMsg; |
| 1665 | } |
| 1666 | |
| 1667 | // Next check that the name doesn't contain a '/' or '\' as this might be used to create |
| 1668 | // a relative path that breaks security (hierarchy travel). |
| 1669 | if (name.find('/') != std::string::npos || name.find('\\') != std::string::npos) { |
| 1670 | NYMPH_LOG_ERROR("File name contained illegal directory separator character."); |