mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-17 00:01:00 -04:00
Compare commits
3 Commits
56bded9d8d
...
145e11bc39
Author | SHA1 | Date | |
---|---|---|---|
|
145e11bc39 | ||
|
72524adf3f | ||
|
2d4083f03c |
@ -9,6 +9,7 @@
|
|||||||
package charset
|
package charset
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -32,7 +33,7 @@ func EscapeControlHTML(text string, locale translation.Locale, allowed ...rune)
|
|||||||
return streamer.escaped, sb.String()
|
return streamer.escaped, sb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// EscapeControlReaders escapes the unicode control sequences in a provider reader and writer in a locale and returns the findings as an EscapeStatus and the escaped []byte
|
// EscapeControlReaders escapes the unicode control sequences in a provided reader of HTML content and writer in a locale and returns the findings as an EscapeStatus and the escaped []byte
|
||||||
func EscapeControlReader(reader io.Reader, writer io.Writer, locale translation.Locale, allowed ...rune) (escaped *EscapeStatus, err error) {
|
func EscapeControlReader(reader io.Reader, writer io.Writer, locale translation.Locale, allowed ...rune) (escaped *EscapeStatus, err error) {
|
||||||
outputStream := &HTMLStreamerWriter{Writer: writer}
|
outputStream := &HTMLStreamerWriter{Writer: writer}
|
||||||
streamer := NewEscapeStreamer(locale, outputStream, allowed...).(*escapeStreamer)
|
streamer := NewEscapeStreamer(locale, outputStream, allowed...).(*escapeStreamer)
|
||||||
@ -44,6 +45,35 @@ func EscapeControlReader(reader io.Reader, writer io.Writer, locale translation.
|
|||||||
return streamer.escaped, err
|
return streamer.escaped, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EscapeControlStringReader escapes the unicode control sequences in a provided reader of string content and writer in a locale and returns the findings as an EscapeStatus and the escaped []byte
|
||||||
|
func EscapeControlStringReader(reader io.Reader, writer io.Writer, locale translation.Locale, allowed ...rune) (escaped *EscapeStatus, err error) {
|
||||||
|
bufRd := bufio.NewReader(reader)
|
||||||
|
outputStream := &HTMLStreamerWriter{Writer: writer}
|
||||||
|
streamer := NewEscapeStreamer(locale, outputStream, allowed...).(*escapeStreamer)
|
||||||
|
|
||||||
|
for {
|
||||||
|
line, rdErr := bufRd.ReadString('\n')
|
||||||
|
if len(line) > 0 {
|
||||||
|
if err := streamer.Text(line); err != nil {
|
||||||
|
streamer.escaped.HasError = true
|
||||||
|
log.Error("Error whilst escaping: %v", err)
|
||||||
|
return streamer.escaped, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if rdErr != nil {
|
||||||
|
if rdErr != io.EOF {
|
||||||
|
err = rdErr
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err := streamer.SelfClosingTag("br"); err != nil {
|
||||||
|
streamer.escaped.HasError = true
|
||||||
|
return streamer.escaped, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return streamer.escaped, err
|
||||||
|
}
|
||||||
|
|
||||||
// EscapeControlString escapes the unicode control sequences in a provided string and returns the findings as an EscapeStatus and the escaped string
|
// EscapeControlString escapes the unicode control sequences in a provided string and returns the findings as an EscapeStatus and the escaped string
|
||||||
func EscapeControlString(text string, locale translation.Locale, allowed ...rune) (escaped *EscapeStatus, output string) {
|
func EscapeControlString(text string, locale translation.Locale, allowed ...rune) (escaped *EscapeStatus, output string) {
|
||||||
sb := &strings.Builder{}
|
sb := &strings.Builder{}
|
||||||
|
@ -13,8 +13,13 @@ import (
|
|||||||
// TimeStamp defines a timestamp
|
// TimeStamp defines a timestamp
|
||||||
type TimeStamp int64
|
type TimeStamp int64
|
||||||
|
|
||||||
// mock is NOT concurrency-safe!!
|
var (
|
||||||
var mock time.Time
|
// mock is NOT concurrency-safe!!
|
||||||
|
mock time.Time
|
||||||
|
|
||||||
|
// Used for IsZero, to check if timestamp is the zero time instant.
|
||||||
|
timeZeroUnix = time.Time{}.Unix()
|
||||||
|
)
|
||||||
|
|
||||||
// Set sets the time to a mocked time.Time
|
// Set sets the time to a mocked time.Time
|
||||||
func Set(now time.Time) {
|
func Set(now time.Time) {
|
||||||
@ -103,5 +108,5 @@ func (ts TimeStamp) FormatDate() string {
|
|||||||
|
|
||||||
// IsZero is zero time
|
// IsZero is zero time
|
||||||
func (ts TimeStamp) IsZero() bool {
|
func (ts TimeStamp) IsZero() bool {
|
||||||
return int64(ts) == 0
|
return int64(ts) == 0 || int64(ts) == timeZeroUnix
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
gocontext "context"
|
gocontext "context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
gotemplate "html/template"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -342,15 +341,13 @@ func renderReadmeFile(ctx *context.Context, readmeFile *namedBlob, readmeTreelin
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Render failed for %s in %-v: %v Falling back to rendering source", readmeFile.name, ctx.Repo.Repository, err)
|
log.Error("Render failed for %s in %-v: %v Falling back to rendering source", readmeFile.name, ctx.Repo.Repository, err)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
ctx.Data["EscapeStatus"], _ = charset.EscapeControlReader(rd, buf, ctx.Locale)
|
ctx.Data["EscapeStatus"], _ = charset.EscapeControlStringReader(rd, buf, ctx.Locale)
|
||||||
ctx.Data["FileContent"] = strings.ReplaceAll(
|
ctx.Data["FileContent"] = buf.String()
|
||||||
gotemplate.HTMLEscapeString(buf.String()), "\n", `<br>`,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ctx.Data["IsRenderedHTML"] = true
|
ctx.Data["IsPlainText"] = true
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
ctx.Data["EscapeStatus"], err = charset.EscapeControlReader(rd, &charset.BreakWriter{Writer: buf}, ctx.Locale, charset.RuneNBSP)
|
ctx.Data["EscapeStatus"], err = charset.EscapeControlStringReader(rd, buf, ctx.Locale)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Read failed: %v", err)
|
log.Error("Read failed: %v", err)
|
||||||
}
|
}
|
||||||
@ -522,15 +519,6 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
|
|||||||
}
|
}
|
||||||
// to prevent iframe load third-party url
|
// to prevent iframe load third-party url
|
||||||
ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'")
|
ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'")
|
||||||
} else if readmeExist && !shouldRenderSource {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
ctx.Data["IsRenderedHTML"] = true
|
|
||||||
|
|
||||||
ctx.Data["EscapeStatus"], _ = charset.EscapeControlReader(rd, buf, ctx.Locale)
|
|
||||||
|
|
||||||
ctx.Data["FileContent"] = strings.ReplaceAll(
|
|
||||||
gotemplate.HTMLEscapeString(buf.String()), "\n", `<br>`,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
buf, _ := io.ReadAll(rd)
|
buf, _ := io.ReadAll(rd)
|
||||||
|
|
||||||
|
@ -338,7 +338,7 @@ func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, in
|
|||||||
}
|
}
|
||||||
|
|
||||||
repoNamesToCheck = append(repoNamesToCheck, name)
|
repoNamesToCheck = append(repoNamesToCheck, name)
|
||||||
if len(repoNamesToCheck) > setting.Database.IterateBufferSize {
|
if len(repoNamesToCheck) >= setting.Database.IterateBufferSize {
|
||||||
if err = checkUnadoptedRepositories(userName, repoNamesToCheck, unadopted); err != nil {
|
if err = checkUnadoptedRepositories(userName, repoNamesToCheck, unadopted); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -17,11 +17,11 @@
|
|||||||
</h4>
|
</h4>
|
||||||
<div class="ui attached table unstackable segment">
|
<div class="ui attached table unstackable segment">
|
||||||
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
|
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
|
||||||
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsRenderedHTML}} plain-text{{else if .IsTextFile}} code-view{{end}}">
|
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextFile}} code-view{{end}}">
|
||||||
{{if .IsMarkup}}
|
{{if .IsMarkup}}
|
||||||
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
|
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
|
||||||
{{else if .IsRenderedHTML}}
|
{{else if .IsPlainText}}
|
||||||
<pre>{{if .FileContent}}{{.FileContent | Str2html}}{{end}}</pre>
|
<pre>{{if .FileContent}}{{.FileContent | Safe}}{{end}}</pre>
|
||||||
{{else if not .IsTextFile}}
|
{{else if not .IsTextFile}}
|
||||||
<div class="view-raw ui center">
|
<div class="view-raw ui center">
|
||||||
{{if .IsImageFile}}
|
{{if .IsImageFile}}
|
||||||
|
@ -82,11 +82,11 @@
|
|||||||
{{if not (or .IsMarkup .IsRenderedHTML)}}
|
{{if not (or .IsMarkup .IsRenderedHTML)}}
|
||||||
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
|
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
|
||||||
{{end}}
|
{{end}}
|
||||||
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsRenderedHTML}} plain-text{{else if .IsTextSource}} code-view{{end}}">
|
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextSource}} code-view{{end}}">
|
||||||
{{if .IsMarkup}}
|
{{if .IsMarkup}}
|
||||||
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
|
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
|
||||||
{{else if .IsRenderedHTML}}
|
{{else if .IsPlainText}}
|
||||||
<pre>{{if .FileContent}}{{.FileContent | Str2html}}{{end}}</pre>
|
<pre>{{if .FileContent}}{{.FileContent | Safe}}{{end}}</pre>
|
||||||
{{else if not .IsTextSource}}
|
{{else if not .IsTextSource}}
|
||||||
<div class="view-raw ui center">
|
<div class="view-raw ui center">
|
||||||
{{if .IsImageFile}}
|
{{if .IsImageFile}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user