SecurityFirst, when we accept data via the /api/statuses endpoint, we can utilize one ormore of the functions in html/template to prevent certain types of data from beingstored. The functions are as follows: • template.HTMLEscapeString: This encodes HTML tags and renders the resulting string as non-HTML content • template.JSEscapeString(): This encodes JavaScript-specific pieces of a string to prevent proper renderingFor the purpose of keeping this simple for potential output through HTML, we canjust apply HTMLEscapeString() to our data, which will disable any JavaScript callsfrom executing: func StatusCreate(w http.ResponseWriter, r *http.Request) {Response := CreateResponse{}UserID := r.FormValue(\"user\")Status := r.FormValue(\"status\")Token := r.FormValue(\"token\")ConsumerKey := r.FormValue(\"consumer_key\") Status = template.HTMLEscapeString(Status)This makes the data escape on the input (StatusCreate) side. If we want to addJavaScript escaping (which, as noted earlier, may not be necessary), it should comebefore the HTML escaping, as noted here: Status = template.JSEscapeString(Status) Status = template.HTMLEscapeString(Status)If in lieu of escaping on the input side, we wish to do it on the output side, the sametemplate escape calls can be made as part of the respective status request API calls,like /api/statuses: func StatusRetrieve(w http.ResponseWriter, r *http.Request) { var Response StatusResponse w.Header().Set(\"Access-Control-Allow-Origin\", \"*\") loggedIn := CheckLogin(w, r) if loggedIn {} else { statuses,_ := Database.Query(\"select * from user_status where user_id=? order by user_status_timestamp desc\",Session.UID) for statuses.Next() {status := Status{} [ 230 ]
Chapter 11 statuses.Scan(&status.ID, &status.UID, &status.Time, &status.Text) status.Text = template.JSEscapeString(status.Text) status.Text = template.HTMLEscapeString(status.Text) Response.Statuses = append(Response.Statuses, status) }If we want to attempt to detect and log attempts to pass specific HTML elements intoinput elements, we can create a new logger for XSS attempts and capture any textthat matches a <script> element, a <iframe> element, or any other element.Doing this can be as complex as a tokenizer or a more advanced security package oras simple as a regular expression match, as we will see in the following examples.First, we will look at the code in our logging setup:var ( Database *log.Logger Authentication *log.Logger Errors *log.Logger Questionable *log.Logger)And the changes in our initialization code are as follows: questlog, err := os.OpenFile(\"injections.log\", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) if err != nil { log.Println(err) } Questionable = log.New(questlog, \"XSS:\", log.Ldate|log.Ltime)And then, make the following changes back in our application's StatusCreatehandler: isinject, _ := regexp.MatchString(\"<(script|iframe).*\",Status) if isinject { }Detecting tags this way, through regular expressions, is not airtight nor is it intendedto be. Remember that we'll be sanitizing the data either on the input side or theoutput side, so if we can catch attempts through this method, it will give us someinsight into potentially malicious attempts against our application.If we want to be more idiomatic and comprehensive, we can simply sanitize the textand compare it with the original. If the two values do not match, we can surmise thatHTML was included. [ 231 ]
SecurityThis does mean that we'll get a positive for innocuous HTML tags such as bold tagsor table tags.Using server-side frameworks in GoWe would be remiss if, while detailing how to build a web service from scratch, wedidn't at least touch upon integrating or exclusively using some existing frameworks.Although you'll never get the same experience by plugging in such a framework asyou would by designing one from scratch, for practical purposes, there's often noreason to reinvent the wheel when you want to start a project.Go has quite a few readily available and mature web/HTML frameworks, but italso has a handful of noteworthy frameworks that are specifically designed for webservices with some of the delivery methods and additional hooks that you mightexpect to see.By some measurements, it's fair to describe Gorilla as a framework; however, as thename implies, it's a little basic.Whether you use an existing framework or choose to build your own (either for theexperience or to completely customize it due to business requirements), you shouldprobably consider doing a bitWe'll briefly look at a few of these frameworks and how they can simplify thedevelopment of small web-based projects.Tiger TonicTiger Tonic is specifically an API-centric framework, so we'll mention it first in thissection. This takes a very idiomatic Go approach to developing JSON web services.Responses are primarily intended to be in JSON only and multiplexing should seempretty familiar to the style introduced by Gorilla.Tiger Tonic also provides some quality logging features that allow you to funnellogs directly into the Apache format for more detailed analysis. Most importantly, ithandles middleware in a way that allows some conditional operations based on theresults of the middleware itself. You can read more about Tiger Tonic at https://github. com/rcrowley/go-tigertonic or download the documentation using the go get github.com/rcrowley/ go-tigertonic command. [ 232 ]
Chapter 11MartiniThe web framework Martini is one of the more popular web frameworks for therelatively young Go language, largely due to its similarity in design to both theNode.js framework Express and the popular Ruby-on-Rails framework Sinatra.Martini also plays extraordinarily well with middleware, so much so that it'soften brought in exclusively for this purpose. It also comes with a few standardmiddleware handlers like Logger() that takes care of logging in and out andRecovery() that recovers from panics and returns HTTP errors.Martini is built for a large swath of web projects, and it may include more thanwhat is necessary for a simple web service; however, it is an excellent all-inclusiveframework that's worth checking out. You can read more about Martini at https://github.com/go- martini/martini or download the documentation using the go get github.com/go-martini/martini command.GojiUnlike Martini, which is quite comprehensive and far-reaching, the Goji frameworkis minimalistic and lean. The primary advantages of Goji are its incredibly quickrouting system, a low overhead for additional garbage collection, and robustmiddleware integrations.Goji uses Alice for middleware, which we briefly touched on in an earlier chapter. You can read more about the Goji micro framework at https://goji.io/ and download it with the go get github.com/zenazn/goji and go get github.com/ zenazn/goji/web commands. [ 233 ]
SecurityBeegoBeego is a more complex type of framework that has quickly become one of the morepopular Go frameworks for web projects.Beego has a lot of features that can come in handy for a web service, despite theadditional feature set that is largely intended for rendered web pages. The frameworkcomes with its own sessions, routing, and cache modules, and also includes a livemonitoring process that allows you to analyze your project dynamically. You can read more about Beego at http://beego.me/ or download it using the go get github.com/astaxie/beego command.SummaryIn this final chapter, we looked at how to keep our web service as airtight as possiblefrom common security issues and looked at solutions to mitigate issues if and whena breach happens.As APIs scale both in popularity and scope, it is paramount to ensure that users andtheir data are safe.We hope you have been (and will be) able to utilize these security best practices andtools to improve the overall reliability and speed of your application.While our primary project—the social network—is by no means a complete orcomprehensive project, we've broken down aspects of such a project to demonstraterouting, caching, authentication, display, performance, and security.If you wish to continue expanding the project, feel free to augment, fork, or clonethe example at https://github.com/nkozyra/masteringwebservices. We'd loveto see the project continue to serve as a demonstration of features and best practicesrelated to web services and APIs in Go. [ 234 ]
IndexA application designing 24, 25Access-Control-Allow-Origin reference link 150 Asynchronous JavaScript (AJAX) 155 authenticationACID 133Advanced Message Queuing Protocol about 87 handling 225, 226 (AMQP) 182Amazon Web Services B about 176 Beego Go to interface, using 176 about 234AngularJS URL 234 about 157 API, consuming 157, 158 binary data URL 158 handling 58, 177-180Apache Go, using 127, 128 Binary JSON formatApache JMeter URL 138 about 5 URL 5 brute-force attemptsAPI log 219-225 about 1 preventing 219 architectures 28 consuming, with AngularJS 157, 158 C consuming, with jQuery 155, 156 logic, separating 76 caching sessions, enabling 130, 131 disk-based caching 194-196 versions, handling 38, 63-69 frontend caching proxy, using in Go 207API access implementing, as middleware 206-208 services, using 49-51 in distributed memory 201 simple interface, using 51-54 NoSQL, using as cache store 201-205API-consuming frontend requests 193 client-side Angular application, URL 208 creating 160-163 CDNs setting up 159 handling 177-180App Engine SDK URL 3 client secure connection 122, 123 client-side Angular application creating, for web service 160-163 [ 235 ]
client-side frameworks error messages using 153, 154 expanding 76-78client-side tools 7 Exec() method 19cloud environments F deploying in 175Comma Separated Values (CSV) 32 file uploadconnections existence, checking 180 enabling, password used 141-143 formats enabling, username used 141, 142 Comma Separated Values (CSV) 32Couchbase JSON 29, 30 about 4 selecting 28 URL 4 XML 30Cross-Site Request Forgery (CSRF) YAML 30, 31 log 227Cross-Site Scripting. See XSS frontend caching proxyCRUD 34 using, in Go 207, 208D frontend interface creating 150-152data logging in 153 serving, from datastore to client 19, 20 setting, via HTTP 16 G using, from other OAuth services 117-121 Genghisdatabase URL 139 setting up 8, 9 Godatasets and NGINX, as reverse proxies 128-130 dummy dataset, URL 5 authentication, handling 225, 226 predefined datasets, using 5 client, secure connection 122, 123 client-side frameworks, using 153, 154disk-based caching custom routers, writing 42-47 about 194-196 installing 2, 3 filtering, enabling 196, 197 language support plugin, URL 7 transforming, into middleware 197-201 NoSQL, using 132-135 OAuth 94-99Docker RabbitMQ used 182-187 deploying with 174, 175 rate limiting 208-211 server-side frameworks 232E server-side frameworks, rendering 164, 165 serving, through reverse proxies 126Eclipse URL 2 about 6 using, with Apache 127, 128 URL 6 goagaine-mails URL 174 sending, net used 180-182 sending, smtp used 180-182 go-av URL 50endpoints adding 35-37 Goclipse plugin URL 6error logging handling, for security 216-218 URL 218 [ 236 ]
go-curl J URL 50 JMeter. See Apache JMeterGoji jQuery about 233 URL 233 about 154, 155 URL 156Gorilla used, for consuming API 155, 156 about 13, 14 JSON 29, 30, 85 advanced routers, using 47, 48 JSON API Server (JAS) URL 15 URL 67 using, for JSON-RPC 48, 49 JSON-RPC web toolkit 35 Gorilla, using 48, 49 URL 28goyaml URL 30HLheaders link header 39 setting, to add clients detail 20, 21 LiteIDEHTTP about 7 actions and methods, comparing 32, 33 URL 7 data, setting via 16 MHTTP package about 9, 10 Manners Hello World 10-11 URL 174 using 172-174HTTPS forcing 87-89 Martini about 233Hypermedia as the Engine of Application URL 233 State (HATEOAS) 27, 28 MemcachedHypertext Application Language (HAL) 39 about 135-138 URL 138I mgoidempotence 34 URL 139injection mitigation middleware handling 226 caching, implementing as 206, 207input validation disk cache, transforming into 197-201 rate limiting, implementing as 211, 212 handling 226 using, to reduce cruft 190-193Integrated Development Environment (IDE) Model-View-Controller (MVC) 157 client-side tools 7 MongoDB Eclipse 6 IntelliJ IDEA 7 about 138-141 LiteIDE 7 URL 139 selecting 5 multiplexer Sublime Text 6 URL 15Internet Engineering Task Force URL 63 [ 237 ]
MySQL PUT verb method connecting to 16-18 versus PATCH method 34 installing 3 URL 3 QN Query() method 19 QueryRow() method 19Nginx about 4 R commands 4 RabbitMQNoSQL URL 187 Memcached 135-138 with Go 182-187 MongoDB 138-141 using, as cache store 201-205 rate limiting using, in Go 132-135 about 208-211 implementing, as middleware 211O RedisOpen Authentication (OAuth) about 3 about 63, 94-99 URL 4 code, URL 108, 112 data, using from services 117-121 Remote procedure calls (RPC) 28 Representational state transfer (REST)OPTIONS sharing 82-84 about 25 self-description 26, 27output URL 28 validating 227 Request for Comments (RFC) URL 82P requests creating, on users behalf 100-104pagination RESTful design with link header, allowing 39, 40 sessions 131, 132 reverse proxiesPATCH method Go, serving through 126 versus PUT verb method 34 Go, using as 128-130 NGINX, using as 128-130Poodlebleed route URL 87 about 15, 16 building 12, 13Poster URL 16 URL 80 routers advanced routers, using in Gorilla 47, 48Postman custom routers, writing 42-47 URL 80 Sprocess control Manners, using for servers 172 salt supervisor, using 171, 172 generating 92-94 using, to keep API running 171 [ 238 ]project structures 170, 171Push API URL 157
security U about 87 error logging, handling 216-218 URI 27 usersserver-side frameworks Beego 234 allowing, to connect 143-146 Goji 233 authentication, allowing 91 Martini 233 connecting, to other services 106-112 rendering, in Go 164, 165 registration, allowing 91 Tiger Tonic 232 updating, via web service 78-80 using 232 viewing 163, 164services V using, for API access 49-51 valuable error informationsessions returning 54-57 enabling, for API 130, 131 in RESTful design 131, 132 Varnish URL 208single-page application (SPA) 154social network API W designing 62, 63 web serviceSPDY used, for saving state 112-116 used, for updating users 78-80 about 212, 213 support, detecting 213 websocket.go URL 212 URL 73SQL best practices 226, 227 WebSocketsSquid about 69-75 URL 208 advantages 70, 71StartSSL browsers 73 URL 91state World Wide Web Consortium (W3C) 26 saving, with web service 112-117status update X creating 165-167Sublime Text XML 30, 85 about 6 XSS URL 6supervisor protecting against 228-232 using 171, 172 YT YAMLTiger Tonic about 30, 31, 85 about 232 URL 85 URL 232 [ 239 ]TLS support, adding 89, 90Tom's Obvious, Minimal Language (TOML) 86, 87
Thank you for buying Mastering Go Web ServicesAbout Packt PublishingPackt, pronounced 'packed', published its first book, Mastering phpMyAdmin for EffectiveMySQL Management, in April 2004, and subsequently continued to specialize in publishinghighly focused books on specific technologies and solutions.Our books and publications share the experiences of your fellow IT professionals in adaptingand customizing today's systems, applications, and frameworks. Our solution-based booksgive you the knowledge and power to customize the software and technologies you're usingto get the job done. Packt books are more specific and less general than the IT books you haveseen in the past. Our unique business model allows us to bring you more focused information,giving you more of what you need to know, and less of what you don't.Packt is a modern yet unique publishing company that focuses on producing quality,cutting-edge books for communities of developers, administrators, and newbies alike.For more information, please visit our website at www.packtpub.com.About Packt Open SourceIn 2010, Packt launched two new brands, Packt Open Source and Packt Enterprise, in orderto continue its focus on specialization. This book is part of the Packt Open Source brand,home to books published on software built around open source licenses, and offeringinformation to anybody from advanced developers to budding web designers. The OpenSource brand also runs Packt's Open Source Royalty Scheme, by which Packt gives a royaltyto each open source project about whose software a book is sold.Writing for PacktWe welcome all inquiries from people who are interested in authoring. Book proposals shouldbe sent to [email protected]. If your book idea is still at an early stage and you wouldlike to discuss it first before writing a formal book proposal, then please contact us; one of ourcommissioning editors will get in touch with you.We're not just looking for published authors; if you have strong technical skills but no writingexperience, our experienced editors can help you develop a writing career, or simply get someadditional reward for your expertise.
Mastering Concurrency in GoISBN: 978-1-78398-348-3 Paperback: 328 pagesDiscover and harness Go's powerful concurrencyfeatures to develop and build fast, scalablenetwork systems1. Explore the core syntaxes and language features that enable concurrency in Go.2. Understand when and where to use concurrency to keep data consistent and applications non-blocking, responsive, and reliable.3. A practical approach to utilize application scaffolding to design highly-scalable programs that are deeply rooted in go routines and channels.Building Your First Applicationwith Go [Video]ISBN: 978-1-78328-381-1 Duration: 02:47 HoursGet practical experience and learn basic skills whiledeveloping an application with Go1. Learn the features and various aspects of Go programming.2. Create a production-ready web application by the end of the course.3. Master time-proven design patterns for creating highly reusable application components.Please check www.PacktPub.com for information on our titles
Go Programming BlueprintsISBN: 978-1-78398-802-0 Paperback: 274 pagesBuild real-world, production-ready solutions in Gousing cutting edge technology and techniques1. Learn to apply the nuances of the Go language, and get to know the open source community that surrounds it to implement a wide range of start-up quality projects.2. Write interesting, and clever but simple code, and learn skills and techniques that are directly transferrable to your own projects.3. Discover how to write code capable of delivering massive world-class scale performance and availability.Flask Framework CookbookISBN: 978-1-78398-340-7 Paperback: 258 pagesOver 80 hands-on recipes to help you createsmall-to-large web applications using Flask1. Get the most out of the powerful Flask framework while remaining flexible with your design choices.2. Build end-to-end web applications, right from their installation to the post-deployment stages.3. Packed with recipes containing lots of sample applications to help you understand the intricacies of the code.Please check www.PacktPub.com for information on our titles
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264