aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/local/pager.nim13
-rw-r--r--src/server/loader.nim62
-rw-r--r--src/server/loaderiface.nim11
-rw-r--r--src/types/url.nim11
4 files changed, 88 insertions, 9 deletions
diff --git a/src/local/pager.nim b/src/local/pager.nim
index c3f0f661..bde67037 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -1822,6 +1822,7 @@ proc applySiteconf(pager: Pager; url: URL; charsetOverride: Charset;
cookieMode: pager.config.buffer.cookie
)
loaderConfig = LoaderClientConfig(
+ originURL: url,
defaultHeaders: newHeaders(pager.config.network.default_headers),
cookiejar: nil,
proxy: pager.config.network.proxy,
@@ -1917,6 +1918,9 @@ proc gotoURL(pager: Pager; request: Request; prevurl = none(URL);
if ourl == nil:
break
request.url = ourl
+ if request.url.username != "" and request.url.password != "":
+ pager.loader.addAuth(request.url)
+ request.url.password = ""
if prevurl.isNone or
not prevurl.get.equals(request.url, excludeHash = true) or
request.url.hash == "" or request.httpMethod != hmGet or save:
@@ -2664,9 +2668,14 @@ proc connected2(pager: Pager; container: Container; res: MailcapResult;
# subtract status line height
attrs.height -= 1
attrs.heightPx -= attrs.ppl
+ var url = container.url
+ if url.username != "" or url.password != "":
+ url = newURL(url)
+ url.username = ""
+ url.password = ""
container.process = pager.forkserver.forkBuffer(
container.config,
- container.url,
+ url,
attrs,
cmfHTML in res.flags,
container.charsetStack
@@ -3094,9 +3103,9 @@ proc acceptBuffers(pager: Pager) =
if stream == nil:
pager.alert("Error: failed to set up buffer")
continue
+ let loader = pager.loader
let key = pager.addLoaderClient(container.process, container.loaderConfig,
container.clonedFrom)
- let loader = pager.loader
if item.istreamOutputId != -1: # new buffer
if container.cacheId == -1:
container.cacheId = loader.addCacheFile(item.istreamOutputId,
diff --git a/src/server/loader.nim b/src/server/loader.nim
index b5e0ee91..6e26b229 100644
--- a/src/server/loader.nim
+++ b/src/server/loader.nim
@@ -244,6 +244,11 @@ proc close(handle: InputHandle) =
output.oclose()
type
+ AuthItem = ref object
+ origin: Origin
+ username: string
+ password: string
+
ClientData = ref object
pid: int
key: ClientKey
@@ -252,6 +257,8 @@ type
# List of file descriptors passed by the client.
passedFdMap: seq[tuple[name: string; fd: cint]] # host -> fd
config: LoaderClientConfig
+ # List of credentials the client has access to (same origin only).
+ authMap: seq[AuthItem]
LoaderContext = ref object
pagerClient: ClientData
@@ -264,6 +271,8 @@ type
clientData: Table[int, ClientData] # pid -> data
# ID of next output. TODO: find a better allocation scheme
outputNum: int
+ # List of *all* credentials the loader knows of.
+ authMap: seq[AuthItem]
LoaderConfig* = object
cgiDir*: seq[string]
@@ -695,10 +704,25 @@ proc loadStreamRegular(ctx: LoaderContext; handle, cachedHandle: InputHandle) =
handle.outputs.setLen(0)
handle.iclose()
-proc putMappedURL(url: URL) =
+proc findItem(authMap: seq[AuthItem]; origin: Origin): AuthItem =
+ for it in authMap:
+ if origin.isSameOrigin(it.origin):
+ return it
+ return nil
+
+proc findAuth(client: ClientData; url: URL): AuthItem =
+ if client.authMap.len > 0:
+ return client.authMap.findItem(url.authOrigin)
+ return nil
+
+proc putMappedURL(url: URL; auth: AuthItem) =
putEnv("MAPPED_URI_SCHEME", url.scheme)
- putEnv("MAPPED_URI_USERNAME", url.username)
- putEnv("MAPPED_URI_PASSWORD", url.password)
+ if auth != nil:
+ putEnv("MAPPED_URI_USERNAME", auth.username)
+ putEnv("MAPPED_URI_PASSWORD", auth.password)
+ else:
+ delEnv("MAPPED_URI_USERNAME")
+ delEnv("MAPPED_URI_PASSWORD")
putEnv("MAPPED_URI_HOST", url.hostname)
putEnv("MAPPED_URI_PORT", url.port)
putEnv("MAPPED_URI_PATH", url.pathname)
@@ -713,7 +737,7 @@ type CGIPath = object
myDir: string
proc setupEnv(cpath: CGIPath; request: Request; contentLen: int; prevURL: URL;
- config: LoaderClientConfig) =
+ config: LoaderClientConfig; auth: AuthItem) =
let url = request.url
putEnv("SCRIPT_NAME", cpath.scriptName)
putEnv("SCRIPT_FILENAME", cpath.cmd)
@@ -724,7 +748,7 @@ proc setupEnv(cpath: CGIPath; request: Request; contentLen: int; prevURL: URL;
headers &= k & ": " & v & "\r\n"
putEnv("REQUEST_HEADERS", headers)
if prevURL != nil:
- putMappedURL(prevURL)
+ putMappedURL(prevURL, auth)
if cpath.pathInfo != "":
putEnv("PATH_INFO", cpath.pathInfo)
if url.search != "":
@@ -865,8 +889,9 @@ proc loadCGI(ctx: LoaderContext; client: ClientData; handle: InputHandle;
istream.sclose()
else:
closeStdin()
+ let auth = if prevURL != nil: client.findAuth(prevURL) else: nil
# we leave stderr open, so it can be seen in the browser console
- setupEnv(cpath, request, contentLen, prevURL, config)
+ setupEnv(cpath, request, contentLen, prevURL, config, auth)
# reset SIGCHLD to the default handler. this is useful if the child process
# expects SIGCHLD to be untouched. (e.g. git dies a horrible death with
# SIGCHLD as SIG_IGN)
@@ -1141,6 +1166,11 @@ proc addClient(ctx: LoaderContext; stream: SocketStream;
for item in client2.cacheMap:
inc item.refc
client.cacheMap = client2.cacheMap
+ if ctx.authMap.len > 0:
+ let origin = config.originURL.authOrigin
+ for it in ctx.authMap:
+ if it.origin.isSameOrigin(origin):
+ client.authMap.add(it)
w.swrite(true)
stream.sclose()
@@ -1262,6 +1292,23 @@ proc tee(ctx: LoaderContext; stream: SocketStream; client: ClientData;
w.swrite(-1)
stream.sclose()
+proc addAuth(ctx: LoaderContext; stream: SocketStream; r: var BufferedReader) =
+ var url: URL
+ r.sread(url)
+ let origin = url.authOrigin
+ let item = ctx.authMap.findItem(origin)
+ if item != nil:
+ item.username = url.username
+ item.password = url.password
+ else:
+ let item = AuthItem(
+ origin: url.authOrigin,
+ username: url.username,
+ password: url.password
+ )
+ ctx.authMap.add(item)
+ ctx.pagerClient.authMap.add(item)
+
proc suspend(ctx: LoaderContext; stream: SocketStream; client: ClientData;
r: var BufferedReader) =
var ids: seq[int]
@@ -1324,6 +1371,9 @@ proc acceptConnection(ctx: LoaderContext) =
of lcAddClient:
privileged_command
ctx.addClient(stream, r)
+ of lcAddAuth:
+ privileged_command
+ ctx.addAuth(stream, r)
of lcRemoveClient:
privileged_command
ctx.removeClient(stream, r)
diff --git a/src/server/loaderiface.nim b/src/server/loaderiface.nim
index d76ee14f..58dc12a2 100644
--- a/src/server/loaderiface.nim
+++ b/src/server/loaderiface.nim
@@ -56,11 +56,13 @@ type
response*: Response
LoaderCommand* = enum
+ lcAddAuth
lcAddCacheFile
lcAddClient
lcGetCacheFile
lcLoad
lcLoadConfig
+ lcOpenCachedItem
lcPassFd
lcRedirectToFile
lcRemoveCachedItem
@@ -69,11 +71,11 @@ type
lcShareCachedItem
lcSuspend
lcTee
- lcOpenCachedItem
ClientKey* = array[32, uint8]
LoaderClientConfig* = object
+ originURL*: URL
cookieJar*: CookieJar
defaultHeaders*: Headers
filter*: URLFilter
@@ -417,6 +419,13 @@ proc removeCachedItem*(loader: FileLoader; cacheId: int) =
w.swrite(cacheId)
stream.sclose()
+proc addAuth*(loader: FileLoader; url: URL) =
+ let stream = loader.connect()
+ if stream != nil:
+ stream.withLoaderPacketWriter loader, w:
+ w.swrite(lcAddAuth)
+ w.swrite(url)
+
proc addClient*(loader: FileLoader; key: ClientKey; pid: int;
config: LoaderClientConfig; clonedFrom: int): bool =
let stream = loader.connect()
diff --git a/src/types/url.nim b/src/types/url.nim
index 52aa67c3..0c460bbd 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -1169,6 +1169,17 @@ proc origin*(url: URL): Origin =
else:
return Origin(t: otOpaque, s: $url)
+# This follows somewhat different rules from the standard:
+# * with htNone, the origin is opaque.
+# * with other host types, the origin is a tuple origin.
+proc authOrigin*(url: URL): Origin =
+ if url.hostType != htNone:
+ return Origin(
+ t: otTuple,
+ tup: (url.scheme, url.hostname, url.port, none(string))
+ )
+ return Origin(t: otOpaque, s: $url)
+
proc `==`*(a, b: Origin): bool {.error.} =
discard