@ranzenjs/client 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/state.d.ts CHANGED
@@ -7,3 +7,5 @@ export interface ConnectionState {
7
7
  }
8
8
  export declare function writeState({ account, secret, cookies }: ConnectionState): string;
9
9
  export declare function readState(text: string): ConnectionState;
10
+ export declare function copyCookie({ name, value, domain, hostOnly, path, secure, expires }: StoredCookie): StoredCookie;
11
+ export declare function isCookie(value: unknown): value is StoredCookie;
package/dist/state.js CHANGED
@@ -38,15 +38,19 @@ export function readState(text) {
38
38
  return {
39
39
  account: { schoolId, username },
40
40
  secret: key,
41
- cookies: cookies.map(({ name, value, domain, hostOnly, path, secure, expires }) => expires === undefined
42
- ? { name, value, domain, hostOnly, path, secure }
43
- : { name, value, domain, hostOnly, path, secure, expires })
41
+ cookies: cookies.map(copyCookie)
44
42
  };
45
43
  }
44
+ // Only the known fields, so stray keys in stored state never reach the jar.
45
+ export function copyCookie({ name, value, domain, hostOnly, path, secure, expires }) {
46
+ return expires === undefined
47
+ ? { name, value, domain, hostOnly, path, secure }
48
+ : { name, value, domain, hostOnly, path, secure, expires };
49
+ }
46
50
  function isRecord(value) {
47
51
  return typeof value === 'object' && value !== null && !Array.isArray(value);
48
52
  }
49
- function isCookie(value) {
53
+ export function isCookie(value) {
50
54
  return (isRecord(value) &&
51
55
  typeof value.name === 'string' &&
52
56
  typeof value.value === 'string' &&
@@ -25,6 +25,7 @@ export class CookieJar {
25
25
  secure: false
26
26
  };
27
27
  let maxAge;
28
+ let pathSet = false;
28
29
  for (const attribute of attributes) {
29
30
  const index = attribute.indexOf('=');
30
31
  const key = (index < 0 ? attribute : attribute.slice(0, index)).trim().toLowerCase();
@@ -36,8 +37,10 @@ export class CookieJar {
36
37
  cookie.domain = domain;
37
38
  cookie.hostOnly = false;
38
39
  }
39
- else if (key === 'path' && value.startsWith('/'))
40
+ else if (key === 'path' && value.startsWith('/')) {
40
41
  cookie.path = value;
42
+ pathSet = true;
43
+ }
41
44
  else if (key === 'secure')
42
45
  cookie.secure = true;
43
46
  else if (key === 'max-age' && /^-?\d+$/.test(value))
@@ -50,6 +53,8 @@ export class CookieJar {
50
53
  }
51
54
  if (maxAge !== undefined)
52
55
  cookie.expires = this.now() + maxAge * 1000;
56
+ if (!prefixAllows(cookie, url, pathSet))
57
+ return;
53
58
  const key = `${cookie.domain};${cookie.path};${cookie.name}`;
54
59
  if (cookie.expires !== undefined && cookie.expires <= this.now())
55
60
  this.cookies.delete(key);
@@ -90,6 +95,15 @@ export class CookieJar {
90
95
  .sort((a, b) => b.path.length - a.path.length);
91
96
  }
92
97
  }
98
+ // RFC 6265bis §4.1.3: __Secure- needs Secure over https; __Host- also needs no Domain and an explicit Path=/.
99
+ function prefixAllows(cookie, url, pathSet) {
100
+ const name = cookie.name.toLowerCase();
101
+ const secure = cookie.secure && url.protocol === 'https:';
102
+ if (name.startsWith('__host-')) {
103
+ return secure && cookie.hostOnly && pathSet && cookie.path === '/';
104
+ }
105
+ return !name.startsWith('__secure-') || secure;
106
+ }
93
107
  function domainMatches(host, domain) {
94
108
  return host === domain || host.endsWith(`.${domain}`);
95
109
  }
@@ -6,6 +6,8 @@ export interface TransportSettings {
6
6
  userAgent: string;
7
7
  timeoutMs: number;
8
8
  maxConcurrency: number;
9
+ /** Hosts requests may reach over https; defaults to Schulportal's. */
10
+ hosts?: ReadonlySet<string>;
9
11
  }
10
12
  export interface Exchange {
11
13
  url: string | URL;
@@ -29,6 +31,7 @@ export interface Reply extends Head {
29
31
  export interface StreamReply extends Head {
30
32
  body: ReadableStream<Uint8Array>;
31
33
  }
34
+ export declare const PORTAL_HOSTS: ReadonlySet<string>;
32
35
  export declare class Transport {
33
36
  private readonly settings;
34
37
  readonly jar: CookieJar;
@@ -2,7 +2,7 @@ import { RanzenError, fail, layoutChanged } from '../errors.js';
2
2
  import { CookieJar } from './cookies.js';
3
3
  import { Limiter } from './limiter.js';
4
4
  // The hosts sign-in and apps use; any other host, subdomain or port is a portal change.
5
- const PORTAL_HOSTS = new Set(['start', 'login', 'connect'].map((name) => `${name}.schulportal.hessen.de`));
5
+ export const PORTAL_HOSTS = new Set(['start', 'login', 'connect'].map((name) => `${name}.schulportal.hessen.de`));
6
6
  const MAX_REDIRECTS = 10;
7
7
  export class Transport {
8
8
  settings;
@@ -113,7 +113,7 @@ export class Transport {
113
113
  let method = exchange.method ?? 'GET';
114
114
  let body = exchange.multipart ?? (exchange.form && new URLSearchParams(exchange.form).toString());
115
115
  for (let hop = 0;; hop++) {
116
- if (url.protocol !== 'https:' || !PORTAL_HOSTS.has(url.host)) {
116
+ if (url.protocol !== 'https:' || !(this.settings.hosts ?? PORTAL_HOSTS).has(url.host)) {
117
117
  throw layoutChanged('a portal address');
118
118
  }
119
119
  const headers = new Headers(exchange.headers);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ranzenjs/client",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Typed client for Schulportal Hessen (SPH, LANIS)",
5
5
  "license": "MIT",
6
6
  "author": "Suko",
@@ -42,6 +42,14 @@
42
42
  "types": "./dist/apps/catalog/index.d.ts",
43
43
  "default": "./dist/apps/catalog/index.js"
44
44
  },
45
+ "./calendar": {
46
+ "types": "./dist/apps/calendar/index.d.ts",
47
+ "default": "./dist/apps/calendar/index.js"
48
+ },
49
+ "./kurswahl": {
50
+ "types": "./dist/kurswahl/index.d.ts",
51
+ "default": "./dist/kurswahl/index.js"
52
+ },
45
53
  "./html": {
46
54
  "types": "./dist/html.d.ts",
47
55
  "default": "./dist/html.js"
@@ -66,6 +74,7 @@
66
74
  "dependencies": {
67
75
  "css-select": "^7.0.0",
68
76
  "domhandler": "^6.0.1",
77
+ "hash-wasm": "^4.12.0",
69
78
  "htmlparser2": "^12.0.0"
70
79
  },
71
80
  "devDependencies": {