diff options
| author | bptato <nincsnevem662@gmail.com> | 2024-12-05 20:20:07 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2024-12-05 20:20:07 +0100 |
| commit | a3dc167925f7255b35c11f8c7e6607fb6e0d6b52 (patch) | |
| tree | 814c57ce0d864c019ba670e19d29324aebeea673 /src | |
| parent | 03dae38fdf2e7d9da912701e851a5d0684947d58 (diff) | |
| download | chawan-a3dc167925f7255b35c11f8c7e6607fb6e0d6b52.tar.gz | |
mimetypes: refactor, use mime.types for inline image extensions
* remove std/streams use from mime.types; mmap and parse directly
* use mime.types for inline image extensions
* add some jpeg file extensions
Latter came up because I was trying to add a format locally and it
wouldn't recognize it on images from my file system (i.e. by extension).
As a security measure we still do not allow additional extensions for
predefined inline image types.
Diffstat (limited to 'src')
| -rw-r--r-- | src/config/config.nim | 39 | ||||
| -rw-r--r-- | src/config/mimetypes.nim | 99 | ||||
| -rw-r--r-- | src/html/dom.nim | 18 | ||||
| -rw-r--r-- | src/html/env.nim | 7 | ||||
| -rw-r--r-- | src/local/container.nim | 1 | ||||
| -rw-r--r-- | src/local/pager.nim | 4 | ||||
| -rw-r--r-- | src/main.nim | 2 | ||||
| -rw-r--r-- | src/server/buffer.nim | 20 | ||||
| -rw-r--r-- | src/server/response.nim | 2 | ||||
| -rw-r--r-- | src/types/blob.nim | 2 | ||||
| -rw-r--r-- | src/utils/mimeguess.nim | 45 | ||||
| -rw-r--r-- | src/utils/twtstr.nim | 19 |
12 files changed, 154 insertions, 104 deletions
diff --git a/src/config/config.nim b/src/config/config.nim index 6809b42e..cb935a2a 100644 --- a/src/config/config.nim +++ b/src/config/config.nim @@ -303,14 +303,22 @@ proc openFileExpand(dir, file: string): FileStream = else: return newFileStream(dir / file) +proc openFileExpand2(dir, file: string): PosixStream = + if file.len == 0: + return nil + if file[0] == '/': + return newPosixStream(file) + else: + return newPosixStream(dir / file) + proc readUserStylesheet(dir, file: string): string = let x = ChaPath(file).unquote() if x.isNone: raise newException(ValueError, x.error) - let s = openFileExpand(dir, x.get) - if s != nil: - result = s.readAll() - s.close() + let ps = openFileExpand2(dir, x.get) + if ps != nil: + result = ps.recvAll() + ps.sclose() type ConfigParser = object config: Config @@ -614,9 +622,12 @@ proc parseConfigValue(ctx: var ConfigParser; x: var MimeTypes; v: TomlValue; ctx.parseConfigValue(paths, v, k) x = default(MimeTypes) for p in paths: - let f = openFileExpand(ctx.dir, p) - if f != nil: - x.parseMimeTypes(f) + let ps = openFileExpand2(ctx.dir, p) + if ps != nil: + let src = ps.recvDataLoopOrMmap() + x.parseMimeTypes(src.toOpenArray(), DefaultImages) + deallocMem(src) + ps.sclose() const DefaultMailcap = block: let ss = newStringStream(staticRead"res/mailcap") @@ -645,9 +656,10 @@ proc parseConfigValue(ctx: var ConfigParser; x: var URIMethodMap; v: TomlValue; ctx.parseConfigValue(paths, v, k) x = default(URIMethodMap) for p in paths: - let f = openFileExpand(ctx.dir, p) - if f != nil: - x.parseURIMethodMap(f.readAll()) + let ps = openFileExpand2(ctx.dir, p) + if ps != nil: + x.parseURIMethodMap(ps.recvAll()) + ps.sclose() x.append(DefaultURIMethodMap) func isCompatibleIdent(s: string): bool = @@ -685,10 +697,11 @@ proc parseConfig(config: Config; dir: string; t: TomlValue; var includes = config.`include` config.`include`.setLen(0) for s in includes: - let fs = openFileExpand(dir, s) - if fs == nil: + let ps = openFileExpand2(dir, s) + if ps == nil: return err("include file not found: " & s) - ?config.parseConfig(dir, fs.readAll(), warnings) + ?config.parseConfig(dir, ps.recvAll(), warnings) + ps.sclose() warnings.add(ctx.warnings) return ok() except ValueError as e: diff --git a/src/config/mimetypes.nim b/src/config/mimetypes.nim index d9e21aaf..e2aeb901 100644 --- a/src/config/mimetypes.nim +++ b/src/config/mimetypes.nim @@ -1,41 +1,78 @@ -import std/streams +import std/algorithm +import std/sets +import std/strutils import std/tables import utils/twtstr # extension -> type -type MimeTypes* = distinct Table[string, string] +type MimeTypes* = object + t: Table[string, string] # ext -> type + image*: Table[string, string] # ext -> image/(\w*) template getOrDefault*(mimeTypes: MimeTypes; k, fallback: string): string = - Table[string, string](mimeTypes).getOrDefault(k, fallback) + mimeTypes.t.getOrDefault(k, fallback) -template contains*(mimeTypes: MimeTypes; k: string): bool = - k in Table[string, string](mimeTypes) +# No error handling for now. +proc parseMimeTypes*(mimeTypes: var MimeTypes; buf: openArray[char]; + defaultImages: HashSet[string]) = + var i = 0 + while i < buf.len: + if buf[i] == '#': # comment + while i < buf.len and buf[i] != '\n': + inc i + else: + let t = buf.untilLower(AsciiWhitespace, i) + i += t.len + while i < buf.len and buf[i] != '\n': + i = buf.skipBlanksTillLF(i) + let ext = buf.untilLower(AsciiWhitespace, i) + i += ext.len + if ext.len > 0 and not mimeTypes.t.hasKeyOrPut(ext, t) and + t.startsWith("image/"): + let t = t.after('/') + # As a fingerprinting countermeasure: prevent additional + # extensions for predefined inline image type detection. + if t notin defaultImages: + mimeTypes.image[ext] = t + inc i -template hasKeyOrPut*(mimeTypes: var MimeTypes; k, v: string): bool = - Table[string, string](mimeTypes).hasKeyOrPut(k, v) +const DefaultGuess* = block: + var mimeTypes = MimeTypes() + mimeTypes.parseMimeTypes(staticRead"res/mime.types", initHashSet[string]()) + mimeTypes -# Add mime types found in stream to mimeTypes. -# No error handling for now. -proc parseMimeTypes*(mimeTypes: var MimeTypes; stream: Stream) = - var line: string - while stream.readLine(line): - if line.len == 0: - continue - if line[0] == '#': - continue - let t = line.untilLower(AsciiWhitespace) - if t == "": continue - var i = t.len - while i < line.len: - i = line.skipBlanks(i) - let ext = line.untilLower(AsciiWhitespace, i) - i += ext.len - if ext != "": - discard mimeTypes.hasKeyOrPut(ext, t) - stream.close() - -proc parseMimeTypes*(stream: Stream): MimeTypes = - var mimeTypes: MimeTypes - mimeTypes.parseMimeTypes(stream) - return mimeTypes +const DefaultImages* = block: + var s = initHashSet[string]() + for _, v in DefaultGuess.image: + s.incl(v) + s + +func guessContentType*(mimeTypes: MimeTypes; path: string; + fallback = "application/octet-stream"): string = + let ext = path.getFileExt() + if ext.len > 0: + return mimeTypes.getOrDefault(ext, fallback) + return fallback + +const JavaScriptTypes = [ + "application/ecmascript", + "application/javascript", + "application/x-ecmascript", + "application/x-javascript", + "text/ecmascript", + "text/javascript", + "text/javascript1.0", + "text/javascript1.1", + "text/javascript1.2", + "text/javascript1.3", + "text/javascript1.4", + "text/javascript1.5", + "text/jscript", + "text/livescript", + "text/x-ecmascript", + "text/x-javascript" +] + +func isJavaScriptType*(s: string): bool = + return JavaScriptTypes.binarySearch(s) != -1 diff --git a/src/html/dom.nim b/src/html/dom.nim index c865d815..b91553f8 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -11,6 +11,7 @@ import std/times import chagashi/charset import chagashi/decoder import chame/tags +import config/mimetypes import css/cssparser import css/cssvalues import css/mediaquery @@ -46,7 +47,6 @@ import types/path import types/referrer import types/url import types/winattrs -import utils/mimeguess import utils/strwidth import utils/twtstr @@ -106,6 +106,7 @@ type # list of streams that must be closed for canvas rendering on load pendingCanvasCtls*: seq[CanvasRenderingContext2D] urandom*: PosixStream + imageTypes*: Table[string, string] # Navigator stuff Navigator* = object @@ -3388,9 +3389,22 @@ proc loadResource*(window: Window; image: HTMLImageElement) = let contentType = response.getContentType("image/x-unknown") if not contentType.startsWith("image/"): return newResolvedPromise() + var t = contentType.after('/') + if t == "x-unknown": + let ext = response.url.pathname.getFileExt() + # Note: imageTypes is taken from mime.types. + # To avoid fingerprinting, we + # a) always download the entire image (through addCacheFile) - + # this prevents the server from knowing what content type + # is supported + # b) prevent mime.types extensions for images defined by + # ourselves + # In fact, a) would by itself be enough, but I'm not sure if + # it's the best way, so I added b) as a fallback measure. + t = window.imageTypes.getOrDefault(ext, "x-unknown") let cacheId = window.loader.addCacheFile(response.outputId, window.loader.clientPid) - let url = newURL("img-codec+" & contentType.after('/') & ":decode") + let url = newURL("img-codec+" & t & ":decode") if url.isNone: return newResolvedPromise() let request = newRequest( diff --git a/src/html/env.nim b/src/html/env.nim index 69592ccb..1b057fe3 100644 --- a/src/html/env.nim +++ b/src/html/env.nim @@ -332,8 +332,8 @@ proc runJSJobs*(window: Window) = ctx.writeException(window.console.err) proc newWindow*(scripting, images, styling: bool; attrs: WindowAttributes; - factory: CAtomFactory; loader: FileLoader; url: URL; urandom: PosixStream): - Window = + factory: CAtomFactory; loader: FileLoader; url: URL; urandom: PosixStream; + imageTypes: Table[string, string]): Window = let err = newDynFileStream(stderr) let window = Window( attrs: attrs, @@ -347,7 +347,8 @@ proc newWindow*(scripting, images, styling: bool; attrs: WindowAttributes; origin: url.origin ), factory: factory, - urandom: urandom + urandom: urandom, + imageTypes: imageTypes ) window.location = window.newLocation() if scripting: diff --git a/src/local/container.nim b/src/local/container.nim index b4fd7d2c..2edd0d38 100644 --- a/src/local/container.nim +++ b/src/local/container.nim @@ -29,7 +29,6 @@ import types/referrer import types/url import types/winattrs import utils/luwrap -import utils/mimeguess import utils/strwidth import utils/twtstr import utils/wordbreak diff --git a/src/local/pager.nim b/src/local/pager.nim index 61833418..d46bdce7 100644 --- a/src/local/pager.nim +++ b/src/local/pager.nim @@ -10,6 +10,7 @@ import chagashi/charset import config/chapath import config/config import config/mailcap +import config/mimetypes import css/render import io/bufreader import io/console @@ -48,7 +49,6 @@ import types/opt import types/url import types/winattrs import utils/luwrap -import utils/mimeguess import utils/regexutils import utils/strwidth import utils/twtstr @@ -1387,6 +1387,8 @@ proc applySiteconf(pager: Pager; url: URL; charsetOverride: Charset; res.metaRefresh = sc.meta_refresh.get loaderConfig.filter.allowschemes .add(pager.config.external.urimethodmap.imageProtos) + if res.images: + res.imageTypes = pager.config.external.mime_types.image return res # Load request in a new buffer. diff --git a/src/main.nim b/src/main.nim index 3a1c8daa..3c88ccf2 100644 --- a/src/main.nim +++ b/src/main.nim @@ -220,10 +220,12 @@ proc initConfig(ctx: ParamParseContext; config: Config; if (let ps = newPosixStream(getCurrentDir() / "res/config.toml"); ps != nil): ?config.parseConfig(getCurrentDir(), ps.recvAll(), warnings) + ps.sclose() if ps != nil: let src = ps.recvDataLoopOrMmap() ?config.parseConfig(config.dir, src.toOpenArray(), warnings) deallocMem(src) + ps.sclose() for opt in ctx.opts: ?config.parseConfig(getCurrentDir(), opt, warnings, laxnames = true) config.css.stylesheet &= ctx.stylesheet diff --git a/src/server/buffer.nim b/src/server/buffer.nim index abdc8cbd..f4e1d1b2 100644 --- a/src/server/buffer.nim +++ b/src/server/buffer.nim @@ -130,11 +130,12 @@ type scripting*: bool images*: bool isdump*: bool - charsets*: seq[Charset] - charsetOverride*: Charset - protocol*: Table[string, ProtocolConfig] autofocus*: bool + charsetOverride*: Charset metaRefresh*: MetaRefresh + charsets*: seq[Charset] + protocol*: Table[string, ProtocolConfig] + imageTypes*: Table[string, string] GetValueProc = proc(iface: BufferInterface; promise: EmptyPromise) {.nimcall.} @@ -1917,8 +1918,17 @@ proc launchBuffer*(config: BufferConfig; url: URL; attrs: WindowAttributes; cacheId: -1, outputId: -1, factory: factory, - window: newWindow(config.scripting, config.images, config.styling, attrs, - factory, loader, url, urandom) + window: newWindow( + config.scripting, + config.images, + config.styling, + attrs, + factory, + loader, + url, + urandom, + config.imageTypes + ) ) if buffer.config.scripting: buffer.window.navigate = proc(url: URL) = buffer.navigate(url) diff --git a/src/server/response.nim b/src/server/response.nim index e6ffe729..37ff2434 100644 --- a/src/server/response.nim +++ b/src/server/response.nim @@ -3,6 +3,7 @@ import std/tables import chagashi/charset import chagashi/decoder +import config/mimetypes import io/dynstream import io/promise import monoucha/javascript @@ -15,7 +16,6 @@ import types/blob import types/opt import types/referrer import types/url -import utils/mimeguess import utils/twtstr type diff --git a/src/types/blob.nim b/src/types/blob.nim index ea5c588d..f21d0954 100644 --- a/src/types/blob.nim +++ b/src/types/blob.nim @@ -2,12 +2,12 @@ import std/options import std/posix import std/strutils +import config/mimetypes import io/bufreader import io/bufwriter import monoucha/fromjs import monoucha/javascript import monoucha/jstypes -import utils/mimeguess type DeallocFun = proc(opaque, p: pointer) {.nimcall, raises: [].} diff --git a/src/utils/mimeguess.nim b/src/utils/mimeguess.nim deleted file mode 100644 index d6224303..00000000 --- a/src/utils/mimeguess.nim +++ /dev/null @@ -1,45 +0,0 @@ -import std/algorithm -import std/streams -import std/tables - -import config/mimetypes - -const DefaultGuess* = block: - let ss = newStringStream(staticRead"res/mime.types") - parseMimeTypes(ss) - -func guessContentType*(mimeTypes: MimeTypes; path: string; - fallback = "application/octet-stream"): string = - var n = 0 - for i in countdown(path.high, 0): - if path[i] == '/': - break - if path[i] == '.': - n = i - break - if n > 0: - let ext = path.substr(n + 1) - return mimeTypes.getOrDefault(ext, fallback) - return fallback - -const JavaScriptTypes = [ - "application/ecmascript", - "application/javascript", - "application/x-ecmascript", - "application/x-javascript", - "text/ecmascript", - "text/javascript", - "text/javascript1.0", - "text/javascript1.1", - "text/javascript1.2", - "text/javascript1.3", - "text/javascript1.4", - "text/javascript1.5", - "text/jscript", - "text/livescript", - "text/x-ecmascript", - "text/x-javascript" -] - -func isJavaScriptType*(s: string): bool = - return JavaScriptTypes.binarySearch(s) != -1 diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index 78dc982d..a5493738 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -235,6 +235,11 @@ func skipBlanks*(buf: openArray[char]; at: int): int = while result < buf.len and buf[result] in AsciiWhitespace: inc result +func skipBlanksTillLF*(buf: openArray[char]; at: int): int = + result = at + while result < buf.len and buf[result] in AsciiWhitespace: + inc result + func stripAndCollapse*(s: openArray[char]): string = var res = newStringOfCap(s.len) var space = false @@ -254,7 +259,7 @@ func until*(s: openArray[char]; c: set[char]; starti = 0): string = break result &= s[i] -func untilLower*(s: string; c: set[char]; starti = 0): string = +func untilLower*(s: openArray[char]; c: set[char]; starti = 0): string = result = "" for i in starti ..< s.len: if s[i] in c: @@ -834,3 +839,15 @@ iterator mypairs*[T](a: openArray[T]): tuple[key: int; val: lent T] {.inline.} = {.push overflowChecks: off.} inc i {.pop.} + +proc getFileExt*(path: string): string = + var n = 0 + for i in countdown(path.high, 0): + if path[i] == '/': + break + if path[i] == '.': + n = i + break + if n > 0: + return path.substr(n + 1) + return "" |