Skip to content

Secure raw url handler #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pastebin/data.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
(use-modules (srfi srfi-9)
(srfi srfi-1)
(ice-9 ftw)
(ice-9 regex)
(ice-9 textual-ports))

(export <pb-data>
<pb-entry>
pb-entry-id
pb-entry-id-valid?
pb-entry-text
pb-data-open
pb-data-close
Expand All @@ -27,6 +29,11 @@
(id pb-entry-id set-pb-entry-id!)
(text pb-entry-text set-pb-entry-text!))

(define (pb-entry-id-valid? id)
(and (= (string-length id) 5)
(string-match "[0-9A-Za-z]{5}" id)
#t))

;; input: dir: string
;; output: <pb-data>
(define (pb-data-open dir)
Expand Down
42 changes: 23 additions & 19 deletions pastebin/httpserver.scm
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@
(values (build-response #:code 400)
(lambda (port) 1))))

(define (raw-handler pb-data-path pb-id)
(if (pb-entry-id-valid? pb-id)
(values (build-response
#:code 200
#:headers '((content-type . (text/plain))))
(lambda (port)
(call-with-input-file
;; the file name
(call-with-dir-as-pb-data
pb-data-path
(lambda (p) (pb-get-file-path p pb-id)))
;; the input port
(lambda (inport)
(let A ((inport' inport))
(let ((bv (get-bytevector-n inport' 4096)))
(if (not (eof-object? bv))
(begin
(put-bytevector port bv)
(A inport')))))))))
(values (build-response #:code 404)
(lambda (port) 1))))

(define (make-pastebin-handler data-path)
(lambda (request request-body)
(match (split-and-decode-uri-path (uri-path (request-uri request)))
Expand All @@ -118,25 +140,7 @@

;; URI: /raw/<id> -- return raw content of the paste
(("raw" pb-id)
(values (build-response
#:code 200
#:headers '((content-type . (text/plain))))

(lambda (port)
(call-with-input-file
;; the file name
(call-with-dir-as-pb-data
data-path
(lambda (p) (pb-get-file-path p pb-id)))

;; the input port
(lambda (inport)
(let A ((inport' inport))
(let ((bv (get-bytevector-n inport' 4096)))
(if (not (eof-object? bv))
(begin
(put-bytevector port bv)
(A inport'))))))))))
(raw-handler data-path pb-id))

;; URI: * -- everything else -- show the top 5 paste list
(_
Expand Down