Repository navigation
Expand file tree
/
Copy pathplex-live-tv-elevate.njs
More file actions
104 lines (85 loc) · 2.84 KB
/
Copy pathplex-live-tv-elevate.njs
File metadata and controls
104 lines (85 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// nginx njs helper for docs/examples/plex-live-tv-elevate.nginx.conf.example.
//
// It mirrors the hardened Tunerr owner-token elevation allowlist:
// - safe methods only: GET, HEAD, OPTIONS
// - explicit Live TV/provider paths
// - transcode/playQueue helper requests only when path/uri targets Live TV
// - no arbitrary bait-query elevation on normal library paths
function ownerToken() {
return (ngx.env.PLEX_OWNER_TOKEN || '').trim();
}
function firstArg(r, name) {
var v = r.args[name];
if (Array.isArray(v)) {
return v.length ? String(v[0]) : '';
}
return v === undefined ? '' : String(v);
}
function liveTVText(s) {
s = String(s || '').toLowerCase();
return s.indexOf('/livetv/') !== -1 ||
s.indexOf('tv.plex.providers.epg.xmltv:') !== -1 ||
s.indexOf('livetv%2f') !== -1 ||
s.indexOf('tv.plex.providers.epg.xmltv%3a') !== -1;
}
function safeMethod(r) {
return r.method === 'GET' || r.method === 'HEAD' || r.method === 'OPTIONS';
}
function shouldElevate(r) {
if (!safeMethod(r) || ownerToken() === '') {
return false;
}
var path = r.uri || '';
if (path === '/media/providers' ||
path === '/media/grabbers/devices' ||
path.indexOf('/livetv/') === 0 ||
path.indexOf('/tv.plex.providers.epg.xmltv:') === 0) {
return true;
}
if (path.indexOf('/video/:/transcode/') === 0) {
return liveTVText(firstArg(r, 'path'));
}
if (path.indexOf('/playQueues') === 0) {
return liveTVText(firstArg(r, 'uri')) || liveTVText(firstArg(r, 'path'));
}
if (path === '/' || path === '/identity') {
return liveTVText(r.headersIn.Referer || r.headersIn.referer || '');
}
return false;
}
function inboundQueryToken(r) {
return firstArg(r, 'X-Plex-Token');
}
function inboundHeaderToken(r) {
return String(r.headersIn['X-Plex-Token'] || '');
}
function effectiveToken(r) {
if (shouldElevate(r)) {
return ownerToken();
}
return inboundHeaderToken(r) || inboundQueryToken(r);
}
function pushArg(parts, key, value) {
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
}
function effectiveArgs(r) {
var elevated = shouldElevate(r);
var parts = [];
var sawToken = false;
for (var key in r.args) {
var values = Array.isArray(r.args[key]) ? r.args[key] : [r.args[key]];
for (var i = 0; i < values.length; i++) {
if (key === 'X-Plex-Token') {
sawToken = true;
pushArg(parts, key, elevated ? ownerToken() : String(values[i]));
} else {
pushArg(parts, key, String(values[i]));
}
}
}
if (elevated && !sawToken) {
pushArg(parts, 'X-Plex-Token', ownerToken());
}
return parts.join('&');
}
export default { effectiveArgs, effectiveToken };