1# OpenBMC Webserver Development 2 31. ### Performance targets 4 As OpenBMC is intended to be deployed on an embedded system, care should be 5 taken to avoid expensive constructs, and memory usage. In general, our 6 performance and metric targets are: 7 8 - Binaries and static files should take up < 1MB of filesystem size 9 - Memory usage should remain below 10MB at all times 10 - Application startup time should be less than 1 second on target hardware 11 (AST2500) 12 132. ### Asynchronous programming 14 Care should be taken to ensure that all code is written to be asynchronous in 15 nature, to avoid blocking methods from stopping the processing of other 16 tasks. At this time the webserver uses boost::asio for it async framework. 17 Threads should be avoided if possible, and instead use async tasks within 18 boost::asio. 19 203. ### Secure coding guidelines 21 Secure coding practices should be followed in all places in the webserver 22 23 In general, this means: 24 - All buffer boundaries must be checked before indexing or using values 25 - All pointers and iterators must be checked for null before dereferencing 26 - All input from outside the application is considered untrusted, and should 27 be escaped, authorized and filtered accordingly. This includes files in 28 the filesystem. 29 - All error statuses are checked and accounted for in control flow. 30 - Where applicable, noexcept methods should be preferred to methods that use 31 exceptions 32 - Explicitly bounded types should be preferred over implicitly bounded types 33 (like std::array<int, size> as opposed to int[size]) 34 - no use of [Banned 35 functions](https://github.com/intel/safestringlib/wiki/SDL-List-of-Banned-Functions 36 "Banned function list") 37 384. ### Error handling 39 Error handling should be constructed in such a way that all possible errors 40 return valid HTTP responses. The following HTTP codes will be used commonly 41 - 200 OK - Request was properly handled 42 - 201 Created - Resource was created 43 - 401 Unauthorized - Request didn't posses the necessary authentication 44 - 403 Forbidden - Request was authenticated, but did not have the necessary 45 permissions to accomplish the requested task 46 - 404 Not found - The url was not found 47 - 500 Internal error - Something has broken within the OpenBMC web server, 48 and should be filed as a bug 49 50 Where possible, 307 and 308 redirects should be avoided, as they introduce 51 the possibility for subtle security bugs. 52 535. ### Startup times 54 Given that the most common target of OpenBMC is an ARM11 processor, care 55 needs to be taken to ensure startup times are low. In general this means: 56 57 - Minimizing the number of files read from disk at startup. Unless a 58 feature is explicitly intended to be runtime configurable, its logic 59 should be "baked in" to the application at compile time. For cases where 60 the implementation is configurable at runtime, the default values should 61 be included in application code to minimize the use of nonvolatile 62 storage. 63 - Avoid excessive memory usage and mallocs at startup. 64 656. ### Compiler features 66 - At this point in time, the webserver sets a number of security flags in 67 compile time options to prevent misuse. The specific flags and what 68 optimization levels they are enabled at are documented in the 69 CMakeLists.txt file. 70 - Exceptions are currently enabled for webserver builds, but their use is 71 discouraged. Long term, the intent is to disable exceptions, so any use 72 of them for explicit control flow will likely be rejected in code review. 73 Any use of exceptions should be cases where the program can be reasonably 74 expected to crash if the exception occurs, as this will be the future 75 behavior once exceptions are disabled. 76 - Run time type information is disabled 77 - Link time optimization is enabled 78 797. ### Authentication 80 The webserver shall provide the following authentication mechanisms. 81 - Basic authentication 82 - Cookie authentication 83 - Token authentication 84 85 There shall be connection between the authentication mechanism used and 86 resources that are available over it. The webserver shall employ an 87 authentication scheme that is in line with the rest of OpenBMC, and allows 88 users and privileges to be provisioned from other interfaces. 89 908. ### Web security 91 The OpenBMC webserver shall follow the latest OWASP recommendations for 92 authentication, session management, and security. 93 949. ### Performance 95 The performance priorities for the OpenBMC webserver are (in order): 96 1. Code is readable and clear 97 2. Code follows secure guidelines 98 3. Code is performant, and does not unnecessarily abstract concepts at the 99 expense of performance 100 4. Code does not employ constructs which require continuous system 101 resources, unless required to meet performance targets. (example: 102 caching sensor values which are expected to change regularly) 103 10410. ### Abstraction/interfacing 105 In general, the OpenBMC webserver is built using the data driven design. 106 Abstraction and Interface guarantees should be used when multiple 107 implementations exist, but for implementations where only a single 108 implementation exists, prefer to make the code correct and clean rather than 109 implement a concrete interface. 110 11111. ### phosphor webui 112 The webserver should be capable of hosting phosphor-webui, and implementing 113 the required flows to host the application. In general, all access methods 114 should be available to the webui. 115 11612. ### Redfish 117 bmcweb's Redfish implementation, including Redfish OEM Resources, shall 118 conform to the Redfish specification. Please keep bmcweb's [Redfish support 119 document](https://github.com/openbmc/bmcweb/blob/master/Redfish.md) updated. 120 OEM schemas should conform and be developed in line with the rules in 121 [OEM SCHEMAS](https://github.com/openbmc/bmcweb/blob/master/OEM_SCHEMAS.md). 122 12313. ### Common errors 124 A number of examples of common errors are captured in the common errors doc. 125 It is recommended that developers read and understand all of them before 126 starting any openbmc development. 127 [Common Errors](https://github.com/openbmc/bmcweb/blob/master/COMMON_ERRORS.md). 128 12914. ### Commit messages 130 Project commit message formatting should be obeyed 131 [link](https://github.com/openbmc/docs/blob/master/CONTRIBUTING.md#formatting-commit-messages) 132 133 Commit messages should answer the following questions: 134 - Why are the changes useful? Given that bmcweb is a user-facing daemon, 135 commits adding new functionality should include statements about how the 136 commit in question is useful to the user. 137 138 - What changes would a user expect to see? This includes new parameters, new 139 resources, and new or changed properties. Any route changes should be 140 explicitly called out. 141 142 - Are there compatibility concerns? Is this change backward compatible for 143 clients? If not, what commit would be broken, and how old is it? Have 144 clients been warned? (ideally on the mailing list) link the discussion. 145 146 Commit messages should be line wrapped 50/72. 147 14815. ### Compatibility 149 "Don't make your users mad" Greg K-H 150 [source](https://git.sr.ht/~gregkh/presentation-application_summit/tree/main/keep_users_happy.pdf) 151 152 The kernel has very similar rules around compatibility that we should aspire 153 to follow in the footsteps of. 154 155 To that end, bmcweb will do its' best to insulate clients from breaking api 156 changes. Being explicit about this ensures that clients can upgrade their 157 OpenBMC version without issue, and resolves a significant bottleneck in 158 getting security patches deployed to users. Any change that's visible to a 159 user is potentially a breaking change, but requiring _all_ visible changes to 160 be configurable would increase the software complexity, therefore bmcweb 161 makes exceptions for things which a client is reasonably expected to code 162 against: 163 - New items added to a collection 164 - Changes in UID for hypermedia resources (In line with Redfish spec) 165 - New properties added to a resource 166 - New versions of a given schema 167 168 Special note: Code exists in bmcweb that is missing upstream backends to 169 make it function. Given that compatibility requires the ability to use and 170 test the feature in question, changes to these methods, including outright 171 removal, does not constitute a breaking change. 172 173 Security: There may be cases where maintainers make explicit breaking changes 174 in the best interest of security; In these rare cases, the maintainers and 175 contributors will endeavor to avoid breaking clients as much as is 176 technically possible, but as with all security, impact will need to be 177 weighed against the security impact of not making changes, and judgement 178 calls will be made, with options to allow providing the old behavior. 179 180## clang-tidy 181 182clang-tidy is a tool that can be used to identify coding style violations, bad 183design patterns, and bug prone constructs. The checks are implemented in the 184.clang-tidy file in the root of bmcweb, and are expected to be passing. To 185run, the best way is to run the checks in yocto. 186 187``` 188# check out meta-clang in your openbmc root 189cd openbmc 190git clone https://github.com/kraj/meta-clang 191 192# add the meta-clang layer to BBLAYERS in $BBPATH/conf/bblayers.conf 193<path_to_your_build_dir>/meta-clang 194 195# Add this line to $BBPATH/conf/local.conf to build bmcweb with clang 196TOOLCHAIN_pn-bmcweb = "clang" 197 198# and build 199bitbake bmcweb 200 201# Open devshell (this will open a shell) 202bitbake -c devshell bmcweb 203 204# cd into the work dir 205cd oe-workdir/bmcweb-1.0+git999 206# run clang tidy 207clang-tidy --header-filter=".*" -p . $BBPATH/workspace/sources/bmcweb/src/webserver_main.cpp 208``` 209