-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnoscript.go
141 lines (132 loc) · 3.47 KB
/
noscript.go
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
// Copyright (C) 2019 Christopher E. Miller
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package smallprox
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/tdewolff/parse/html"
"golang.org/x/exp/errors/fmt"
)
type NoscriptResponder struct {
toggle
}
func (er *NoscriptResponder) Response(req *http.Request, resp *http.Response) *http.Response {
if !er.Enabled() {
return resp
}
respContentType := resp.Header.Get("Content-Type")
respMIMEType := respContentType
{
isem := strings.IndexByte(respMIMEType, ';')
if isem != -1 {
respMIMEType = respMIMEType[:isem]
}
}
if respMIMEType == "text/html" {
outbuf := &Mutable{}
noscriptStreamer(resp.Body, outbuf)
resp.Body.Close()
resp.Body = outbuf
} else {
simpleMIME := respMIMEType
if iplus := strings.IndexByte(simpleMIME, '+'); iplus != -1 {
simpleMIME = simpleMIME[:iplus]
}
switch simpleMIME {
case "application/javascript",
"application/x-javascript",
"text/javascript",
"application/ecmascript",
"text/ecmascript":
resp.Body.Close()
resp.Body = ioutil.NopCloser(bytes.NewBufferString("// noscript\n"))
for x := range resp.Header {
delete(resp.Header, x)
}
resp.StatusCode = 521
resp.Status = fmt.Sprintf("%v %v", resp.StatusCode, "Down")
}
}
return resp
}
func noscriptStreamer(r io.Reader, w io.Writer) error {
inScript := false
startTagIsScript := false
var finalErr error
write := func(x []byte) {
if !inScript && finalErr == nil {
_, finalErr = w.Write(x)
}
}
lex := html.NewLexer(r)
lexing:
for finalErr == nil {
tt, data := lex.Next()
switch tt {
case html.ErrorToken:
if lex.Err() != io.EOF && finalErr == nil {
finalErr = lex.Err()
}
break lexing
case html.StartTagToken:
if !inScript && scriptTagRegexp.Match(data) {
// Skip.
inScript = true
startTagIsScript = true
} else if noscriptTagRegexp.Match(data) {
write([]byte("<div data-from-noscript=true"))
} else {
startTagIsScript = false
write(data)
}
case html.StartTagCloseToken: // the > in <foo>
startTagIsScript = false
write(data)
case html.StartTagVoidToken: // self closing, after StartTagToken
if startTagIsScript {
// Skip.
inScript = false
startTagIsScript = false
} else {
write(data)
}
case html.EndTagToken:
if scriptTagEndRegexp.Match(data) {
// Skip.
inScript = false
startTagIsScript = false
} else if noscriptTagEndRegexp.Match(data) {
write([]byte("</div>"))
} else {
write(data)
}
case html.AttributeToken:
if eventAttribRegexp.Match(data) {
// Skip.
} else if hrefJsAttribRegexp.Match(data) {
write([]byte(` href=#noscript`))
} else if srcJsAttribRegexp.Match(data) {
write([]byte(` src=#noscript`))
} else {
write(data)
}
default:
write(data)
}
}
return finalErr
}
var scriptTagRegexp = regexp.MustCompile(`(?si)^\s*<script$`)
var scriptTagEndRegexp = regexp.MustCompile(`(?si)^\s*</\s*script\s*>`)
var noscriptTagRegexp = regexp.MustCompile(`(?si)^\s*<noscript$`)
var noscriptTagEndRegexp = regexp.MustCompile(`(?si)^\s*</\s*noscript\s*>`)
var eventAttribRegexp = regexp.MustCompile(`(?si)^\s*on\w`)
var hrefJsAttribRegexp = regexp.MustCompile(`(?si)^\s*href\s*=.*javascript:`)
var srcJsAttribRegexp = regexp.MustCompile(`(?si)^\s*src\s*=.*javascript:`)