Skip to content
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

Allow whitelisted HTTP headers to pass through to shiny apps #257

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
340 changes: 194 additions & 146 deletions config.html

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions config/shiny-server-rules.config
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,10 @@ sanitize_errors {
at $ server location;
maxcount 1;
}

whitelist_headers {
desc "Allow specific headers to pass through to the shiny apps, useful to detect browser attributes or get specific information from a proxy.";
param String names... "The headers to let through. Examples are: referer, user-agent, x-my-special-header";
at $ server location;
maxcount 1;
}
14 changes: 14 additions & 0 deletions config/whitelist_headers.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;

# Define a server that listens on port 3838
server {
listen 3838;

# Define a location at the base URL
location / {

# Allow referer and user-agent to pass through from the original request to the shiny app
whitelist_headers referer user-agent;
}
}
6 changes: 4 additions & 2 deletions lib/proxy/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ function ShinyProxy(router, schedulerRegistry) {
req.url = '/' + req.url;
// TODO: Also strip path members


var headers = _.pick(req.headers, appSpec.settings.appDefaults.whitelistedHeaders);
//TODO: clean this up. This should be a part of the promise chain, but
// when returning an error, it would just crash as an unhandled excptn
// and never make it into the .fail().
var wrk;
try{
// Extract the non-param portion of the URL
wrk = schedulerRegistry.getWorker_p(appSpec, req.url.match(/([^\?]+)(\?.*)?/)[1],
worker)
wrk = schedulerRegistry.getWorker_p(appSpec, req.url.match(/([^\?]+)(\?.*)?/)[1],
worker, headers)
}
catch(err){
if (err instanceof OutOfCapacityError){
Expand Down
2 changes: 1 addition & 1 deletion lib/proxy/sockjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function createServer(router, schedulerRegistry) {
}

pathInfo = pathInfo.replace(/\/__sockjs__\/.*/, "/websocket/");
wsClient = appWorkerHandle.endpoint.createWebSocketClient(pathInfo);
wsClient = appWorkerHandle.endpoint.createWebSocketClient(pathInfo, appWorkerHandle.headers);

wsClient.onopen = function(event) {
conn.removeListener('data', connDataHandler);
Expand Down
7 changes: 7 additions & 0 deletions lib/router/config-router-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function parseApplication(settings, locNode, provideDefaults){
appSettings.sanitizeErrors = true;
appSettings.disableProtocols = [];
appSettings.bookmarkStateDir = '/var/lib/shiny-server/bookmarks';
appSettings.whitelistedHeaders = [];
}

if (locNode.getValues('app_idle_timeout') &&
Expand Down Expand Up @@ -89,6 +90,12 @@ function parseApplication(settings, locNode, provideDefaults){
}
});
}

var whitelistHeadersNode = locNode.getOne('whitelist_headers', true);
if (whitelistHeadersNode && whitelistHeadersNode.values.names) {
appSettings.whitelistedHeaders = whitelistHeadersNode.values.names;
}

if (locNode.getValues('disable_websockets').val){
appSettings.disableProtocols.push('websocket');
}
Expand Down
8 changes: 4 additions & 4 deletions lib/scheduler/scheduler-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = SchedulerRegistry;
* targetting. If left blank, an arbitrary worker will be selected. Ignored
* when using the SimpleScheduler.
*/
this.getWorker_p = function(appSpec, url, worker) {
this.getWorker_p = function(appSpec, url, worker, headers) {
var key = appSpec.getKey();
if (!this.$schedulers[key]){
//no scheduler, instantiate a simple scheduler
Expand All @@ -72,9 +72,9 @@ module.exports = SchedulerRegistry;
if (this.$transport){
this.$schedulers[key].setTransport(this.$transport);
}
}
return this.$schedulers[key].acquireWorker_p(appSpec, url, worker);
}

return this.$schedulers[key].acquireWorker_p(appSpec, url, worker, headers);
};

this.shutdown = function() {
Expand Down
2 changes: 1 addition & 1 deletion lib/scheduler/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function connectEndpoint_p(endpoint, timeout, shouldContinue) {
return workerPromise.then(function(appWorker) {
var appWorkerHandle = new AppWorkerHandle(appSpec, endpoint,
logFilePath, exitPromise,
_.bind(appWorker.kill, appWorker));
_.bind(appWorker.kill, appWorker), workerData.headers);


var initTimeout = 60 * 1000;
Expand Down
4 changes: 2 additions & 2 deletions lib/scheduler/simple-scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ util.inherits(SimpleScheduler, Scheduler);
* Defines how this schedule will identify and select an R process to
* fulfill its requests.
*/
this.acquireWorker_p = function(appSpec, url){
this.acquireWorker_p = function(appSpec, url, worker, headers){
if (this.$workers && _.size(this.$workers) > 0) {
logger.trace('Reusing existing instance');

Expand Down Expand Up @@ -65,7 +65,7 @@ util.inherits(SimpleScheduler, Scheduler);
}

logger.trace('Spawning new instance');
return this.spawnWorker_p(appSpec);
return this.spawnWorker_p(appSpec, {headers: headers});
};

}).call(SimpleScheduler.prototype);
3 changes: 2 additions & 1 deletion lib/worker/app-worker-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ var events = require('events');
var util = require('util');

var AppWorkerHandle = function(appSpec, endpoint, logFilePath,
exitPromise, kill) {
exitPromise, kill, headers) {

events.EventEmitter.call(this);
this.appSpec = appSpec;
this.endpoint = endpoint;
this.logFilePath = logFilePath;
this.exitPromise = exitPromise;
this.kill = kill;
this.headers = headers;
};
module.exports = AppWorkerHandle;

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@
"license": "AGPL-3.0",
"engines": {
"node": ">=6.6.0"
},
"devDependencies": {
"escape-html": "^1.0.3"
}
}
19 changes: 19 additions & 0 deletions samples/sample-apps/headers/server.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library(shiny)

shinyServer(function(input, output, session) {

output$summary <- renderText({
ls(env=session$request)
})

output$headers <- renderUI({
selectInput("header", "Header:", ls(env=session$request))
})

output$value <- renderText({
if (nchar(input$header) < 1 || !exists(input$header, envir=session$request)){
return("NULL");
}
return (get(input$header, envir=session$request));
})
})
12 changes: 12 additions & 0 deletions samples/sample-apps/headers/ui.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
shinyUI(pageWithSidebar(
headerPanel("Shiny Client Data"),
sidebarPanel(
uiOutput("headers")
),
mainPanel(
h3("Headers passed into Shiny"),
verbatimTextOutput("summary"),
h3("Value of specified header"),
verbatimTextOutput("value")
)
))
4 changes: 2 additions & 2 deletions tools/makedocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* This script is for generating config.html. It is designed to run under
* node-supervisor (https://github.com/isaacs/node-supervisor).
*
* supervisor -n exit --extensions 'js|html' lib/makedocs.js
* supervisor -n exit --extensions 'js|html' tools/makedocs.js
*/

var fs = require('fs');
var path = require('path');
var util = require('util');
var htmlEscape = require('connect/lib/utils').escape;
var htmlEscape = require('escape-html');
var Handlebars = require('handlebars');
var _ = require('underscore');
var map = require('../lib/core/map');
Expand Down