|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Red Hat, Inc. |
| 3 | + * |
| 4 | + * This file is part of csdiff. |
| 5 | + * |
| 6 | + * csdiff is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * any later version. |
| 10 | + * |
| 11 | + * csdiff is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with csdiff. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "parser-json-zap.hh" |
| 21 | + |
| 22 | +struct ZapTreeDecoder::Private { |
| 23 | + std::string timeStamp; |
| 24 | + Defect defPrototype = Defect("OWASP_ZAP_WARNING"); |
| 25 | + const pt::ptree *alertList = nullptr; |
| 26 | + pt::ptree::const_iterator alertIter; |
| 27 | + |
| 28 | + Private() |
| 29 | + { |
| 30 | + this->defPrototype.tool = "owasp-zap"; |
| 31 | + } |
| 32 | + |
| 33 | + void readAlert(Defect *pDef, const pt::ptree &alertNode); |
| 34 | +}; |
| 35 | + |
| 36 | +template <typename TPropList> |
| 37 | +void readNonEmptyProps( |
| 38 | + TEvtList *pDst, |
| 39 | + const pt::ptree &node, |
| 40 | + const DefEvent &evtProto, |
| 41 | + const TPropList &propList) |
| 42 | +{ |
| 43 | + // make our own copy of the given prototype event |
| 44 | + DefEvent evt = evtProto; |
| 45 | + |
| 46 | + for (const auto &evtName : propList) { |
| 47 | + evt.event = evtName; |
| 48 | + evt.msg = valueOf<std::string>(node, evtName); |
| 49 | + if (!evt.msg.empty()) |
| 50 | + pDst->push_back(evt); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void ZapTreeDecoder::Private::readAlert(Defect *pDef, const pt::ptree &alertNode) |
| 55 | +{ |
| 56 | + // read per-defect properties |
| 57 | + *pDef = this->defPrototype; |
| 58 | + pDef->cwe = valueOf<int>(alertNode, "cweid"); |
| 59 | + pDef->imp = (1 < valueOf<int>(alertNode, "riskcode")); |
| 60 | + |
| 61 | + // get "uri" for the key event |
| 62 | + std::string uri; |
| 63 | + const pt::ptree *instList = nullptr; |
| 64 | + if (findChildOf(&instList, alertNode, "instances")) { |
| 65 | + for (const auto &item : *instList) { |
| 66 | + uri = valueOf<std::string>(item.second, "uri"); |
| 67 | + if (!uri.empty()) |
| 68 | + // found! |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + TEvtList &events = pDef->events; |
| 74 | + if (uri.empty() && !events.empty()) |
| 75 | + // fallback to "uri" from the prototype event |
| 76 | + uri = events.front().fileName; |
| 77 | + |
| 78 | + // initialize key event |
| 79 | + DefEvent evt("alert"); |
| 80 | + evt.fileName = uri; |
| 81 | + |
| 82 | + // read "alertRef" if available |
| 83 | + const auto alertRef = valueOf<std::string>(alertNode, "alertRef"); |
| 84 | + if (!alertRef.empty()) |
| 85 | + evt.event += "[" + alertRef + "]"; |
| 86 | + |
| 87 | + // read "alert" if available |
| 88 | + evt.msg = valueOf<std::string>(alertNode, "alert"); |
| 89 | + |
| 90 | + // append the key event |
| 91 | + pDef->keyEventIdx = events.size(); |
| 92 | + events.push_back(evt); |
| 93 | + |
| 94 | + // read other per-alert events if available |
| 95 | + evt.verbosityLevel = /* info event */ 1; |
| 96 | + const auto defProps = { "desc", "solution", "otherinfo", "reference" }; |
| 97 | + readNonEmptyProps(&events, alertNode, evt, defProps); |
| 98 | + |
| 99 | + if (!instList) |
| 100 | + // no instances to go through |
| 101 | + return; |
| 102 | + |
| 103 | + // read per-instance properties |
| 104 | + const auto instProps = { "method", "param", "attack", "evidence" }; |
| 105 | + for (const auto &item : *instList) { |
| 106 | + const pt::ptree &instNode = item.second; |
| 107 | + evt.fileName = valueOf<std::string>(instNode, "uri"); |
| 108 | + if (evt.fileName.empty()) |
| 109 | + // no "uri" for this instance |
| 110 | + continue; |
| 111 | + |
| 112 | + readNonEmptyProps(&events, instNode, evt, instProps); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +ZapTreeDecoder::ZapTreeDecoder(): |
| 117 | + d(new Private) |
| 118 | +{ |
| 119 | +} |
| 120 | + |
| 121 | +ZapTreeDecoder::~ZapTreeDecoder() = default; |
| 122 | + |
| 123 | +void ZapTreeDecoder::readScanProps( |
| 124 | + TScanProps *pDst, |
| 125 | + const pt::ptree *root) |
| 126 | +{ |
| 127 | + const auto version = valueOf<std::string>(*root, "@version"); |
| 128 | + if (!version.empty()) |
| 129 | + (*pDst)["analyzer-version-owasp-zap"] = version; |
| 130 | + |
| 131 | + d->timeStamp = valueOf<std::string>(*root, "@generated"); |
| 132 | +} |
| 133 | + |
| 134 | +bool ZapTreeDecoder::readNode(Defect *pDef) |
| 135 | +{ |
| 136 | + // iterate over sites unless we are processing a site already |
| 137 | + while (!d->alertList || d->alertList->end() == d->alertIter) { |
| 138 | + const pt::ptree *siteNode = this->nextNode(); |
| 139 | + if (!siteNode) |
| 140 | + // failed initialization or EOF |
| 141 | + return false; |
| 142 | + |
| 143 | + if (!findChildOf(&d->alertList, *siteNode, "alerts")) { |
| 144 | + // "alerts" node missing for this site |
| 145 | + d->alertList = nullptr; |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + // initialize iteration over alerts |
| 150 | + d->alertIter = d->alertList->begin(); |
| 151 | + |
| 152 | + if (d->alertList->end() != d->alertIter) { |
| 153 | + // site with alerts found -> update defect prototype based on site |
| 154 | + d->defPrototype.events.clear(); |
| 155 | + const auto siteName = valueOf<std::string>(*siteNode, "@name"); |
| 156 | + if (!siteName.empty() && !d->timeStamp.empty()) { |
| 157 | + // create a prototype "note" event |
| 158 | + DefEvent siteEvt("note"); |
| 159 | + siteEvt.fileName = std::move(siteName); |
| 160 | + siteEvt.msg = "dynamically analyzed on " + d->timeStamp; |
| 161 | + siteEvt.verbosityLevel = /* info event */ 1; |
| 162 | + d->defPrototype.events.push_back(std::move(siteEvt)); |
| 163 | + } |
| 164 | + |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // get the current alert and move to the next one |
| 170 | + const auto itNow = d->alertIter++; |
| 171 | + |
| 172 | + // process the current alert |
| 173 | + d->readAlert(pDef, itNow->second); |
| 174 | + |
| 175 | + return true; |
| 176 | +} |
0 commit comments