Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix={{branch}}-
type=sha

- name: Build and push Docker image
id: build
Expand Down
17 changes: 17 additions & 0 deletions modules/decode-post-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const decodePostId = function decodePostId (hashids, value) {
let decoded;

try {
decoded = hashids.decode(value);
} catch (decodeError) {
return null;
}

if (decoded.length !== 1 || !Number.isSafeInteger(decoded[0])) {
return null;
}

return decoded[0];
};

module.exports = decodePostId;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "node --test test/**/*.test.js",
"start": "node --env-file-if-exists=.env server.js",
"pretest": "eslint **/*.js",
"pretest": "eslint modules/decode-post-id.js test/**/*.js",
"dev": "node --watch --env-file-if-exists=.env server.js"
},
"keywords": [],
Expand Down
12 changes: 11 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { Op } = require('sequelize');
const { LRUCache } = require( 'lru-cache' );

const models = require( './models' );
const decodePostId = require( './modules/decode-post-id' );

const LISTEN_PORT = process.env.PORT || 3000;
const JSON_INDENTATION = 4;
Expand Down Expand Up @@ -683,11 +684,20 @@ server.get(
}
);
} else {
const decodedId = decodePostId( hashids, request.params.id );

if ( decodedId === null ) {
response.status( NOT_FOUND_STATUS_CODE );
response.end();

return;
}

query.where = Object.assign(
{},
query.where,
{
id: hashids.decode( request.params.id ),
id: decodedId,
}
);
}
Expand Down
28 changes: 28 additions & 0 deletions test/unit/decode-post-id.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable no-magic-numbers */
const test = require('node:test');
const assert = require('node:assert/strict');
const Hashids = require('hashids/cjs');

const decodePostId = require('../../modules/decode-post-id');

const hashids = new Hashids('', 8, 'abcdefghijklmnopqrstuvwxyz');

test('decodes a valid post hash to one numeric database ID', () => {
const encoded = hashids.encode(12345);

assert.equal(decodePostId(hashids, encoded), 12345);
});

test('returns null when Hashids rejects malformed input', () => {
assert.equal(decodePostId(hashids, 'invalid!'), null);
});

test('returns null when input decodes to no IDs', () => {
assert.equal(decodePostId(hashids, 'aaaaaaaa'), null);
});

test('returns null when input decodes to multiple IDs', () => {
const encoded = hashids.encode(123, 456);

assert.equal(decodePostId(hashids, encoded), null);
});
Loading