@jacebenson/jsn 5.5.6-dev.209.1 → 5.5.6-dev.210.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacebenson/jsn",
3
- "version": "5.5.6-dev.209.1",
3
+ "version": "5.5.6-dev.210.1",
4
4
  "description": "A command-line interface for ServiceNow that follows the Unix philosophy: simple, composable, and scriptable.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -118,20 +118,52 @@ export function ensureDocsCacheDir() {
118
118
  * { dir, prefix } so community content can live under a `community/` prefix
119
119
  * without colliding with the ServiceNowDocs markdown tree.
120
120
  */
121
+ // The caller-selected lexical root is the authorization boundary, not whatever
122
+ // realpath happens to point at. Reject aliases in *every* component (including
123
+ // junctions on Windows), before using realpath to pin that boundary. Missing
124
+ // optional roots remain optional; dangling links are not missing directories.
125
+ export function resolveDocsRoot(dir) {
126
+ const absolute = path.resolve(dir);
127
+ const anchor = path.parse(absolute).root;
128
+ let current = anchor;
129
+ for (const component of absolute.slice(anchor.length).split(path.sep).filter(Boolean)) {
130
+ current = path.join(current, component);
131
+ let stat;
132
+ try { stat = fs.lstatSync(current); }
133
+ catch (error) { if (error.code === 'ENOENT') return null; throw error; }
134
+ if (stat.isSymbolicLink()) throw new Error(`Documentation source contains a symbolic link or junction alias: ${current}`);
135
+ if (!stat.isDirectory()) throw new Error(`Documentation source is not a directory: ${current}`);
136
+ }
137
+ const canonical = fs.realpathSync(absolute);
138
+ if (path.relative(absolute, canonical) !== '') throw new Error(`Documentation source resolves through an alias: ${absolute}`);
139
+ return canonical;
140
+ }
141
+
142
+ function containedDocsPath(root, file) {
143
+ const relative = path.relative(root, fs.realpathSync(file));
144
+ if (relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
145
+ throw new Error(`Documentation source escapes its root: ${file}`);
146
+ }
147
+ }
148
+
121
149
  export function* walkDocs(roots) {
122
150
  function* walk(dir, root, prefix) {
151
+ containedDocsPath(root, dir);
123
152
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
124
153
  const full = path.join(dir, entry.name);
125
154
  if (entry.isDirectory()) {
126
155
  yield* walk(full, root, prefix);
127
156
  } else if (entry.isFile() && entry.name.toLowerCase().endsWith('.md')) {
157
+ containedDocsPath(root, full);
128
158
  const rel = prefix + path.relative(root, full).split(path.sep).join('/');
129
159
  yield { file: full, rel };
130
160
  }
131
161
  }
132
162
  }
133
- for (const root of roots) {
134
- if (!fs.existsSync(root.dir)) continue;
163
+ // Validate every root before yielding any document from any root.
164
+ const validated = roots.map(root => ({ ...root, dir: resolveDocsRoot(root.dir) }));
165
+ for (const root of validated) {
166
+ if (!root.dir) continue;
135
167
  yield* walk(root.dir, root.dir, root.prefix || '');
136
168
  }
137
169
  }
@@ -7,7 +7,7 @@ import { docsReaderLease } from './locks.js';
7
7
  import { errUsageHint } from '../../errors.js';
8
8
  import { createHash } from 'node:crypto';
9
9
  import { parseFrontmatter } from '../../frontmatter.js';
10
- import { withDocsDb, resolveDocsDbPath, walkDocs, defaultDocsRoots, getDocsSourceDir } from './db.js';
10
+ import { withDocsDb, resolveDocsDbPath, resolveDocsRoot, walkDocs, defaultDocsRoots, getDocsSourceDir } from './db.js';
11
11
  import { encodeText, phasesToBytes, docSurface, DEFAULT_DIM } from './hrr.js';
12
12
 
13
13
  export function refreshDocs(opts = {}) {
@@ -20,6 +20,7 @@ function refreshDocsLocked(opts = {}) {
20
20
  const dbPath = opts.dbPath || resolveDocsDbPath(opts.release);
21
21
  const roots = opts.roots || defaultDocsRoots(opts);
22
22
 
23
+ for (const root of roots) resolveDocsRoot(root.dir);
23
24
  if (roots.some(root => (!root.prefix || opts.roots) && (!fs.existsSync(root.dir) || !fs.statSync(root.dir).isDirectory()))) {
24
25
  throw errUsageHint('Raw documentation source is missing; existing index retained.', 'Use jsn docs install --source snapshot to update a prebuilt index, or --source github to install raw source before refreshing.');
25
26
  }
@@ -10,7 +10,7 @@ import { ingestDocs } from './ingest.js';
10
10
  import { refreshDocs } from './refresh.js';
11
11
  import { installSnapshot } from './snapshot.js';
12
12
  import { publishServiceSnapshot } from './service-snapshot.js';
13
- import { DEFAULT_DOCS_RELEASE, resolveDocsDbPath, resolveDocsRelease } from './db.js';
13
+ import { DEFAULT_DOCS_RELEASE, resolveDocsDbPath, resolveDocsRelease, resolveDocsRoot } from './db.js';
14
14
 
15
15
  const defaultStorage = path.join(process.env.JSN_DATA_HOME || path.join(os.homedir(), '.jsn'), 'docs-service');
16
16
  function git(args) {
@@ -30,6 +30,8 @@ async function performSync(options = {}) {
30
30
  const branch = family ? resolveDocsRelease(family) : options.branch || process.env.DOCS_SOURCE_BRANCH || DEFAULT_DOCS_RELEASE;
31
31
  fs.mkdirSync(storage, { recursive: true });
32
32
  const repo = path.join(storage, 'source');
33
+ resolveDocsRoot(repo);
34
+ resolveDocsRoot(path.join(repo, 'markdown'));
33
35
  const live = family ? resolveDocsDbPath(family) : path.join(storage, 'docs.sqlite3');
34
36
  const staging = path.join(storage, `build-${randomUUID()}.sqlite3`);
35
37
  let previous = {};
@@ -89,7 +91,7 @@ async function performSync(options = {}) {
89
91
  }
90
92
  const revision = git(['-C', repo, 'rev-parse', 'HEAD']);
91
93
  const markdown = path.join(repo, 'markdown');
92
- if (!fs.statSync(markdown).isDirectory()) throw new Error('Source has no markdown directory');
94
+ if (!resolveDocsRoot(markdown)) throw new Error('Source has no markdown directory');
93
95
  let oldRevision;
94
96
  if (fs.existsSync(live)) {
95
97
  const db = new Database(live, { readonly: true });