Skip to content

Commit

Permalink
fix: throw error for invalid $refs missing the / (#102)
Browse files Browse the repository at this point in the history
* test: add test to check for component names missing the `/`

* test: add test case file

* fix: throw error for component names missing the `/`

* refacotr: update logic

* refactor: revert logic

* refactor: update logic

* refactor: update logic

* chore: update eslint ecma version

* chore: fix lint issues
  • Loading branch information
darrenyong authored Oct 24, 2023
1 parent f9bd203 commit a44c276
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ rules:
no-use-before-define: off
node/no-deprecated-api: off # `url.parse` has been deprecated but `url.URL` isn't a good replacement yet.
prefer-rest-params: off
parserOptions:
ecmaVersion: 2020
14 changes: 14 additions & 0 deletions lib/dereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { ono } = require('@jsdevtools/ono');

const Pointer = require('./pointer');
const $Ref = require('./ref');
const { InvalidPointerError } = require('./util/errors');
const url = require('./util/url');

/**
Expand Down Expand Up @@ -82,6 +83,19 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
const keyPath = Pointer.join(path, key);
const keyPathFromRoot = Pointer.join(pathFromRoot, key);
const value = obj[key];
// Ensure that there are valid refs. Refs like #components/schemas is invalid
const refSlashSplit = value?.$ref?.split('/');

// Catches external file refs: /absolute-root/absolute-root.yaml
const refHashSplit = value?.ref?.split('#');

// Valid refs split on `/` should have an element with '#' as the last character
// #components/schemas will not return a "validRef"
const validRef = refSlashSplit?.find(ele => ele.slice(-1) === '#');
if (refSlashSplit?.length > 1 && refHashSplit?.length > 1 && !validRef) {
throw new InvalidPointerError(value.$ref, keyPath);
}

let circular = false;

if ($Ref.isAllowed$Ref(value, options)) {
Expand Down
10 changes: 10 additions & 0 deletions test/specs/invalid-pointers/invalid-pointers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ describe('Schema with invalid pointers', function () {
}
});

it('should throw an error for an invalid reference', async function () {
try {
await $RefParser.dereference(path.rel('specs/invalid-pointers/invalid-reference.json'));
helper.shouldNotGetCalled();
} catch (err) {
expect(err).to.be.an.instanceOf(InvalidPointerError);
expect(err.message).to.contain('Invalid $ref pointer "#components/". Pointers must begin with "#/"');
}
});

it('should throw a grouped error for an invalid pointer if continueOnError is true', async function () {
const parser = new $RefParser();
try {
Expand Down
5 changes: 5 additions & 0 deletions test/specs/invalid-pointers/invalid-reference.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"foo": {
"$ref": "#components/"
}
}

0 comments on commit a44c276

Please sign in to comment.