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/html | |
| 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/html')
| -rw-r--r-- | src/html/dom.nim | 18 | ||||
| -rw-r--r-- | src/html/env.nim | 7 |
2 files changed, 20 insertions, 5 deletions
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: |