enforce limits better
This commit is contained in:
parent
f482c8972e
commit
a66d31f447
2
Makefile
2
Makefile
@ -8,7 +8,7 @@ VENDOR_JS=$(REPO)/contrib/js/vendor
|
||||
SRND_DIR=$(REPO)/contrib/backends/srndv2
|
||||
SRND=$(REPO)/srndv2
|
||||
|
||||
all: build
|
||||
all: clean build
|
||||
|
||||
build: js srnd
|
||||
|
||||
|
@ -596,7 +596,7 @@ func (self *NNTPDaemon) Run() {
|
||||
nntp.Pack()
|
||||
file := self.store.CreateFile(nntp.MessageID())
|
||||
if file != nil {
|
||||
err = nntp.WriteTo(file)
|
||||
err = nntp.WriteTo(file, MaxMessageSize)
|
||||
file.Close()
|
||||
if err == nil {
|
||||
self.loadFromInfeed(nntp.MessageID())
|
||||
|
@ -381,7 +381,7 @@ func (self *httpFrontend) poll() {
|
||||
f := self.daemon.store.CreateFile(nntp.MessageID())
|
||||
if f != nil {
|
||||
b := new(bytes.Buffer)
|
||||
err := nntp.WriteTo(b)
|
||||
err := nntp.WriteTo(b, self.daemon.messageSizeLimitFor(nntp.Newsgroup()))
|
||||
if err == nil {
|
||||
r := bufio.NewReader(b)
|
||||
var msg *mail.Message
|
||||
@ -955,7 +955,7 @@ func (self *httpFrontend) handle_postRequest(pr *postRequest, b bannedFunc, e er
|
||||
e(errors.New("failed to store article"))
|
||||
return
|
||||
} else {
|
||||
err = nntp.WriteTo(f)
|
||||
err = nntp.WriteTo(f, self.daemon.messageSizeLimitFor(nntp.Newsgroup()))
|
||||
f.Close()
|
||||
if err == nil {
|
||||
go self.daemon.loadFromInfeed(nntp.MessageID())
|
||||
|
@ -6,18 +6,25 @@ import (
|
||||
)
|
||||
|
||||
type LineWriter struct {
|
||||
w io.Writer
|
||||
w io.Writer
|
||||
Left int64
|
||||
}
|
||||
|
||||
func NewLineWriter(w io.Writer) *LineWriter {
|
||||
func NewLineWriter(w io.Writer, limit int64) *LineWriter {
|
||||
return &LineWriter{
|
||||
w: w,
|
||||
w: w,
|
||||
Left: limit,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LineWriter) Write(data []byte) (n int, err error) {
|
||||
n = len(data)
|
||||
if l.Left <= 0 {
|
||||
err = ErrOversizedMessage
|
||||
return
|
||||
}
|
||||
data = bytes.Replace(data, []byte{13, 10}, []byte{10}, -1)
|
||||
_, err = l.w.Write(data)
|
||||
n, err = l.w.Write(data)
|
||||
l.Left -= int64(n)
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -83,9 +83,9 @@ type NNTPMessage interface {
|
||||
// all headers
|
||||
Headers() ArticleHeaders
|
||||
// write out everything
|
||||
WriteTo(wr io.Writer) error
|
||||
WriteTo(wr io.Writer, limit int64) error
|
||||
// write out body
|
||||
WriteBody(wr io.Writer) error
|
||||
WriteBody(wr io.Writer, limit int64) error
|
||||
// attach a file
|
||||
Attach(att NNTPAttachment)
|
||||
// get the plaintext message if it exists
|
||||
@ -175,7 +175,7 @@ func signArticle(nntp NNTPMessage, seed []byte) (signed *nntpArticle, err error)
|
||||
signed.signedPart = &nntpAttachment{}
|
||||
// write body to sign buffer
|
||||
mw := io.MultiWriter(sha, signed.signedPart)
|
||||
err = nntp.WriteTo(mw)
|
||||
err = nntp.WriteTo(mw, MaxMessageSize)
|
||||
mw.Write([]byte{10})
|
||||
if err == nil {
|
||||
// build keypair
|
||||
@ -198,15 +198,20 @@ func signArticle(nntp NNTPMessage, seed []byte) (signed *nntpArticle, err error)
|
||||
return
|
||||
}
|
||||
|
||||
func (self *nntpArticle) WriteTo(wr io.Writer) (err error) {
|
||||
func (self *nntpArticle) WriteTo(wr io.Writer, limit int64) (err error) {
|
||||
// write headers
|
||||
var n int
|
||||
hdrs := self.headers
|
||||
for hdr, hdr_vals := range hdrs {
|
||||
for _, hdr_val := range hdr_vals {
|
||||
wr.Write([]byte(hdr))
|
||||
wr.Write([]byte(": "))
|
||||
wr.Write([]byte(hdr_val))
|
||||
_, err = wr.Write([]byte{10})
|
||||
n, err = wr.Write([]byte(hdr))
|
||||
limit -= int64(n)
|
||||
n, err = wr.Write([]byte(": "))
|
||||
limit -= int64(n)
|
||||
n, err = wr.Write([]byte(hdr_val))
|
||||
limit -= int64(n)
|
||||
n, err = wr.Write([]byte{10})
|
||||
limit -= int64(n)
|
||||
if err != nil {
|
||||
log.Println("error while writing headers", err)
|
||||
return
|
||||
@ -214,14 +219,19 @@ func (self *nntpArticle) WriteTo(wr io.Writer) (err error) {
|
||||
}
|
||||
}
|
||||
// done headers
|
||||
_, err = wr.Write([]byte{10})
|
||||
n, err = wr.Write([]byte{10})
|
||||
limit -= int64(n)
|
||||
if err != nil {
|
||||
log.Println("error while writing body", err)
|
||||
return
|
||||
}
|
||||
|
||||
// write body
|
||||
err = self.WriteBody(wr)
|
||||
if limit > 0 {
|
||||
// write body
|
||||
err = self.WriteBody(wr, limit)
|
||||
} else {
|
||||
err = ErrOversizedMessage
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -359,10 +369,15 @@ func (self *nntpArticle) Attach(att NNTPAttachment) {
|
||||
self.attachments = append(self.attachments, att)
|
||||
}
|
||||
|
||||
func (self *nntpArticle) WriteBody(wr io.Writer) (err error) {
|
||||
func (self *nntpArticle) WriteBody(wr io.Writer, limit int64) (err error) {
|
||||
// this is a signed message, don't treat it special
|
||||
var n int
|
||||
if self.signedPart != nil {
|
||||
_, err = wr.Write(self.signedPart.Bytes())
|
||||
n, err = wr.Write(self.signedPart.Bytes())
|
||||
limit -= int64(n)
|
||||
if limit <= 0 {
|
||||
err = ErrOversizedMessage
|
||||
}
|
||||
return
|
||||
}
|
||||
self.Pack()
|
||||
@ -375,7 +390,8 @@ func (self *nntpArticle) WriteBody(wr io.Writer) (err error) {
|
||||
|
||||
boundary, ok := params["boundary"]
|
||||
if ok {
|
||||
w := multipart.NewWriter(NewLineWriter(wr))
|
||||
nlw := NewLineWriter(wr, limit)
|
||||
w := multipart.NewWriter(nlw)
|
||||
|
||||
err = w.SetBoundary(boundary)
|
||||
if err == nil {
|
||||
@ -411,9 +427,13 @@ func (self *nntpArticle) WriteBody(wr io.Writer) (err error) {
|
||||
}
|
||||
err = w.Close()
|
||||
w = nil
|
||||
if nlw.Left <= 0 {
|
||||
err = ErrOversizedMessage
|
||||
}
|
||||
} else {
|
||||
nlw := NewLineWriter(wr, limit)
|
||||
// write out message
|
||||
_, err = io.WriteString(wr, self.message)
|
||||
_, err = io.WriteString(nlw, self.message)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user